【POJ 3159】 Candies(差分约束系统)

【POJ 3159】 Candies(差分约束系统)


Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 26996   Accepted: 7438

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

POJ Monthly--2006.12.31, Sempr

题目大意就是n个child 给出m关系 A B c 代表B不能比A多拥有超过c个糖果

求出1与n最多能相差的糖果数 保证有接

很容易列出不等关系 B-A <= c 根据这个关系建图跑最短路

需要注意的是普通的SPFA会TLE 队列的SPFA也会TLE 用栈让它倒着就可以过 赤果果卡SPFA...

Dijkstra+heap据说能过 但我无限WA 找不出问题 贴上来路过的大家帮忙看看~~~


SPFA+STACK

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <stack>

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 1e5;
const int mod = 1e9+7;
const double eps = 1e-8;

struct Edge
{
	int v,w,next;
};

Edge eg[155555];
int head[30030];
bool vis[30030];
int dis[30030];
int tp,n,m;

void Add(int u,int v,int w)
{
	eg[tp].v = v;
	eg[tp].w = w;
	eg[tp].next = head[u];
	head[u] = tp++;
}

void SPFA(int u)
{
	memset(dis,INF,sizeof(dis));
	memset(vis,0,sizeof(vis));
	stack <int> s;

	dis[u] = 0;
	vis[u] = 1;
	s.push(u);
	int v,w;

	while(!s.empty())
	{
		u = s.top();
		s.pop();
		vis[u] = 0;
		for(int i = head[u]; i != -1; i = eg[i].next)
		{
			v = eg[i].v;
			w = eg[i].w;
			if(dis[v] > dis[u]+w)
			{
				dis[v] = dis[u]+w;
				if(!vis[v])
				{
					vis[v] = 1;
					s.push(v);
				}
			}
		}
	}

}

int main(int argc,char **argv)
{
	int u,v,w;
	while(~scanf("%d%d",&n,&m))
	{
		memset(head,-1,sizeof(head));
		tp = 0;

		while(m--)
		{
			scanf("%d%d%d",&u,&v,&w);
			Add(u,v,w);
		}

		SPFA(1);
		printf("%d\n",dis[n]);
	}
	return 0;
}

WA的Dijkstra= =

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <stack>

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 1e5;
const int mod = 1e9+7;
const double eps = 1e-8;

struct Edge
{
	int v,w,next;
};

Edge eg[155555];
int head[30030];
bool vis[30030];
int dis[30030];
int tp,n,m;

void Add(int u,int v,int w)
{
	eg[tp].v = v;
	eg[tp].w = w;
	eg[tp].next = head[u];
	head[u] = tp++;
}

struct cmp
{
	bool operator() (int a,int b)
	{
		return dis[a] > dis[b];
	}
};

void Dijkstra(int u)
{
	memset(dis,INF,sizeof(dis));
	memset(vis,0,sizeof(vis));
	dis[u] = 0;

	priority_queue <int,vector<int>,cmp> q;
	int v,w;

	while(1)
	{
		for(int i = head[u]; i != -1; i = eg[i].next)
		{
			v = eg[i].v;
			w = eg[i].w;
			if(!vis[v] && dis[v] > dis[u]+w)
			{
				dis[v] = dis[u]+w;
				q.push(v);
			}
		}

		vis[u] = 1;
		if(u == n) return;

		while(vis[q.top()]) q.pop();
		u = q.top();
		q.pop();
	}
}

int main(int argc,char **argv)
{
	int u,v,w;
	while(~scanf("%d%d",&n,&m))
	{
		memset(head,-1,sizeof(head));
		tp = 0;

		while(m--)
		{
			scanf("%d%d%d",&u,&v,&w);
			Add(u,v,w);
		}

		Dijkstra(1);
		printf("%d\n",dis[n]);
	}
	return 0;
}


你可能感兴趣的:(【POJ 3159】 Candies(差分约束系统))