poj3159 Candies--单源最短路径&差分约束
发布日期:2021-10-03 20:32:08 浏览次数:3 分类:技术文章

本文共 3130 字,大约阅读时间需要 10 分钟。

原题链接:

一:原题内容

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 21 2 52 1 4

Sample Output

5

二:分析理解

班上有n个同学,现在有一些糖要分给他们,设第i个同学得到的糖为p[i],分糖必须满足条件:第i个同学要求第j个同学的糖不能超过自己k个,即p[j] - p[i] <= k,k >= 0。要求在满足这些条件的情况下,求出p[n] - p[1]的最大值。

由p[j] - p[i] <= k可得p[j] <= p[i] + k
在单源最短路径的算法中有一步是“若mindis[j] > mindis[i] + dis[i][j],则mindis[j] = mindis[i] + dis[i][j],这样就满足mindis[j] <= mindis[i] + dis[i][j]”。因此本题可以使用单源最短路径的算法来解决,对于“第i个同学要求第j个同学的糖不能超过自己k个,即p[j] - p[i] <= k,k >= 0”这个条件,建立一条边(i->j)=k,由于不含负权路径,因此建立完所有边之后以第1个同学为起点,采用Dijkstra+Heap求最短路径即可。除了Dijkstra也可以利用Spfa+Stack算法求解,但由于数据原因必须用Stack,如果用Queue则会超时。

三:AC代码

#include
#include
#include
using namespace std;struct Edge{ int v;//箭头指向的位置,在此不存储u节点 int dis;//边的距离,也就是权值 int next;//持有同一个u的下一个边};Edge edge[150005];int head[30005];int dis[30005];int sta[30005];bool visited[30005];int n, m;int a, b, c;int num;void AddEdge(int u, int v, int w){ edge[num].dis = w; edge[num].v = v; edge[num].next = head[u]; head[u] = num++;}void Spfa(int s){ int top = 0; fill(dis, dis + 30005, INT_MAX); sta[++top] = s; visited[s] = 1; dis[s] = 0; while (top) { int u = sta[top--]; visited[u] = 0; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if (dis[v] > dis[u] + edge[i].dis) { dis[v] = dis[u] + edge[i].dis; if (!visited[v]) { sta[++top] = v; visited[v] = 1; } } } }}int main(){ scanf("%d%d", &n, &m); num = 0; memset(head, -1, sizeof(head)); while (m--) { scanf("%d%d%d", &a, &b, &c); AddEdge(a, b, c); } Spfa(1); printf("%d\n", dis[n]); return 0;}

转载地址:https://blog.csdn.net/LaoJiu_/article/details/51027346 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:poj1716 Integer Intervals--单源最短路径&差分约束
下一篇:hdu1531 King--单源最短路径&差分约束

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年03月17日 23时06分34秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

php taglib.php,thinkphp5 taglib自定义标签教程 2019-04-21
java常用包类 array,Java中的StringBuffer和数组Arrays以及常用类型的包装类 2019-04-21
ctf常见php,CTF中常见的PHP伪协议 2019-04-21
php语言冒泡法,PHP 冒泡排序法 2019-04-21
php如何数组去重复,PHP如何去除数组重复元素? 2019-04-21
java转换ab的值,查看新闻/公告--[整理]Java将AB1234形式的16进制字符串转换为10进制数值,考虑字节序的影响.... 2019-04-21
ui php h5,画出自己的UI组件的详情 2019-04-21
linux服务文件编写,linux编写systemd下服务脚本 2019-04-21
hdfs linux 目录是否存在,Linux中判断hdfs文件是否存在 2019-04-21
linux学习需要什么基础,学linux需要什么基础? 2019-04-21
linux vim编辑kconfig 无法wq,Linux-4.9.2内核在mini2440上的移植(三)——编译环境测试... 2019-04-21
高斯勒让德在c语言中的程序,c语言:用递归方法编写程序,求n阶勒让德多项式的值... 2019-04-21
c语言单片机电子时钟,新人求个51单片机的电子时钟汇编语言(C语言的还没学到)... 2019-04-21
c++语言文件流,C++文件流 2019-04-21
android 动态毛玻璃,Android毛玻璃背景效果简单实现代码 2019-04-21
android 按钮提示,的Android按钮工具提示 2019-04-21
iphone通讯录 android,3个方法,教你如何快速而又有效的将联系人从iPhone转移到安卓... 2019-04-21
android horizontalscrollview 滑动事件,ScrollView的滑动监听(以HorizontalScrollView为例) 2019-04-21
win7自定义html为桌面,Win7系统自定义桌面主题的方法 2019-04-21
单系统 台电x80pro_台电x80 pro (ID:E3E6)安装remix OS系统教程整理 2019-04-21