首页 > 代码库 > POJ - 3159 Candies
POJ - 3159 Candies
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<algorithm> 6 #define INF 0x3f3f3f3f3f3f3f3f 7 #define maxn 1000010 8 9 using namespace std; 10 11 struct EDGE 12 { 13 int u,v,w,next; 14 }edge[maxn]; 15 16 struct node 17 { 18 int u; 19 int d; 20 node(int u,int d):u(u),d(d) {} 21 bool operator <(const node& p)const 22 { 23 return d>p.d; 24 } 25 }; 26 27 long long dis[maxn]; 28 int a[maxn][3],head[maxn]; 29 bool vis[maxn]; 30 int cnt,n,m; 31 32 void Init() 33 { 34 cnt=0; 35 for(int i=1;i<=n;i++) 36 { 37 vis[i]=false; 38 dis[i]=INF; 39 head[i]=-1; 40 } 41 } 42 43 void addedge(int u,int v,int w) 44 { 45 edge[cnt].u=u; 46 edge[cnt].v=v; 47 edge[cnt].w=w; 48 edge[cnt].next=head[u]; 49 head[u]=cnt++; 50 } 51 52 long long dj(int s) 53 { 54 priority_queue<node> p; 55 dis[s]=0; 56 p.push(node(1,0)); 57 while(!p.empty()) 58 { 59 node p1=p.top(); 60 p.pop(); 61 int u=p1.u; 62 if(vis[u]) 63 continue; 64 vis[u]=true; 65 for(int i=head[u];~i;i=edge[i].next) 66 { 67 int v=edge[i].v; 68 int w=edge[i].w; 69 //cout<<"bian"<<i<<" "<<w<<endl; 70 if(!vis[v]&&dis[v]>dis[u]+w) 71 { 72 dis[v]=dis[u]+w; 73 p.push(node(v,dis[v])); 74 //cout<<v<<" "<<dis[v]<<endl; 75 } 76 } 77 } 78 long long ans=0; 79 ans=dis[n]; 80 return ans; 81 } 82 83 int main() 84 { 85 while(cin>>n>>m) 86 { 87 Init(); 88 for(int i=0;i<m;i++) 89 { 90 scanf("%d%d%d",&a[i][0],&a[i][1],&a[i][2]); 91 addedge(a[i][0],a[i][1],a[i][2]); 92 } 93 long long ans=dj(1); 94 /*Init(); 95 for(int i=0;i<m;i++) 96 addedge(a[i][1],a[i][0],a[i][2]); 97 ans+=dj(1);*/ 98 cout<<ans<<endl; 99 } 100 101 102 103 return 0; 104 }
POJ - 3159 Candies