Candies_poj3159_差分约束

来源:互联网 发布:node 编辑:程序博客网 时间:2024/04/30 08:01

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.

Analysis


对于每一对限制条件可以表示为
TbTac
然后就是连边spfa
值得注意的是用queue会TLE然而stack不会。为什么呢?知らない

Code


/*ID:wjp13241PROG:candiesLANG:C++*/#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <stack>#include <queue>#define fo(i,a,b) for (int i=a;i<=b;i++)#define fil(x,t) memset(x,t,sizeof(x))#define STP system("pause")#define min(x,y) x<y?x:y#define max(x,y) x>y?x:y#define ll long long#define INF 0x7f7f7f7f#define E 150001*2+1#define N 30001using namespace std;struct edge{int y;ll w;int next;}e[E];int vis[N],ls[E],cnt[N],maxE=0;ll dis[N];int add(int x,int y,ll w){e[++maxE]=(edge){y,w,ls[x]};ls[x]=maxE;}ll spfa(int st,int ed){    stack<int>q;    q.push(st);    fil(dis,127);    fil(vis,0);    dis[st]=0;    vis[st]=1;    while (!q.empty())    {        int now=q.top();q.pop();        for (int i=ls[now];i;i=e[i].next)            if (e[i].w+dis[now]<dis[e[i].y])            {                dis[e[i].y]=dis[now]+e[i].w;                if (!vis[e[i].y])                {                    vis[e[i].y]=1;                    q.push(e[i].y);                }            }        vis[now]=0;    }    return dis[ed];}int main(){    int n,m;    scanf("%d%d",&n,&m);    fo(i,1,m)    {        int x,y;        ll w;        scanf("%d%d%lld",&x,&y,&w);        add(x,y,w);    }    ll ans=spfa(1,n);    printf("%lld\n",ans);    return 0;}
0 0
原创粉丝点击