Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
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 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 20), which stands for the number of test cases you need to solve.
For each case, two integers 1≤Ti≤109).
Output
For every test case, you should output Case #k: first, where −1 if no solution can be found.
Sample input and output
| Sample Input | Sample Output |
|---|---|
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: 4 Case #3: 10 |
Source
一开始只有1元的东西,让你求出交换到价值至少为m的最少时间代价。
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 //#include<bits/stdc++.h> 8 #include<iostream> 9 #include<cstring> 10 #include<cstdio> 11 #include<map> 12 #include<algorithm> 13 #include<queue> 14 #include<cmath> 15 #define ll long long 16 #define PI acos(-1.0) 17 #define mod 1000000007 18 using namespace std; 19 struct node 20 { 21 int to; 22 ll we; 23 int pre; 24 friend bool operator < (node a, node b) 25 { 26 return a.we > b.we; 27 } 28 }N[100005]; 29 priority_queue<node> pq; 30 int t; 31 int n,m; 32 bool cmp(struct node aa,struct node bb) 33 { 34 return aa.pre<bb.pre; 35 } 36 ll bfs() 37 { 38 struct node exm,now; 39 while(!pq.empty()) pq.pop(); 40 exm.pre=0; 41 exm.to=1; 42 exm.we=0; 43 pq.push(exm); 44 int l=1; 45 int i; 46 while(!pq.empty()) 47 { 48 now=pq.top(); 49 pq.pop(); 50 if(now.to>=m) 51 { 52 return now.we; 53 } 54 for(i=l; i<=n; i++) 55 { 56 if(now.to>=N[i].pre&&now.to<N[i].to)//遍历可以用的边 57 { 58 exm.pre=now.to; 59 exm.to=N[i].to; 60 exm.we=now.we+N[i].we; 61 pq.push(exm); 62 l=i;//爆内存点 63 } 64 if(now.to<N[i].pre)//T点 65 break; 66 } 67 } 68 return -1; 69 } 70 int main() 71 { 72 scanf("%d",&t); 73 { 74 for(int i=1; i<=t; i++) 75 { 76 scanf("%d %d",&n,&m); 77 for(int j=1; j<=n; j++) 78 scanf("%d %d %lld",&N[j].to,&N[j].pre,&N[j].we); 79 sort(N+1,N+1+n,cmp); 80 ll ans=bfs(); 81 printf("Case #%d: %lld\n",i,ans); 82 } 83 } 84 return 0; 85 }