1. 程式人生 > >POJ 3159 Candies(最短路 差分約束)

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

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 0x3f3f3f3f3f3f
using 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;
}