四川省ACM省赛B题,优先队列+BFS Charitable Exchange

题目链接:http://acm.uestc.edu.cn/problem.php?pid=1558&cid=129

Description
Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values 1 yuan. Then, through the efforts of repeatedly exchanges which continuously increase the value of item in hand, he (she) finally brings back a valuable item and donates it to the needy.
In each exchange, one can exchange for an item of Vi yuan if he (she) has an item values more than or equal to Ri yuan, with a time cost of Ti minutes.
Now, you task is help the star to exchange for an item which values more than or equal to M yuan with the minimum time.

Input


The first line of the input is T (no more than 20), which stands for the number of test cases you need to solve.
For each case, two integers N, M (1 <= N <= 10^5, 1 <= M <= 10^9) in the first line indicates the number of available exchanges and the expected value of final item. Then N lines follow, each line describes an exchange with 3 integers Vi, Ri, Ti (1 <= Ri <= Vi <= 10^9, 1 <= Ti <= 10^9).


Output

For every test case, you should output "Case #k: " first, where k indicates the case number and counts from 1. Then output the minimum time. Output “-1” if no solution can be found.


Sample Input
3
3 10
5 1 3
8 2 5
10 9 2
4 5
2 1 1
3 2 1
4 3 1
8 4 1
5 9
5 1 1
10 4 10
8 1 10
11 6 1
7 3 8


Sample Output
Case #1: -1
Case #2: 4
Case #3: 10


Source
Sichuan State Programming Contest 2011

解题思路:

由于题目数据量很大,点的个数达到了10^5,所以普通的图论最短路方法是要超时的。

所以我想到了BFS,但是这个题目,如果仅仅使用BFS的话还是会超时的。

于是我们可以更进一步优化,变为优先队列+BFS


我们可以先对所有的边按起点(初始金额)排序。

然后优先队列我们用每一条边的交换时间来作为优先条件。


我的代码:

#include #include #include using namespace std; struct edge { long long v; long long u; long long t; }; struct node { long long money; long long t; friend bool operator<(node a,node b) { return a.t>b.t; } }; edge e[100005]; long long num,m; bool cmp(edge a,edge b) { return a.uq; k1.money=1; k1.t=0; q.push(k1); while(!q.empty()) { k2=q.top(); q.pop(); if(k2.money>=m) return k2.t; for(i=left;i<=num;i++) { if(k2.money>=e[i].u&&e[i].v>k2.money) { k1.money=e[i].v; k1.t=k2.t+e[i].t; q.push(k1); } if(k2.money



你可能感兴趣的:(四川省ACM省赛B题,优先队列+BFS Charitable Exchange)