poj--3159--Candies(简单差分约束)
Time Limit: 1500MS | Memory Limit: 131072K | |
Total Submissions: 26888 | Accepted: 7398 |
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 throughN.
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<cstdio>
- #include<iostream>
- #include<cstring>
- #include<queue>
- #include<algorithm>
- using namespace std;
- #define MAXN 30010
- #define MAXM 150000
- #define INF 0x3f3f3f
- int head[MAXN],vis[MAXN],dis[MAXN],Instack[MAXN];
- int n,m,cnt;
- struct node
- {
- int u,v,val;
- int next;
- }edge[MAXM];
- void init()
- {
- memset(head,-1,sizeof(head));
- cnt=0;
- }
- void add(int u,int v,int val)
- {
- node E={u,v,val,head[u]};
- edge[cnt]=E;
- head[u]=cnt++;
- }
- void getmap()
- {
- while(m--)
- {
- int a,b,c;
- // cin>>a>>b>>c;
- scanf("%d%d%d",&a,&b,&c);
- add(a,b,c);
- }
- }
- void SPFA()
- {
- memset(vis,0,sizeof(vis));
- memset(Instack,0,sizeof(Instack));
- memset(dis,INF,sizeof(dis));
- dis[1]=0;
- vis[1]=1;
- int top=0;
- Instack[top++]=1;
- while(top)
- {
- int u=Instack[--top];
- vis[u]=0;
- for(int i=head[u];i!=-1;i=edge[i].next)
- {
- node E=edge[i];
- if(dis[E.v]>dis[u]+E.val)
- {
- dis[E.v]=dis[u]+E.val;
- if(!vis[E.v])
- {
- vis[E.v]=1;
- Instack[top++]=E.v;
- }
- }
- }
- }
- cout<<dis[n]<<endl;
- }
- int main()
- {
- while(cin>>n>>m)
- {
- init();
- getmap();
- SPFA();
- }
- return 0;
- }
poj--3159--Candies(简单差分约束)的更多相关文章
- POJ 3159 Candies(差分约束,最短路)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 20067 Accepted: 5293 Descrip ...
- POJ 3159 Candies(差分约束+spfa+链式前向星)
题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...
- POJ 3159 Candies 【差分约束+Dijkstra】
<题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 1 ...
- POJ 3159 Candies(差分约束+最短路)题解
题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...
- POJ 3159 Candies(差分约束)
http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...
- poj 3159 Candies (差分约束)
一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c. ...
- POJ 3159 Candies 还是差分约束(栈的SPFA)
http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...
- POJ 3159 Candies (图论,差分约束系统,最短路)
POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...
- (简单) POJ 3159 Candies,Dijkstra+差分约束。
Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...
- POJ 3159 Candies(SPFA+栈)差分约束
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c 最后求fly[n]最多能比so[1] ...
随机推荐
- POJ 3122 二分
大致题意: 就是公平地分披萨pie 我生日,买了n个pie,找来f个朋友,那么总人数共f+1人 每个pie都是高为1的圆柱体,输入这n个pie的每一个尺寸(半径),如果要公平地把pie分给每一个人(就 ...
- winfrom窗体属性
- javascript学习中自己对作用域和作用域链理解
在javascript学习中作用域和作用域链还是相对难理解些,下面我关于javascript作用域和作用域链做一下详细介绍,给各位初学者答疑解惑. 首先我们介绍一下什么是作用域? 从字面上理解就是起 ...
- C# 学习笔记_类
定义:将成员及方法封装到类中,类的实例则称为对象. 结构:属性,类修饰符,class,类名,{类体} 类修饰符:new,public,protected,internal,private,abstra ...
- Eclipse中添加对Python的中文支持
原文链接:http://down.51cto.com/data/751371 首先要确保eclipse编辑器环境的编码为utf8,这个是大前提:其次如果py文件中含有中文字符的话,需要在py文件中对编 ...
- 创建一个dynamics CRM workflow (六) - Debugging Custom Workflows
我们也deploy部署了custom workflows, debugging是开发当中不可或缺的一个步骤. debug workflow的步骤和debug有些许不一样: 1. install pro ...
- 使用DOS命令查找包含某一字符串的所有文件
在windows系统下,来查找并修改指定目录下包含某一字符串的所有文件,麻烦又费时.其实在DOS命令中,提供了Findstr命令来查找指定的一个或多个文件文件中包含(或通过参数 /V来控制不包含) ...
- JS 封装一个求n~m的求和函数
var a = 0; cc(2,10); function cc(n,m){ for(var i =n;i<(m+1);i++){ a = a + ...
- 日常记录-Pandas Cookbook
Cookbook 1.更新内容 2.关于安装 3.Pandas使用注意事项 4.包环境 5.10分钟Pandas初识 6.教程 7.Cookbook 8.数据结构简介 9.基本功能 10.使用文本数据 ...
- Bash 如何取得当前正在执行的脚本的绝对路径?
转自:http://blogread.cn/it/article/6549?f=wb Bash 如何取得当前正在执行的脚本的绝对路径? 如题,一般我们写Shell脚本的时候,都倾向使用绝对路径,这样无 ...