标签:
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Time Limit: 1500MS | Memory Limit: 131072K |
Description
During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.
snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?
Input
The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and c in order, meaning that kid A believed that kid B should never get over c candies more than he did.
Output
Output one line with only the largest difference desired. The difference is guaranteed to be finite.
Sample Input
2 2 1 2 5 2 1 4
Sample Output
5
Hint
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <stack> 6 #include <cstdlib> 7 using namespace std; 8 #define REP(A,X) for(int A=0;A<X;A++) 9 #define MAXE 200010 10 #define MAXP 30010 11 #define INF 0x7fffffff 12 #define MP(A,B) make_pair(A,B) 13 typedef pair<int,int> PII ; 14 struct node{ 15 int v,d,next; 16 }edge[MAXE]; 17 int e=0; 18 int head[MAXP]; 19 void init(){ 20 e=0; 21 REP(i,MAXP)head[i]=-1; 22 } 23 void add_edge(int u,int v,int d) 24 { 25 edge[e].v=v; 26 edge[e].d=d; 27 edge[e].next=head[u]; 28 head[u]=e; 29 e++; 30 } 31 int vis[MAXP],dis[MAXP]; 32 void spfa() 33 { 34 REP(i,MAXP)vis[i]=0; 35 REP(i,MAXP)dis[i]=i==1?0:INF; 36 //queue<int>q; 37 stack<int>q; 38 q.push(1); 39 vis[1]=1; 40 while(!q.empty()){ 41 //int x=q.front(); 42 int x=q.top(); 43 q.pop(); 44 for(int t=head[x];t!=-1;t=edge[t].next) 45 { 46 int y=edge[t].v; 47 int d=edge[t].d; 48 if(dis[y]>dis[x]+d){ 49 dis[y]=dis[x]+d; 50 if(!vis[y]){ 51 q.push(y); 52 vis[y]=1; 53 } 54 } 55 } 56 vis[x]=0; 57 } 58 } 59 void dijkstra(int s) 60 { 61 REP(i,MAXP)vis[i]=0; 62 REP(i,MAXP)dis[i]=i==s?0:INF; 63 priority_queue<PII,vector<PII>,greater<PII> > q; 64 q.push(MP(dis[s],s)); 65 while(!q.empty()) 66 { 67 PII p=q.top(); 68 q.pop(); 69 int x=p.second; 70 if(vis[x])continue; 71 vis[x]=1; 72 for(int t=head[x];t!=-1;t=edge[t].next) 73 { 74 int y=edge[t].v; 75 int d=edge[t].d; 76 if(!vis[y]&&dis[y]>dis[x]+d) 77 { 78 dis[y]=dis[x]+d; 79 q.push(MP(dis[y],y)); 80 } 81 } 82 } 83 84 } 85 86 int main() 87 { 88 int m,n; 89 while(scanf("%d%d",&n,&m)!=EOF){ 90 int u,v,w; 91 init(); 92 REP(i,m){ 93 scanf("%d%d%d",&u,&v,&w); 94 add_edge(u,v,w); 95 } 96 //dijkstra(1); 97 spfa(); 98 printf("%d\n",dis[n]); 99 } 100 return 0; 101 }
标签:
原文地址:http://www.cnblogs.com/fraud/p/4304517.html