poj 3159 Candies 差分约束
| Time Limit: 1500MS | Memory Limit: 131072K | |
| Total Submissions: 22177 | Accepted: 5936 |
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 Bshould 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,m
接下来m行,每行输入A,B,C
输入A B C,表示孩子B最多比孩子A多C块蛋糕,问孩子1与孩子N最多相差多少块蛋糕!
Tips:
不能用queue<>队列来做,模拟队列可以!
#include "stdio.h"
#include "string.h"
//#include "queue"
//using namespace std; #define N 30005 //图中点的个数
#define INF 0x3fffffff struct node
{
int x,y;
int weight;
int next;
}edge[5*N]; int n,m;
int dist[N];
bool mark[N];
int head[N],idx;
int stack[5*N]; void Init();
void SPFA();
void Add(int x,int y,int k); int main()
{
int i;
int x,y,k;
scanf("%d %d",&n,&m);
Init();
for(i=0; i<m; ++i)
{
scanf("%d %d %d",&x,&y,&k); /***y-x<=k***从点x到点y建边,权值为k**/
Add(x,y,k);
}
SPFA();
printf("%d\n",dist[n]);
return 0;
} void Init()
{
idx = 0;
memset(head,-1,sizeof(head));
} void Add(int x,int y,int k)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = k;
edge[idx].next = head[x];
head[x] = idx++;
} void SPFA() //这题只能模拟队列~
{
int i;
int x,y;
memset(mark,false,sizeof(mark));
for(i=1; i<=n; ++i) dist[i] = INF;
int top = 0;
//queue<int> q;
stack[++top] = 1;//q.push(1);
mark[1] = true;
dist[1] = 0;
while(top)
{
x = stack[top--];
//q.pop();
for(i=head[x]; i!=-1; i=edge[i].next)
{
y = edge[i].y;
if(dist[y] > dist[x] + edge[i].weight)
{
dist[y] = dist[x] + edge[i].weight;
if(!mark[y])
{
mark[y] = true;
stack[++top] = y;//q.push(y);
}
}
}
mark[x] = false;
}
}
poj 3159 Candies 差分约束的更多相关文章
- POJ 3159 Candies 差分约束dij
分析:设每个人的糖果数量是a[i] 最终就是求a[n]-a[1]的最大值 然后给出m个关系 u,v,c 表示a[u]+c>=a[v] 就是a[v]-a[u]<=c 所以对于这种情况,按照u ...
- [poj 3159]Candies[差分约束详解][朴素的考虑法]
题意 编号为 1..N 的人, 每人有一个数; 需要满足 dj - di <= c 求1号的数与N号的数的最大差值.(略坑: 1 一定要比 N 大的...difference...不是" ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)
原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- 图论--差分约束--POJ 3159 Candies
Language:Default Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 43021 Accep ...
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
随机推荐
- EF容器---代理类对象
#region 修改--官方的修改是,先查询,然后修改 /// <summary> /// 修改--官方的修改是,先查询,然后修改 /// </summary> static ...
- 【Unity】13.1 场景视图中的GI可视化
分类:Unity.C#.VS2015 创建日期:2016-05-19 一.简介 在场景视图中设计不同的场景内容时,可以根据需要勾选相关的渲染选项,以便让场景仅显示其中的一部分或者全部渲染效果. 在这些 ...
- 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop
[源码下载] 重新想象 Windows 8 Store Apps (49) - 输入: 获取输入设备信息, 虚拟键盘, Tab 导航, Pointer, Tap, Drag, Drop 作者:weba ...
- inotify--内核中文件系统的通知机制
转载:http://www.ibm.com/developerworks/cn/linux/l-inotifynew/index.html 一. 引言 众所周知,Linux 桌面系统与 MAC 或 W ...
- JDK动态代理的实现及原理
Proxy.newProxyInstance(classloader,Class,invocationHandler) 调用getProxyClass0(loader, interfaces)生成代理 ...
- 转载 教你使用PS来制作unity3D随机地形
- SharpGL学习笔记(十四) 材质:十二个材质球
材质颜色 OpenGL用材料对光的红.绿.蓝三原色的反射率来近似定义材料的颜色.象光源一样,材料颜色也分成环境.漫反射和镜面反射成分,它们决定了材料对环境光.漫反射光和镜面反射光的反射程度.在进行光照 ...
- [Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考、NuGet组件参考
[Xamarin.iOS] Visual Studio中Xamarin.iOS项目,无法加入PCL项目参考.NuGet组件参考 解决方案 目前Visual Studio中最新版本的Xamarin.iO ...
- Jquery easyui Tree的简单使用
Jquery easyui Tree的简单使用 Jquery easyui 是jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery EasyUI的目标就是帮助web开发者更轻 ...
- JavaScript this特性,静态方法 和实例方法,prototype
<script type="text/javascript"> function logs(str) { document.write(str + "< ...