Poj 3159 Candies SPFA+stack

Candies

Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 32572 Accepted: 9107

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

Hint

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

Source

题解:

首先我们来看一下题意,题目说的是给你一些关系,表示B比A多的糖果数不会超过C,最后问你N号人比1号人多多少糖果。
显然,给出的是一些不等关系,那么就很显然是一道差分约束的题,我们用f[i]来表示当前这个人有多少糖果,那么一定会有f[b] <= f[a] + c;
那么移项后就可以得到f[b] – f[a] <= c;
因为我们是求的最大数,所以建边时就可以仿照最短路从而建一条从a到b的边权为c的边。
那么就可以用最短路算法,这里我用的spfa,但要注意的是,STL里面的队列是会TLE的,而栈是不会的,所以这里用stack

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
int fst[1333333], nxt[1333333], d[1333333], tot = 0;
bool use[1333333];
struct qer
{
	int f, t, d;
}es[1333333];
stack <int> q;
void init()
{
	memset(d, 0x3f, sizeof(d));
	memset(fst, -1, sizeof(fst));
	memset(use, 0, sizeof(use));
	tot = 0;
}
void build(int ff, int tt, int dd)
{
	es[++tot] = (qer){ff, tt, dd};
	nxt[tot] = fst[ff];
	fst[ff] = tot;
}
void spfa(int s)
{
	use[s] = 1;
	q.push(s);
	d[s] = 0;
	while (!q.empty())
	{
		int u = q.top();
		q.pop();
		use[u] = 0;
		for (int i = fst[u]; i; i = nxt[i])
		{
			int v = es[i].t;
			if (d[v] > d[u] + es[i].d)
			{
				d[v] = d[u] + es[i].d;
				if (!use[v])
				{
					use[v] = 1;
					q.push(v);
				}
			}
		}
	}
}
int main()
{
	int n, m;
	while (scanf("%d%d", &n, &m) != EOF)
	{
		init();
		for (int i = 1; i <= m; i++)
		{
			int f, t, d;
			scanf("%d%d%d", &f, &t, &d);
			build (f, t, d);
		}
		spfa(1);
		cout << d[n] << endl;
	}
	return 0;
}

 

上一篇
下一篇