poj-3159 Candies(差分约束问题)

Candies
Time Limit: 1500MS Memory Limit: 131072K
Total Submissions: 25821 Accepted: 7050

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
 
这个题算是spfa算法的一个应用吧, 就是用来找到满足不等式的解
题目大意 :有n个小朋友,有m项限制条件,每一行限制条件有a,b,c三个变量,代表的是b比a最多多c个糖果,求第n个小朋友最多比第一个小朋友多多个糖果
可以想一下,求b比a最多多几个糖果,也就是求b与a之间的最短路径
那么不等式就可以看做求最短路径的问题b-a <= c (dist[v] <= dist[u] + p[i].val 把它变形一下就是
dist[v] - dist[u] <= p[i].val) 故我们可以把a,b看做u,v两个节点,把c看做边上的权值,然后套模板就ok了。
但要注意一下这是个有向图,只能赋一条边
若有言辞不严谨的地方,还请大牛们不吝赐教
代码如下:
# include<cstdio>
# include<cstring>
# include<algorithm>
# define INF 0x3f3f3f3f
# define MAXA 30000 + 100
# define MAXB 150000 + 100
using namespace std;
int n, m;
int head[MAXA];
int vis[MAXA];
int dist[MAXA];
int cnt;

struct node
{
	int from, to, val, next;
}p[MAXB];

void init()
{
	cnt = 0;
	memset(head, -1, sizeof(head));
}

void add(int u, int v, int w)
{
	p[cnt].from = u;
	p[cnt].to = v;
	p[cnt].val = w;
	p[cnt].next = head[u];
	head[u] = cnt++; 
}

void get()
{
	int a, b, c;
	while(m--)
	{
		scanf("%d%d%d",&a, &b, &c);
		add(a, b, c);//这里是有向图,只能单边赋值 
	}
}

void spfa()
{
	int stack[MAXA];
	int top = 0;
	for(int i = 1; i <= n; i++)
	{
		dist[i] = INF;
		vis[i] = 0;
	}
	dist[1] = 0;
	vis[1] = 1;
	stack[top++] = 1;
	while(top)
	{
		int u = stack[--top];
		vis[u] = 0;
		for(int i = head[u]; i != -1; i = p[i].next)
		{
			int v = p[i].to;
			if(dist[v] > dist[u] + p[i].val)
			{
				dist[v] = dist[u] + p[i].val;
				if(!vis[v])
				{
					vis[v] = 1;
					stack[top++] = v;
				}
			}
		}
	}
	printf("%d\n",dist[n]);
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		get();
		spfa();
	}
	return 0;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、资源1项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值