Candies POJ

来源:互联网 发布:数据来源有哪些 编辑:程序博客网 时间:2024/05/17 01:13


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 integersN 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 throughN. snoopy and flymouse were always numbered 1 and N. Then followM lines each holding three integers A, B and c in order, meaning that kidA 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 21 2 52 1 4
Sample Output
5
Hint
32-bit signed integer type is capable of doing all arithmetic.

附上第一篇推荐博客:(差束约分)

http://www.cnblogs.com/scau20110726/archive/2012/11/29/2794756.html

代码:


/*********************************************PS:先看博客(差束约分)        博客中的精华:                 对于这种有一个未知数定死的差分约束系统,还有一个有趣的性质,                 那就是通过最短路径算法求出来的一组解当中,所有未知数都达到最大值。                 那么如果在一个未知数定死的情况下,要求其它所有未知数的最小值怎么办?只要反过来求最长路径就可以了。最长路径中的三角不等式与最短路径中相反:                 d(v) >= d(u) + w(u, v)                 也就是 d(v) - d(u) >= w(u, v)关于差束约分的一点总结:(以本题为例)            题目告诉了我们一个已知条件:b-a<=c;转化成 b<=a+c;对于这种模式,            根据刚刚博客的中提到一点性质通过最短路算法求出的是最大值(对应题目的需求)            所以直接套用最短路方法来做即可。            强调一遍性质:                        a<=b+c:最短路可以求得最大值                        a>=b+c;最长路可以求得最小值。            性质很重要! 性质很重要!性质很重要!!!    最后一点:spfa + stack 优于 spfa + queue**********************************************/#include <cstdio>#include<cstring>#include <cstdlib>#include <algorithm>#include <cmath>#include <string>#include <fstream>#include <map>#include<vector>#include <queue>#include <stack>#include <iostream>#define fo(i,a,b) for (int i=a;i<b;i++)using namespace std;const int maxn =1e6+5;const int INF=0x3f3f3f3f;typedef long long ll;struct node{     int to;//终点     int next;//下一条边的位置     int w;}e[maxn];int cnt ;bool vis[maxn];int head[maxn],dist[maxn];void add(int a,int b,int c){     e[cnt].to=b;     e[cnt].w=c;     e[cnt].next=head[a];     head[a]=cnt++;}bool yes ;//pan fu huanint n,m;int num[maxn];void spfa(int st){     fill(num,num+n+1,0);     fill(vis,vis+n+1,0);     fill(dist,dist+n+1,INF);     int i,j,k;        dist[st]=0;        vis[st]=1;        num[st]++;//        queue<int>q;        stack<int>q;        q.push(st);        while (!q.empty())        {             int u=q.top();             q.pop();             vis[u]=0;             for (i=head[u];i!=-1;i=e[i].next)             {                 int qq=e[i].to;                     if (dist[qq]>dist[u]+e[i].w)                     {                         dist[qq]=dist[u]+e[i].w;                         if (!vis[qq])                         {                             num[qq]++;                             vis[qq]=1;                                q.push(qq);                             if(num[qq]>n)                             {                                 yes=1;                                 return ;                             }                         }                     }             }        }}int main (){//     std::ios::sync_with_stdio(0);//     cin.tie(0);//     cin>>n>>m;        scanf("%d%d",&n,&m);        int x,y,z;        fill(head,head+n+1,-1);     cnt=0;     yes=0;     fo (i,0,m)     {         scanf("%d%d%d",&x,&y,&z);//         cin>>x>>y>>z;         add(x,y,z);     }     spfa(1);//     fo(i,1,n+1)//     cout<<dist[i]<<" ";//     cout<<endl;     cout<<dist[n]<<endl;    return 0;}


原创粉丝点击