poj 3159 Candies (差分约束系统裸题)

来源:互联网 发布:mysql被攻击 编辑:程序博客网 时间:2024/05/22 01:51

Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 31698 Accepted: 8837
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 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
题目大意
从外面偷的题意给n个人发糖果,给出m组限制,每组限制包含A,B,c 三个数, 且满足B拥有的糖果比A多不超过 c个 。 求n号人最多比1号人多几个糖果。
此题裸得连不等式都给了,直接建图+spfa跑一遍。由于有:

x[B] - x[A] >= c 

变形为

x[B] >= x[A] + c

且该式与spfa的松弛操作相似,所以经过一系列非常神奇的操作(……戳链接看吧)就能得出跑一遍最短路即可。
代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <stack>using namespace std;struct Edge {    int v, next, w;}edge[300005];int n, m, num = 0, head[300005], vis[550005], dis[550005];void add(int u, int v, int w) {    num ++;    edge[num].v = v;    edge[num].w = w;    edge[num].next = head[u];    head[u] = num;}stack <int> s;void spfa() {    memset(dis, 127, sizeof(dis));    memset(vis, 0, sizeof(vis));    vis[1] = 1;    dis[1] = 0;    s.push(1);    while(! s.empty()) {        int u = s.top();        s.pop();        vis[u] = false;        for(int i = head[u]; i; i = edge[i].next) {            int v = edge[i].v;            if(dis[v] > dis[u] + edge[i].w) {                dis[v] = dis[u] + edge[i].w;                if(! vis[v]) {                    vis[v] = 1;                    s.push(v);                }            }        }    }}int main() {    scanf("%d %d", &n, &m);    for(int i = 1; i <= m; i ++) {        int a, b, c;        scanf("%d %d %d", &a, &b, &c);        add(a, b, c);    }    spfa();    printf("%d", dis[n]);    return 0;}