您的位置:首页 > 其它

POJ 3159 Candies

2018-01-20 15:13 197 查看
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组约束条件。 每组条件有三个数字A,B,C,表示:B的糖果数量比A的糖果数量多,并且最多多C个(即B-A<=C)。求第n个人得到的糖果,最多比第一个人得到的糖果多几个。

差分约束:如若一个系统由n个变量和m个不等式组成,并且这m个不等式对应的系数矩阵中每一行有且仅有一个1和-1,其它的都为0,这样的系统称为差分约束( difference constraints )系统。观察 B[i] - A[j] <= C[k], 将这个不等式稍稍变形,将x[j]移到不等式右边,则有B[i] <= A[j] + C[k],然后我们令C[k] = w(j, i),再将不等式中的i和j变量替换掉,i = v, j = u,将B数组的名字改成d(以上都是等价变换,不会改变原有不等式的性质),则原先的不等式变成了以下形式:d[u]
+ w(u, v) >= d[v]。这时候联想到SPFA中的一个松弛操作。

这道题应该注意的一点就是用队列会超时,只能用栈实现SPFA。
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 30005;
int dis[maxn];
int book[maxn];
int head[maxn];
struct node{
int e, w;
int next;
void Node(int x, int y, int z) {
e = x; w = y; next = z;
}
}e[3*maxn];
void SPFA_sta() {
dis[1] = 0;
book[1] = 1;
int pre, nex;
stack<int> sta;
sta.push(1);
while(!sta.empty()) {
pre = sta.top();
sta.pop();
book[pre] = 0;
for(int i = head[pre]; i != -1; i = e[i].next) {
nex = e[i].e;
if(dis[nex] > dis[pre] + e[i].w) {
dis[nex] = dis[pre]+e[i].w;
if(!book[nex]) {
book[nex] = 1;
sta.push(nex);
}
}
}
}
}
void SPFA_que() {//超时
dis[1] = 0;
book[1] = 1;
int pre, nex;
queue<int> q;
q.push(1);
while(!q.empty()) {
pre = q.front();
q.pop();
book[pre] = 0;
for(int i = head[pre]; i != -1; i = e[i].next) {
nex = e[i].e;
if(dis[nex] > dis[pre] + e[i].w) {
dis[nex] = dis[pre] + e[i].w;
if(!book[nex]) {
book[nex] = 1;
q.push(nex);
}
}
}
}
}
int main() {
int n, m;
int t1, t2, t3;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) {
head[i] = -1;
}
for(int i = 0; i < m; i++) {
scanf("%d %d %d", &t1, &t2, &t3);
e[i].Node(t2, t3, head[t1]);
head[t1] = i;
}
for(int i = 1; i <= n; i++) {
dis[i] = inf;
book[i] = 0;
}
SPFA_sta();
printf("%d\n", dis
);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: