poj3159 Candies
地址:http://poj.org/problem?id=3159
题目:
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 31463 | Accepted: 8782 |
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
Source
1 #include <cstdio>
2 #include <algorithm>
3 #include <cmath>
4
5 using namespace std;
6
7 #define MP make_pair
8 #define PB push_back
9 typedef long long LL;
10 typedef pair<int,int> PII;
11 const double eps=1e-8;
12 const double pi=acos(-1.0);
13 const int K=1e6+7;
14 const int mod=1e9+7;
15
16 struct node
17 {
18 int to,v,next;
19 }edge[K];
20 int tot,head[K];
21 void add(int x,int y,int z)
22 {
23 edge[tot].to=y,edge[tot].v=z,edge[tot].next=head[x];
24 head[x]=tot++;
25 }
26 int vis[K],dis[K],sk[K];
27 void spfa(void)
28 {
29 int cnt=0;
30 sk[cnt++]=1,vis[1]=1,dis[1]=0;
31 while(cnt)
32 {
33 int u=sk[--cnt];
34 vis[u]=0;
35 for(int i=head[u];~i;i=edge[i].next)
36 {
37 int v=edge[i].to,w=edge[i].v;
38 if(dis[v]>dis[u]+w)
39 {
40 dis[v]=dis[u]+w;
41 if(!vis[v])
42 sk[cnt++]=v,vis[v]=1;
43 }
44 }
45 }
46 }
47 int main(void)
48 {
49 int n,m;
50 while(~scanf("%d%d",&n,&m))
51 {
52 tot=0;
53 for(int i=1;i<=n;i++)
54 head[i]=-1,vis[i]=0,dis[i]=0x3f3f3f3f;
55 for(int i=1,u,v,w;i<=m;i++)
56 scanf("%d%d%d",&u,&v,&w),add(u,v,w);
57 spfa();
58 printf("%d\n",dis[n]);
59 }
60 return 0;
61 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。