poj3159
| 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 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
Source
题意:班上有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<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 150010
int d[N],u[N],v[N],head[N],next[N],stack[N*],vis[N];
int n,m,S,T,x,y,z,tot=;
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=;i<=n;i++) d[i]=0x3f3f3f3f;
d[S=]=;
int top=;
stack[++top]=S;
vis[S]=;
while(top){
int p=stack[top--];
vis[p]=;
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]]=;
stack[++top]=u[i];
}
}
}
printf("%d\n",d[n]);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
bianbao(x,y,z);
}
spfa();
return ;
}
poj3159的更多相关文章
- 【poj3159】 Candies
http://poj.org/problem?id=3159 (题目链接) 题意 有n个小朋友,班长要给每个小朋友发糖果.m种限制条件,小朋友A不允许小朋友B比自己多C个糖果.问第n个小朋友最多比第1 ...
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- POJ-3159 Candies 最短路应用(差分约束)
题目链接:https://cn.vjudge.net/problem/POJ-3159 题意 给出一组不等式 求第一个变量和最后一个变量可能的最大差值 数据保证有解 思路 一个不等式a-b<=c ...
- POJ-3159(差分约束+Dijikstra算法+Vector优化+向前星优化+java快速输入输出)
Candies POJ-3159 这里是图论的一个应用,也就是差分约束.通过差分约束变换出一个图,再使用Dijikstra算法的链表优化形式而不是vector形式(否则超时). #include< ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- poj3159 最短路(差分约束)
题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...
- poj3159 Candies(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Candies Time Limit: 1500MS Memory Limit ...
- 【POJ3159】Candies 裸的pqspfa模版题
不多说了.就是裸的模版题. 贴代码: <span style="font-family:KaiTi_GB2312;font-size:18px;">#include & ...
- poj3159最短路spfa+邻接表
https://vjudge.net/contest/66569#problem/K 相当于模板吧,第一次写spfa的 #include<iostream> #include<cst ...
- poj3159 Candies SPFA
题目链接:http://poj.org/problem?id=3159 题目很容易理解 就是简单的SPFA算法应用 刚开始用STL里的队列超时了,自己写了个栈,果断过,看来有时候栈还是快啊.... 代 ...
随机推荐
- ActiveMQ消息游标 --转载
转:http://blog.csdn.net/m13321169565/article/details/8081358 在Activemq以前的版本中,broker会把待发送的消息保存在内存中.这种方 ...
- MySQL解释--百度百科
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS ...
- AR路由器web界面每IP限速配置方法
一.做下载方向的限速:在 QOS>接口限速,选择“新建”“接口名称”选择内网接口“限速类型”选择IP限速(目的)“方向”选择流出“起始/目的ip”写内网的ip“类型”选择独占“承诺速率”为限速的 ...
- 架构师-盛大许式伟VS金山张宴
许式伟:作为系统架构师,您一般会从哪些方面来保证网站的高可用性(降低故障时间)? 张宴:很多因素都会导致网站发生故障,从而影响网站的高可用性,比如服务器硬件故障.软件系统故障.IDC机房故障.程序上线 ...
- iBatis in或not in 查询
iBatis in或not in 查询 open:内容开头 close:内容结尾 conjunction:分隔符 <isNotNull prepend="and" pro ...
- NIO之缓冲区(Buffer)的数据存取
缓冲区(Buffer) 一个用于特定基本数据类行的容器.有java.nio包定义的,所有缓冲区都是抽象类Buffer的子类. Java NIO中的Buffer主要用于与NIO通道进行交互,数据是从通道 ...
- Atitit. . 软件命名空间与类名命名单词的统计程序设计v2
Atitit. . 软件命名空间与类名命名单词的统计程序设计v2 1. 要实现的目标1 1.1. Camel字符串模式的分词处理1 1.2. 多个大写的处理1 1.3. 数字与字幕的分离1 1.4. ...
- [ci]sonar sonar-runner安装并实现手动扫描项目
安装sonar: 下载地址:https://www.sonarqube.org/downloads/ wget https://sonarsource.bintray.com/Distribution ...
- activiti自己定义流程之整合(三):整合自己定义表单创建模型
本来在创建了表单之后应该是表单列表和预览功能.可是我看了看整合的代码,和之前没实用angularjs的基本没有什么变化,一些极小的变动也仅仅是基于angularjs的语法,因此全然能够參考之前说些的表 ...
- Drawable资源的初步使用
刚開始接触到Android的时候,看到类似以下的一个Button: 当时感觉这种button有点像Material Design风格.真的以为是裁剪好的图片,好奇心驱使我上网查找实现的方法,原来不是裁 ...