差分约束系统
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
题意,对于第x第孩子和第y个孩子,y孩子获得的糖果最多比x孩子多w个问如何使abs(1-n)最大
这是典型的差分约束系统,满足f(y)-f(x) <= w;
将f替换成dis,则dis(y) <= dis(x) + w; 眼熟的式子,我们在松弛的时候
if(dis[y] > dis[x] + w)
{
dis[y]= dis[x] + w;
}
以x,y为顶点,w为x->y的边的权
当我们求得从1到n的最短路的时候就已经满足了所有f(y)-f(x)<=w;
现在考虑为什么能够使得abs(1-n)最大
看下面这个简单的图:
有道云笔记和csdn的语法有些不同啊,图没画出来…..1 –> |4| 2表示1->2有一条边,权为4
graph LR
1 --> |4| 2
2 --> |4| 4
1 --> |1| 3
3 --> |1| 2
1 --> |3| 4
从1到4有三条路径,每条边对应一个不等式,先思考1->2两条路径,节点2最多比节点1多2个糖果,否则的话,最短的那条路径不满足要求,而2又是最大的abs(1->2),路径已经满载了,同样,从1->4有三条路径,首先一定要选择最短的路径,这样才能满足不等式,最短路径满载,abs(1->4)最大,所以差分约束可以使用最短路求解,本题比较卡时间,使用dijkstra算法的heap优化,还有就是,vector G[maxn]这种实现在节点很多时似乎很慢,我先使用这种方法超时了一次
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300010;
const int maxm = 150005;
const int INF = 0x3f3f3f3f;
int n,m;
struct Edge
{
int from,to,w,next;
};
struct Pair
{
int v,distance;
bool operator < (const Pair &t) const
{
return distance > t.distance;
}
};
int pre[maxn];
Edge e[maxm];
priority_queue<Pair> pq;
bool vis[maxn];
int dis[maxn];
int main()
{
scanf("%d%d" ,&n,&m);
memset(pre,-1,sizeof(pre));
int from,to,w;
for(int i=1; i<=m; i++)
{
scanf("%d%d%d" ,&from,&to,&w);
e[i].from=from;e[i].to=to;e[i].w=w;
e[i].next=pre[from]; pre[from]=i;
}
while(!pq.empty()) pq.pop();
memset(dis,INF,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1]=0;
pq.push( (Pair){1,0} );
while(!pq.empty())
{
Pair t = pq.top();
pq.pop();
if(t.distance != dis[t.v]) continue;
vis[t.v]=true;
for(int i=pre[t.v]; i!=-1; i=e[i].next)
{
if(vis[e[i].to]) continue;
if(dis[e[i].to] > dis[t.v] + e[i].w)
{
dis[e[i].to] = dis[t.v] + e[i].w;
pq.push( (Pair){e[i].to,dis[e[i].to]} );
}
}
}
printf("%d\n" ,dis[n]);
return 0;
}
本文探讨了一个关于差分约束系统的具体问题,通过构建一个模型来分配糖果,目标是最小化不同孩子之间的糖果数量差距,同时确保没有孩子感到不满。文章详细介绍了如何利用最短路径算法解决这个问题,并给出了具体的代码实现。
593

被折叠的 条评论
为什么被折叠?



