1. 程式人生 > >poj 3159 Candies (差分約束系統裸題)

poj 3159 Candies (差分約束系統裸題)

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;
}