POJ——T 3159 Candies
http://poj.org/problem?id=3159
| Time Limit: 1500MS | Memory Limit: 131072K | |
| Total Submissions: 33328 | Accepted: 9349 |
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
#include <cstring>
#include <cstdio>
#include <stack> const int N(+);
const int M(+);
int n,m,u,v,w;
int head[N],sumedge;
struct Edge
{
int v,next,w;
Edge(int v=,int next=,int w=):
v(v),next(next),w(w){}
}edge[M];
inline void ins(int u,int v,int w)
{
edge[++sumedge]=Edge(v,head[u],w);
head[u]=sumedge;
} int dis[N];
bool instack[N];
int SPFA(int s)
{
for(int i=;i<=n;i++)
instack[i]=,dis[i]=0x7fffffff;
dis[s]=instack[s]=;
std::stack<int>sta; sta.push(s);
for(;!sta.empty();)
{
u=sta.top(); sta.pop(); instack[u]=;
for(int i=head[u];i;i=edge[i].next)
{
v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!instack[v]) instack[v]=,sta.push(v);
}
}
}
return dis[n];
} inline void read(int &x)
{
x=; register char ch=getchar();
for(;ch>''||ch<'';) ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-'';
} int AC()
{
for(;~scanf("%d%d",&n,&m);)
{
for(;m--;ins(u,v,w))
read(u),read(v),read(w);
printf("%d\n",SPFA());
memset(head,,sizeof(head));
memset(edge,,sizeof(edge));
sumedge=;
}
return ;
} int Hope=AC();
int main(){;}
POJ——T 3159 Candies的更多相关文章
- (poj)3159 Candies
题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- poj 3159 Candies (dij + heap)
3159 -- Candies 明明找的是差分约束,然后就找到这题不知道为什么是求1~n的最短路的题了.然后自己无聊写了一个heap,518ms通过. 代码如下: #include <cstdi ...
- 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(差分约束)
http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...
- 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 还是差分约束(栈的SPFA)
http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...
随机推荐
- XML基本语法
本节要点: 了解XML的文档声明 了解XML的元素.命名规则.属性.元素内容.处理指令等概念 1 XML文档声明 表示该文档是一个XML文档,以及遵循哪个XML版本的规范. 规范:<?xml ...
- Hadoop集群(第13期)_HBase 常用Shell命令
进入hbase shell console$HBASE_HOME/bin/hbase shell如果有kerberos认证,需要事先使用相应的keytab进行一下认证(使用kinit命令),认证成功之 ...
- python2.7编码与解码
常见的编码 ASCII: 美国人发明的,只编码英文字母和符号,1个字节. GB2312: 中国人发明的,增加了中文汉字和符号,2个字节. Unicode: 为了把所有语言都统一到一套编码里,一般是2个 ...
- Inside ARC — to see the code inserted by the compiler
前言 这是我在我们技术团队所做的一次分享,稍作改动放到博客上来. 我们技术团队会不定期(一般一个月1~2次)做技术分享,对我们团队有兴趣的能够私信我. 下面是正文. 这里的主题是"Insid ...
- unity坐标转换问题
unity最经常使用的几种坐标.屏幕坐标.世界坐标.ngui坐标,相对于父物体的坐标(localPoisition). 可是有时候这几种坐标相互转换何其困难,让自己狠抓头. 不得不操作的方式是创建虚拟 ...
- [HDU 5542] The Battle of Chibi
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5542 [算法] 树状数组优化DP [代码] #include<bits/stdc++.h&g ...
- [jzoj 3175] 数树数 解题报告 (树链剖分)
interlinkage: https://jzoj.net/senior/#main/show/3175 description: 给定一棵N 个节点的树,标号从1~N.每个点有一个权值.要求维护两 ...
- python之--初始面向对象
阅读目录 楔子 面向过程vs面向对象 初识面向对象 类的相关知识 对象的相关知识 对象之间的交互 类命名空间与对象.实例的命名空间 类的组合用法 初识面向对象小结 面向对象的三大特性 继承 多态 封装 ...
- Git 学习笔记(一)
某大牛曾经说过,版本控制的最大好处就是让你可以永远后悔,而 Git 无疑是众多版本控制软件当中的佼佼者,在开源社区更是备受青睐,那么它为何会诞生,和其他的版本控制软件项目又有什么不同?且让我们慢慢来看 ...
- POJ 3660 Floyd传递闭包
题意:牛有强弱,给出一些牛的强弱的胜负关系,问可以确定几头牛的排名. 思路: Floyd传递闭包 // by SiriusRen #include <bitset> #include &l ...