POJ 3159 Candies(差分规划+SPFA)

来源:互联网 发布:美国情景喜剧排行知乎 编辑:程序博客网 时间:2024/06/08 13:25

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 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 21 2 52 1 4

Sample Output

5
题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c  三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数小于等于c 。最后求n 比 1 最多多多少糖果。思路:因为最后求的是n比1最多多多少。所以可以大胆的想dis[B]-dis[A]小于等于C所以dis[B]小于等于dis[A]+c因为要求最大。所以dis[B]=dis[A]+c那么这就是一道纯粹的SPFA的模板题了。代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,m;const int mx=1<<29,V=30010,E=150010;int head[V],visited[V],dis[V];struct node{    int v,w,next;}nodes[E];int main(){    while(~scanf("%d%d",&n,&m)){        for(int i=1;i<=n;i++){            head[i]=-1;            visited[i]=0;            dis[i]=mx;        }        int a,b,c;        for(int i=1;i<=m;i++){            scanf("%d%d%d",&a,&b,&c);            nodes[i].v=b;            nodes[i].w=c;            nodes[i].next=head[a];            head[a]=i;        }        int start=1;        int starck[V];        visited[start]=1;        dis[1]=0;        int index=0;        starck[index++]=start;        while(index>0){            int tem=starck[--index];            int cur=head[tem];            while(cur!=-1){                if(nodes[cur].w+dis[tem]<dis[nodes[cur].v]){                    dis[nodes[cur].v]=nodes[cur].w+dis[tem];                    if(!visited[nodes[cur].v]){                        visited[nodes[cur].v]=1;                        starck[index++]=nodes[cur].v;                    }                }                cur=nodes[cur].next;            }        }        printf("%d\n",dis[n]);    }    return 0;}
1 0
原创粉丝点击