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
Hint
32-bit signed integer type is capable of doing all arithmetic.   

差分约束问题

小朋友A认为小朋友B的糖糖不能比他多C个=^=

套用SPFA的模板就好啦~

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
struct node
{
    int u,v,w;
    int next;
}eage[121234];
int head[31234];
int vis[31234];
int dis[31234];
int top;
void Add(int u,int v,int w)
{
    eage[top].v=v;
    eage[top].w=w;
    eage[top].next=head[u];
    head[u]=top++;
}
int n,m;
void SPFA()
{
    int q[31234];
    int t=0;
    int i;
    for(i=0;i<=n;i++)
    {
        dis[i]=INF;
    }
    dis[1]=0;
    q[++t]=1;
    while(t)
    {
        int u=q[t--];
        vis[u]=0;
        for(i=head[u];i!=-1;i=eage[i].next)
        {
            int v=eage[i].v;
            if(dis[v]>dis[u]+eage[i].w)
            {
                dis[v]=dis[u]+eage[i].w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q[++t]=v;
                }
            }
        }
    }
}
int main()
{
    top=0;
    memset(head,-1,sizeof(head));
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&m);
    while(m--)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        Add(u,v,w);
    }
    SPFA();
    printf("%d\n",dis[n]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值