POJ 3159-Candies(差分约束)

版权声明:转载请标明出处哦 https://blog.csdn.net/PleasantlY1/article/details/82822350

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;
}

猜你喜欢

转载自blog.csdn.net/PleasantlY1/article/details/82822350
今日推荐