POJ 3159-Candies(差分约束)

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值