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

分析：

就是差分约束啊。。。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include<cstdio>//POJ 3159 BY:uncle-lu #include<algorithm> #include<cstring> #include<queue> using namespace std; struct node{ int v,next,val; }edge[150010]; int head[30010]; int dis[30010]; int n,m,cnt; int add_edge(int x,int y,int val) { edge[++cnt].v=y; edge[cnt].next=head[x]; edge[cnt].val=val; head[x]=cnt; return 0; } priority_queue<int>qu; bool visit[30010]; int spfa() { memset(dis,0x3f3f,sizeof(dis)); dis[1]=1; visit[1]=true; qu.push(1); while(!qu.empty()) { int k=qu.top(); qu.pop(); for(int i=head[k];~i;i=edge[i].next) { if(dis[edge[i].v]>dis[k]+edge[i].val) { dis[edge[i].v]=dis[k]+edge[i].val; if(!visit[edge[i].v]) { visit[edge[i].v]=true; qu.push(edge[i].v); } } } visit[k]=false; } return dis[n]-dis[1]; } int main() { memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); int x,y,val; for(int i=1;i<=m;++i) { scanf("%d%d%d",&x,&y,&val); add_edge(x,y,val); } printf("%d",spfa()); return 0; } |