poj3159——Candies(差分约束+SPFA堆栈)

来源:互联网 发布:地球空心 知乎 编辑:程序博客网 时间:2024/05/16 16:58

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 2
1 2 5
2 1 4
Sample Output

5

一开始没什么思路,题意都不太懂,网上看了下才知道是差分约束,又是个新知识点。
题意是a最多能忍受b比他多c个糖果,也就是b-a<=c,把这个等式转换成图上的边,a,b为两点,c为权值,等式转换成a+c<=b就可以看出这是条a至b的有向边。图建完就可以用求最短路的方法解出1到n最多能给的糖果数。
一般用SPFA,本题没有负权环路,不能用SPFA+QUENE,开小了会WA,开大了就TLE,只能用SPFA+STACK

#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 1000010#define mod 1000000007using namespace std;struct Edge{    int v,w,next;};Edge edge1[MAXN<<1];int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];int q[MAXN];void add(Edge *edge,int *head,int u,int v,int w){    edge[e].v=v;    edge[e].w=w;    edge[e].next=head[u];    head[u]=e;}long long spfa(Edge *edge,int *head){    /*memset(vis,0,sizeof(vis));    for(int i=1; i<=n; ++i)        dis[i]=INF;    dis[u]=0;    queue<int> q;    q.push(u);    while(!q.empty())    {        u=q.front();        q.pop();        vis[u]=1;        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v,w=edge[i].w;            if(w+dis[u]<dis[v])            {                dis[v]=w+dis[u];                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }    return dis[n];*/    int top=0;    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;++i)        dis[i]=INF;    vis[1]=1;    dis[1]=0;    q[top++]=1;    while(top!=0)    {        int u=q[--top];        vis[u]=false;        for(int i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].v,w=edge[i].w;            if(dis[v]>dis[u]+w)            {                dis[v]=dis[u]+w;                if(!vis[v])                {                    vis[v]=1;                    q[top++]=v;                }            }        }    }    return dis[n];}int main(){    while(~scanf("%d%d",&n,&m))    {        e=0;        int u,v,w;        memset(head1,-1,sizeof(head1));        while(m--)        {            scanf("%d%d%d",&u,&v,&w);            //v-u<=w            add(edge1,head1,u,v,w);            //即w+u<=v,权值+起点<=终点            e++;        }        long long ans;        ans=spfa(edge1,head1);        printf("%I64d\n",ans);    }    return 0;}
0 0
原创粉丝点击