POJ 3159-Candies(差分約束)

大白QQly成長日記發表於2018-09-23

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

題目大意:給n個人分糖,然後給出幾組abc,要求第a個人要求第b個人的糖不能超過自己c個。求出第n與第1個人的糖差值最大。

思路:典型的差分約束系統,不用抽象出關系式,也不用判負環,題目直接給出系統條件,然後用最短路改變一下鬆弛條件即可。

程式碼如下:

#include<set>
#include<map>
#include<list>
#include<deque>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<stdlib.h>
#include<string.h>
//#include<ext/rope>
#include<iostream>
#include<algorithm>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define per(i,a,b) for(int i=a;i<=b;++i)
#define LL long long 
#define swap(a,b) {int t=a;a=b;b=t} 
using namespace std;
//using namespace __gnu_cxx;
#define N 200010
LL d[N];
int n,m;
int head[N];
int vis[N];
struct node{
	int w;
	int v;
	int next;
}p[N];
void add(int u,int v,int w,int k)
{
    p[k].v=v;
    p[k].w=w;//權 
    p[k].next=head[u];//出發點 
    head[u]=k;
}
void spfa_stack()
{
    stack<int>q;
    memset(d,INF,sizeof(d));
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    d[1]=0;
    vis[1]=1;
    q.push(1);
    while(!q.empty())
    {
        int u=q.top();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=p[i].next)
        {
            int v=p[i].v;
            int w=p[i].w;
            if(d[u]+w<d[v])
            {
                d[v]=d[u]+w;
                if(vis[v]==0)
                {
                	vis[v]=1;
                	q.push(v);
				}
            }
        }
    }
}
int main()
{
    int a,b,c;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    	memset(head,-1,sizeof(head));
        per(i,0,m-1)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c,i);
        }
        spfa_stack();
        printf("%lld\n",d[n]);
    }
    return 0;
}

 

相關文章