向前走莫回头❤

【POJ 3159】Candies(差分约束+SPFA)

Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 29796   Accepted: 8258

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

Sample Output

5

Hint

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

【差分约束的裸题,直接跑最短路,不过本题时限较小,所以要将队列换成栈才不会T】
【差分约束,也是线性规划里的一种,是用来求不等式组的变量的最大取值。
比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图:                                                                                                               

由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较k1+k2和k3的大小,求出最小的就是c-a的最大值了 
【通过这样的解释,可以发现不等式的求解与最短路的求解有一定关系,即通过求最短路可以求出不等式的最大值】 
【然而有的时候,不一定是要求最大值,当求最小值时,求最大路径。】
【注意:每次求解时,必须要将不等式化成a-b>=k或a-b<=k的情况,当出现用'>'或'<'连接时,改成a-b<=k-1或a-b>=k+1】
【若权值有负数,就要先判有没有负环,有负环的话就不存在结果】 


#include<stack>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
stack<int>que;
int a[150010],next[150010],p[150010],val[150010],tot;
int dis[150010];
int n,m;
bool b[150010];
inline void add(int x,int y,int z)
{
     tot++; a[tot]=y; next[tot]=p[x]; p[x]=tot;
     val[tot]=z;
     return;
}

inline void spfa(int s,int e)
 {
     memset(dis,127/3,sizeof(dis));
     memset(b,true,sizeof(b));
     que.push(s);  b[s]=false;
     dis[s]=0;
     while (!que.empty())
       {
         int u,v;
         u=que.top(); que.pop();
         v=p[u]; b[u]=true;
         while (v)
           { 
            if (dis[a[v]]>dis[u]+val[v])
               {
                dis[a[v]]=dis[u]+val[v];
                if(b[a[v]])
                   { 
                     b[a[v]]=false;
                     que.push(a[v]);
                     }
              }
           v=next[v];
          }
      }
   printf("%d\n",dis[e]);
   return;
}

int main()
 {
   int i;
   scanf("%d%d",&n,&m);
   for (i=1;i<=m;++i)
    {
      int x,y,z;
      scanf("%d%d%d",&x,&y,&z);
      add(x,y,z);
    }
  spfa(1,n);
  return 0;
}  


posted @ 2016-11-10 21:28  lris0-0  阅读(85)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m