POJ-3159.Candies.(差分约束 + Spfa)

Candies

Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 40407   Accepted: 11367

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 AB 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

Hint

32-bit signed integer type is capable of doing all arithmetic.

Source

 
本题大意:n个孩子分糖果,给出m个a,b,c意为b比a分的糖果不能多于c个,即t[ b ] - t[ a ] <= c。很明显是差分约束了,可以转为单源最短路进行求解。
我们可以根据上面的约束方程推出松弛方程为if(dist[b] > dist[a] + edge[i].w) {dist[b] = dist[a] + edge[i].w;}。
 
这题应该是数据问题吧,至今不太明白为什么queue会TLE,而栈可以过...
 
参考代码:
 #include <cstdio>
#include <cstring>
#include <stack>
using namespace std; const int maxn = , maxe = , INF = 0x3f3f3f3f;
struct node {
int to, w, next;
}edge[maxe];
int num, head[maxn], dist[maxn];
bool vis[maxn]; void addedge(int u, int v, int cost) {
edge[num].to = v;
edge[num].w = cost;
edge[num].next = head[u];
head[u] = num ++;
} void spfa(int start, int n) {
stack <int> Q;
for(int v = ; v <= n; v ++) {
if(v == start) {
Q.push(v);
vis[v] = true;
dist[v] = ;
}
else {
vis[v] = false;
dist[v] = INF;
}
}
while(!Q.empty()) {
int u = Q.top(); Q.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if(dist[v] > dist[u] + edge[i].w) {
dist[v] = dist[u] + edge[i].w;
if(!vis[v]) {
vis[v] = true;
Q.push(v);
}
}
}
}
} int main () {
int n, m, a, b, cost;
while(~scanf("%d %d", &n, &m)) {
num = ;
memset(head, -, sizeof head);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &a, &b, &cost);
addedge(a, b, cost);
}
spfa(, n);
printf("%d\n", dist[n]);
}
return ;
}

或者用数组模拟栈也可以:

 #include <cstdio>
#include <cstring>
using namespace std; const int maxn = , maxe = , INF = 0x3f3f3f3f;
struct node {
int to, w, next;
}edge[maxe];
int num, head[maxn], dist[maxn];
int Q[maxn];
bool vis[maxn]; void addedge(int u, int v, int cost) {
edge[num].to = v;
edge[num].w = cost;
edge[num].next = head[u];
head[u] = num ++;
} void spfa(int start, int n) {
int top = ;
for(int v = ; v <= n; v ++) {
if(v == start) {
Q[top ++] = v;
vis[v] = true;
dist[v] = ;
}
else {
vis[v] = false;
dist[v] = INF;
}
}
while(top != ) {
int u = Q[-- top];
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if(dist[v] > dist[u] + edge[i].w) {
dist[v] = dist[u] + edge[i].w;
if(!vis[v]) {
vis[v] = true;
Q[top ++] = v;
}
}
}
}
} int main () {
int n, m, a, b, cost;
while(~scanf("%d %d", &n, &m)) {
num = ;
memset(head, -, sizeof head);
for(int i = ; i <= m; i ++) {
scanf("%d %d %d", &a, &b, &cost);
addedge(a, b, cost);
}
spfa(, n);
printf("%d\n", dist[n]);
}
return ;
}
上一篇:CS50.2


下一篇:图论分支-差分约束-SPFA系统