博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3159
阅读量:7087 次
发布时间:2019-06-28

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

Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 28133   Accepted: 7766

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

Sample Output

5

Hint

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

Source

, Sempr
题解:

题意:班上有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个同学为起点,可以利用Spfa+Stack算法求解,但由于数据原因必须用Stack,如果用Queue则会超时。

Pass:

一直不知道差分约束是什么类型题目,最近在写最短路问题就顺带看了下,原来就是给出一些形如x-y<=b不等式的约束,问你是否满足有解的问题

好神奇的是这类问题竟然可以转换成图论里的最短路径问题,下面开始详细介绍下

比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图

由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较k1+k2和k3的大小,求出最小的就是c-a的最大值了

根据以上的解法,我们可能会猜到求解过程实际就是求从a到c的最短路径,没错的....简单的说就是从a到c沿着某条路径后把所有权值和k求出就是c -a<=k的一个

推广的不等式约束,既然这样,满足题目的肯定是最小的k,也就是从a到c最短距离...

#include
#include
#include
using namespace std;#define N 150010int d[N],u[N],v[N],head[N],next[N],stack[N*4],vis[N];int n,m,S,T,x,y,z,tot=0;inline void bianbao(int x,int y,int z){ u[++tot]=y; v[tot]=z; next[tot]=head[x]; head[x]=tot;}inline void spfa(){ for(int i=2;i<=n;i++) d[i]=0x3f3f3f3f; d[S=1]=0; int top=0; stack[++top]=S; vis[S]=1; while(top){ int p=stack[top--]; vis[p]=0; for(int i=head[p];i;i=next[i]) if(d[u[i]]>d[p]+v[i]){ d[u[i]]=d[p]+v[i]; if(!vis[u[i]]){ vis[u[i]]=1; stack[++top]=u[i]; } } } printf("%d\n",d[n]);}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&z); bianbao(x,y,z); } spfa(); return 0;}

 

转载于:https://www.cnblogs.com/shenben/p/5618619.html

你可能感兴趣的文章
WPF如何实现一个漂亮的页签导航UI
查看>>
Dubbo+zookeeper实现分布式服务框架
查看>>
HTML编码规范
查看>>
游戏开发者福音:微软开源部分 Minecraft 的 Java 代码
查看>>
Firefox 66 存在使 PPT 文字消失的 bug,v68 才修复
查看>>
Android 三星手机拍照,从图库选择照片旋转问题完美解决
查看>>
在线表格 x-spreadsheet 1.0.16 发布
查看>>
PostgreSQL 多值列的选择性 - Statistics, Cardinality, Selectivity, Estimate
查看>>
三大主流芯片架构特点
查看>>
Python Flask学习知识点(四)
查看>>
Confluence 6 数据库整合的限制
查看>>
scala 与 java泛型数组
查看>>
哈佛团队开发出使用声波来辅助粘性液体的3D打印技术
查看>>
leaflet实用插件整理
查看>>
vue基础
查看>>
Eclipse中安装MemoryAnalyzer插件及使用
查看>>
GEF入门实例_总结_02_新建初始RCP空项目
查看>>
用js来实现那些数据结构04(栈01-栈的实现)
查看>>
你的api加锁了吗?
查看>>
Alpine Linux 3.9.2 发布,轻量级 Linux 发行版
查看>>