Have you ever heard a star charity show called Charitable Exchange? In this show, a famous star starts with a small item which values 1yuan. 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.
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≤105, 1≤M≤109) 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≤109, 1≤Ti≤109).
For every test case, you should output Case #k:
first, where kindicates
the case number and counts from 1.
Then output the minimum time. Output −1 if
no solution can be found.
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
Case #1: -1 Case #2: 4Case #3: 10
题目大意:T组数据,你本来有一块钱,可以用R块钱去换值V块钱的东西,但是得花费T个时间,最后问达到m的钱需要花费多长时间(当然是最少)。
刚开始一想,这提莫就是个最短路啊。连接头尾加时间就好了。
然后想了一下spfa。 可能我每次想到最短路的时候就想到这个东西吧。
后面看了题解才想起优先队列。差不多其实跟dij的思想如出一辙吧。
不过存数据的时候先排个序。
#include <bits/stdc++.h> //#include <ext/pb_ds/tree_policy.hpp> //#include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; using namespace std; #define pi acos(-1) #define endl '\n' #define me(x) memset(x,0,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define close() ios::sync_with_stdio(0); const int INF=0x3f3f3f3f; //const LL LINF=0x3f3f3f3f3f3f3f3fLL; const int dx[]={-1,0,1,0,-1,-1,1,1}; const int dy[]={0,1,0,-1,1,-1,1,-1}; const int maxx=1e5+100; const double EPS=1e-7; const int MOD=1000000007; #define mod(x) ((x)%MOD); template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} //typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree; /*lch[root] = build(L1,p-1,L2+1,L2+cnt); rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/ /*lch[root] = build(L1,p-1,L2,L2+cnt-1); rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/ long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);} typedef long long ll; const int maxn=1e5+100; int len; int n; ll m; struct Edge { ll u,v,time; bool friend operator<(Edge a,Edge b) { return a.u<b.u; } } edge[maxn]; struct node { ll money; ll time; bool friend operator<(node a,node b) { return a.time>b.time; } }; ll bfs() { priority_queue<node>q; int i=1; node ed; q.push(node{1,0}); while(!q.empty()) { ed=q.top(); q.pop(); if(ed.money>=m) return ed.time; for(;i<=len;i++) { if(ed.money<edge[i].u) break; if(ed.money>=edge[i].u&&edge[i].v>ed.money) q.push(node{edge[i].v,ed.time+edge[i].time}); } } return -1; } int main() { int t; cin>>t; for(int cas=1;cas<=t;cas++) { cin>>n>>m; len=1; for(int i=1;i<=n;i++) { ll a,b,c; cin>>a>>b>>c; if(a==b) continue; edge[len].u=b; edge[len].v=a; edge[len++].time=c; } sort(edge+1,edge+len+1); printf("Case #%d: %lld\n",cas,bfs()); } }