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里的队列超时了,自己写了个栈,果断过,看来有时候栈还是快啊.... 代 ...
随机推荐
- Java Executor框架
java.util.concurrent 包中包含灵活的线程池实现,但是更重要的是,它包含用于管理实现 Runnable 的任务的执行的整个框架,该框架称为 Executor 框架.该框架基于生产者- ...
- 野哥点评了Facebook、Amazon、Google、微软和苹果
娱乐一下.我们来比較一下4家相似的科技公司的软件政治观. (1)Facebook-诊断:极端自由. Facebook的规模已经非常大了.可他们的行为处事仍然像是一家创业公司.并且到眼下为止似乎也活得挺 ...
- iOS 带IAP提交注意事项及无法submit for review的解决方案
原地址:http://blog.sina.com.cn/s/blog_71ce775e0101dl4a.html 最近项目接触到了苹果的程序内购(IAP),碰到不少问题,参考了很多帖子才得以解决.在此 ...
- Android项目-几种常见的应用架构
android两种Tab分页的方式:TabActivity和ActivityGroup http://www.fengfly.com/plus/view-209429-1.html 1.单个Activ ...
- CSAPP:异常控制流
在一般的情况下,处理器处理的指令序列是相邻的(顺序执行). 异常控制流提供了指令的跳转,它一部分是由硬件实现的,一部分是由操作系统实现的. 异常处理 在系统启动时,操作系统分配和初始化一张称为异常表的 ...
- PNP管理器简析--基于ReactOS0.33
CSDN上转悠了一圈发现关于PNP管理的文章不多.那就由我献个丑,记录自己对PNP管理器的看法. pnp管理器被描写叙述为向内核和应用程序提供关于设备拔插的通知,凭感觉,pnp管理器应该是个线程函数等 ...
- 更改 easyUI 的皮肤样式
我的版本是:jquery-easyui-1.3.2.根据官方提供的皮肤样式,——在theme 里面: 只需要在引入的 页面中 link样式的地址改变即可: <link rel="sty ...
- PHP多线程处理问题
近日工作中涉及到项目同时处理多个线程问题时,在网上找到了PHP的pthreads扩展以及curl_multi_init函数,具体如下: 一 .windows下安装php真正的多线程扩展pthreads ...
- U盘安装
首先插入已经制作为启动盘的U盘,然后开机长按ESC,出现如下界面: 选择KingstonDataTraveler 3.0 按enter键进入,进入加载界面,之后出现如下界面: 这是在已经下载好系统之后 ...
- JQuery选择器大全 前端面试送命题:面试题篇 对IOC和DI的通俗理解 c#中关于协变性和逆变性(又叫抗变)帮助理解
JQuery选择器大全 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素 ...