poj 3195 Candies 差分约束

来源:互联网 发布:9.9元淘宝斜挎包女包 编辑:程序博客网 时间:2024/05/17 10:43
题意:
有一批糖果要由班长flymouse分发给班里的小朋友,给出m对数据u,v,w,表示第v个小朋友最多比第u个小朋友多w个,但flymouse与snoopy不和,所以在满足上述约束的条件下,flymouse要尽量比snoopy多。snoopy标号1,flymouse标号n
思路:

以dis[1]=0,dis[i]表示相对dis[1]最多的糖果数。下面构造差分约束系统,对每一组数据u,v,w都是一条边(u,v,w),由题意约束条件为 d[v]-d[u]<=w,即 d[v]<=w+d[u] ,这与求最短路中的不等式 d[v]>w+d[u]被满足后则有 d[v]<=w+d[u] 相对应,所以求最短路满足约束条件且为最大值;最后用spfa(由于数据原因要用栈来实现)算法来求各个点的最短路程。dis[n]就是flymouse比snoopy最多的数目

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 followM lines each holding three integers AB 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 21 2 52 1 4

Sample Output

5

Hint

32-bit signed integer type is capable of doing all arithmetic.

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define maxn 299909#define INF 0x3f3f3f3fint dis[maxn],head[maxn],vis[maxn];int q[maxn];int n,m,cnt,st;struct node{    int v,w,next;} edge[maxn];void add(int u,int v,int w){    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;}void SPFA(){    int top=0;    memset(vis,0,sizeof(vis));    memset(dis,INF,sizeof(dis));    dis[st]=0;    vis[st]=1;    q[top++]=st;    while(top!=0)    {        int u=q[--top];        vis[u]=0;        for(int i=head[u]; i!=-1; i=edge[i].next)        {            if(dis[edge[i].v]>dis[u]+edge[i].w)            {                dis[edge[i].v]=dis[u]+edge[i].w;                if(!vis[edge[i].v])                {                    vis[edge[i].v]=1;                    q[top++]=edge[i].v;                }            }        }    }}int main(){    int u,v,w;    while(~scanf("%d %d",&n,&m))    {        memset(head,-1,sizeof(head));        for(int i=0; i<m; i++)        {            scanf("%d %d %d",&u,&v,&w);            add(u,v,w);        }        st=1;        SPFA();        printf("%d\n",dis[n]);    }    return 0;}

今天第一次学差分约束,感觉这道题目很简单,但是还是看得别人博客

0 0