poj3159 Candies(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
| Time Limit: 1500MS | Memory Limit: 131072K |
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <cstdlib>
using namespace std;
#define REP(A,X) for(int A=0;A<X;A++)
#define MAXE 200010
#define MAXP 30010
#define INF 0x7fffffff
#define MP(A,B) make_pair(A,B)
typedef pair<int,int> PII ;
struct node{
int v,d,next;
}edge[MAXE];
int e=;
int head[MAXP];
void init(){
e=;
REP(i,MAXP)head[i]=-;
}
void add_edge(int u,int v,int d)
{
edge[e].v=v;
edge[e].d=d;
edge[e].next=head[u];
head[u]=e;
e++;
}
int vis[MAXP],dis[MAXP];
void spfa()
{
REP(i,MAXP)vis[i]=;
REP(i,MAXP)dis[i]=i==?:INF;
//queue<int>q;
stack<int>q;
q.push();
vis[]=;
while(!q.empty()){
//int x=q.front();
int x=q.top();
q.pop();
for(int t=head[x];t!=-;t=edge[t].next)
{
int y=edge[t].v;
int d=edge[t].d;
if(dis[y]>dis[x]+d){
dis[y]=dis[x]+d;
if(!vis[y]){
q.push(y);
vis[y]=;
}
}
}
vis[x]=;
}
}
void dijkstra(int s)
{
REP(i,MAXP)vis[i]=;
REP(i,MAXP)dis[i]=i==s?:INF;
priority_queue<PII,vector<PII>,greater<PII> > q;
q.push(MP(dis[s],s));
while(!q.empty())
{
PII p=q.top();
q.pop();
int x=p.second;
if(vis[x])continue;
vis[x]=;
for(int t=head[x];t!=-;t=edge[t].next)
{
int y=edge[t].v;
int d=edge[t].d;
if(!vis[y]&&dis[y]>dis[x]+d)
{
dis[y]=dis[x]+d;
q.push(MP(dis[y],y));
}
}
} } int main()
{
int m,n;
while(scanf("%d%d",&n,&m)!=EOF){
int u,v,w;
init();
REP(i,m){
scanf("%d%d%d",&u,&v,&w);
add_edge(u,v,w);
}
//dijkstra(1);
spfa();
printf("%d\n",dis[n]);
}
return ;
}
代码君
poj3159 Candies(差分约束)的更多相关文章
- poj3159 Candies(差分约束,dij+heap)
poj3159 Candies 这题实质为裸的差分约束. 先看最短路模型:若d[v] >= d[u] + w, 则连边u->v,之后就变成了d[v] <= d[u] + w , 即d ...
- POJ-3159.Candies.(差分约束 + Spfa)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 40407 Accepted: 11367 Descri ...
- [poj3159]Candies(差分约束+链式前向星dijkstra模板)
题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...
- POJ3159 Candies —— 差分约束 spfa
题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS Memory Limit: 131072K Total Submiss ...
- POJ-3159(差分约束+Dijikstra算法+Vector优化+向前星优化+java快速输入输出)
Candies POJ-3159 这里是图论的一个应用,也就是差分约束.通过差分约束变换出一个图,再使用Dijikstra算法的链表优化形式而不是vector形式(否则超时). #include< ...
- 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 差分约束
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 22177 Accepted: 5936 Descrip ...
- POJ3159(KB4-K 差分约束)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 33283 Accepted: 9334 Descrip ...
随机推荐
- Python爬虫——抓取贴吧帖子
抓取百度贴吧帖子 按照这个学习教程,一步一步写出来,中间遇到很多的问题,一一列举 首先, 获得 标题 和 贴子总数 # -*- coding:utf-8 -*- #!/user/bin/python ...
- UWP开发小记
针对个人的上一篇文章中提到的遇到的几个问题,做一下个人解答 DLL部署的问题,可以将DLL添加到工程中,属性中设置content为true,这样,部署目录下就会有这个文件. 需要说明的是,这个文件确实 ...
- oracle表空间使用情况查询
1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_files 2 group by tabl ...
- 五种实用DOM方法总结
DOM:Document Object Model 文档对象模型 对象的三种类型:用户定义类型.内建对象.宿主对象 节点类型:元素节点.文本节点.属性节点 getElementById()方法: 将返 ...
- [FML]学习笔记二 PAC Learning Model
对于一个concept class C,如果存在一个算法A和一个多项式poly(.,.,.,.),有对于任意的ε>0.δ>0以及X的任意分布D和任何target concept C,当sa ...
- uva 227 Puzzle
Puzzle A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...
- [TYVJ] P1015 公路乘车
公路乘车 描述 Description 一个特别的单行街道在每公里处有一个汽车站.顾客根据他们乘坐汽车的公里使来付费.例如样例的第一行就是一个费用的单子. 没有一辆车子行驶超过10公里,一个顾客打算行 ...
- 【CCNA学习笔记】1.思科路由器的基本配置
教学视频来源:http://edu.51cto.com/lesson/id-10930.html. 怎么安装模块.连交叉线什么的视频里面老师说的很清楚了,我只记录一下IOS配置的命令(虽然一副不明觉厉 ...
- 【转】嵌入式Linux学习笔记
一 嵌入式系统定义: 应用于特定环境的硬件体系. 二 两样非常重要的能力: 1. 掌握各种新概念的能力 2. 调试的能力( 包括软件, 硬件 ) 三 需要的基础知识: 1. 操作系统理论基 ...
- linux中查找命令find、locate、whereis、which、type区别
linux中查找命令find.locate.whereis.which.type区别 1. find Java代码 find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件.与查询数据库(/ ...