Candies
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
差分约束+SPFA
给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数, 意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c 。 最后求n 比 1 最多多多少糖果。
【解题思路】
这是一题典型的差分约束题。不妨将糖果数当作距离,把相差的最大糖果数看成有向边AB的权值, 我们得到 dis[B]-dis[A]<=w(A,B)。看到这里,我们联想到求最短路时的松弛技术, 即if(dis[B]>dis[A]+w(A,B), dis[B]=dis[A]+w(A,B)。 即是满足题中的条件dis[B]-dis[A]<=w(A,B),由于要使dis[B] 最大, 所以这题可以转化为最短路来求。 这题如果用SPFA 算法的话,则需要注意不能用spfa+queue 来求,会TLE ,而是用 spfa + stack
通过这篇博客理解的,我是用的C++的栈 https://www.cnblogs.com/kuangbin/archive/2012/08/17/2644685.html
周公子提醒之后对这道题有了更深的理解
如果你单纯理解题意的话你要求最大差距那么答案应该是40
但是如果编号为8的人为40,编号为1的人为0,
那么按照下面的那条链推回去编号为5的人最小也拥有25块糖果
但是编号为5的人所拥有的糖果数最多只能比编号为1的人多5,
而25-0>5,而且是远大于,到这儿你应该就能理解这为什么是一道最短路的题了吧,并且叫“差分约束”吧
因为只有满足短的限制条件,长的才一定满足,这就是差分约束
#include <iostream>
#include <string.h>
#include <stack>
#include <cstdio>
using namespace std;
#define N 30010
#define INF 0x3f3f3f
int dis[N];
int n,k,head[N];
int vis[N];
struct edge
{
int v,w;
int next;
}e[150010];//注意题目所给边的条数是最多是150000条
void add(int u,int v,int w)//链式前向星
{
e[k].v=v;
e[k].w=w;
e[k].next=head[u];
head[u]=k++;
}
void spfa()
{
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
vis[1]=1;
dis[1]=0;
stack<int>s;
s.push(1);
while(!s.empty())
{
int k=s.top();
s.pop();
vis[k]=0;
for(int i=head[k];i!=-1;i=e[i].next)
{
int v=e[i].v;
int w=e[i].w;
if(dis[k]+w<dis[v])//差分约束其实也就是最短路
{
dis[v]=dis[k]+w;
if(!vis[v])
{
vis[v]=1;
s.push(v);
}
}
}
}
return ;
}
int main()
{
int m;
memset(head,-1,sizeof(head));
k=0;//注意初始化
scanf("%d %d",&n,&m);
for(int i=1; i<=m; i++)
{
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
spfa();
printf("%d\n",dis[n]);
return 0;
}