POJ 3159 Candies(最短路 差分约束)

来源:互联网 发布:大数据对企业的意义 编辑:程序博客网 时间:2024/06/06 17:30

Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 31848 Accepted: 8896
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

32-bit signed integer type is capable of doing all arithmetic.
Source

POJ Monthly–2006.12.31, Sempr

用到的知识:

  • 邻接表
  • spfa
  • 差分约束
  • 堆栈优化

今天听学长讲课讲到了差分约束,一直不明白为什么是最短路而不是最长路。。。。以此题为例,终于理解了。
题意:有N个小朋友,给他们发糖,给M个关系ai bi ci
表示bi-ai<=ci ,问1和n间的最大差距;
假设1到N间有k条路,即有k个不等式 dn-d1<=wk 差分约束就是让这k个不等式都成立,所以只能取wk的最小值才能让k个都成立,是最短路。不由得感叹自己真是个蠢货。。。。

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <queue>#define maxn 30010#define inf 0x3f3f3f3f3f3fusing namespace std;typedef long long ll;int m,n;int cnt=0;ll dis[maxn];struct Node{    int fr;    int to;    ll l;    int next;}e[maxn*5];int head[maxn*5];void add(int a,int  b,ll c){    e[cnt].fr=a;    e[cnt].to=b;    e[cnt].l=c;    e[cnt].next=head[a];    head[a]=cnt++;}int q[maxn];void spfa(int st){    int vis[maxn];    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)    {        dis[i]=inf;    }    vis[st]=1;    dis[st]=0;    int top=0;    q[top++]=st;    while(top)    {        int cur=q[--top];        vis[cur]=0;        for(int i=head[cur];i!=-1;i=e[i].next)        {            int to=e[i].to;            if(dis[to]>dis[cur]+e[i].l)            {                dis[to]=dis[cur]+e[i].l;                if(!vis[to])                {                    q[top++]=to;                    vis[to]=1;                }            }        }    }}void init(){    cnt=0;    memset(head,-1,sizeof(head));}int main(){    int a,b;    ll l;    scanf("%d%d",&n,&m);    init();    for(int i=0;i<m;i++)    {        scanf("%d%d%I64d",&a,&b,&l);        add(a,b,l);    }    spfa(1);    printf("%I64d\n",dis[n]);    return 0;}
原创粉丝点击