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 cin 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.
题目大意:n个孩子分糖果,有m条规则,分别为b的糖果不多于a的糖果w个,要求满足条件后,得到糖果数最多的那一个减最少的那一个得到的数字尽可能大
解题思路:差分约束(第一次听到这个名字),浅浅了解了一下,大概和最短路比较像,下面是摘抄部分
https://www.cnblogs.com/zhangmingcheng/p/3929394.html
https://www.cnblogs.com/murmured/p/5004082.html
B-A<=c
可以化为:B<=A+c
这里可以联想一下最短路中松弛条件。
1 2 3 4 |
|
可以看出松弛完后dis数组中都会满足dis[v]<=dis[u]+w,这就与满足B<=A+c结合起来了。
可见求最短路过程中可以使题目中的条件全部满足。
至于为什么求完最短路后是最大值:我们是根据条件一点点缩减范围的,就像在坐标轴上,根据条件不断向左缩减,最后所处位置必然是最大值。
这样这题就可以解决了,输入abc,也就是a到b有一条有向边,权值为c。
为什么是a到b???
因为:最后要满足B<=A+c,结合到最短路,B是dis[b],A是dis[a],那么c自然就是map[a][b].
有的题让你最后求最小值,这就要与求最长路联系了,可以在不等式两边同时乘-1,使不等号变一下方向。
我的代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <stack>
#define inf 0x3f3f3f3f
using namespace std;
int head[150009];
bool vis[30009];
int dis[30009];
struct node
{
int from,to,w,next;;
}e[150009];
int t;
void addedge(int u,int v,int w)
{
e[t].from=u;
e[t].to=v;
e[t].w=w;
e[t].next=head[u];
head[u]=t++;
}
void spfa()
{
int i;
memset(dis,inf,sizeof(dis));
stack<int> q;
q.push(1);
vis[1]=1;
dis[1]=0;
int u;
while(!q.empty())
{
u= q.top();
q.pop();
vis[u]=0;
for(i=head[u];i!=-1;i=e[i].next)
{
if(dis[e[i].to]>dis[u]+e[i].w)
{
dis[e[i].to]=dis[u]+e[i].w;
if(vis[e[i].to]==0)
{
vis[e[i].to]=1;
q.push(e[i].to);
}
}
}
}
}
int main()
{
int n,m,i,u,v,w;
scanf("%d %d",&n,&m);
t=0;
memset(head,-1,sizeof(head));
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
addedge(u,v,w);
}
memset(vis,0,sizeof(vis));
spfa();
int mx=0;
for(i=2;i<=n;i++)
{
mx=max(mx,dis[i]);
}
printf("%d\n",mx);
return 0;
}
还有一个问题是,这个题用栈可以过用队列不能过,虽然我没试过队列,但是我觉得用栈和队列对spfa代码实现起来一样,为何不用栈呢?