地址:http://poj.org/problem?id=3159

题目:

Candies
Time Limit: 1500MS   Memory Limit: 131072K
Total Submissions: 31463   Accepted: 8782

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 AB 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

32-bit signed integer type is capable of doing all arithmetic.

Source

POJ Monthly--2006.12.31, Sempr
 
思路:
  差分约束:对于每个信息 x y z,建一条从x到y的权值为z的边。然后跑最短路。
  别用queue,会T。要用stack。
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath>
 4
 5 using namespace std;
 6
 7 #define MP make_pair
 8 #define PB push_back
 9 typedef long long LL;
10 typedef pair<int,int> PII;
11 const double eps=1e-8;
12 const double pi=acos(-1.0);
13 const int K=1e6+7;
14 const int mod=1e9+7;
15
16 struct node
17 {
18     int to,v,next;
19 }edge[K];
20 int tot,head[K];
21 void add(int x,int y,int z)
22 {
23     edge[tot].to=y,edge[tot].v=z,edge[tot].next=head[x];
24     head[x]=tot++;
25 }
26 int vis[K],dis[K],sk[K];
27 void spfa(void)
28 {
29     int cnt=0;
30     sk[cnt++]=1,vis[1]=1,dis[1]=0;
31     while(cnt)
32     {
33         int u=sk[--cnt];
34         vis[u]=0;
35         for(int i=head[u];~i;i=edge[i].next)
36         {
37             int v=edge[i].to,w=edge[i].v;
38             if(dis[v]>dis[u]+w)
39             {
40                 dis[v]=dis[u]+w;
41                 if(!vis[v])
42                     sk[cnt++]=v,vis[v]=1;
43             }
44         }
45     }
46 }
47 int main(void)
48 {
49     int n,m;
50     while(~scanf("%d%d",&n,&m))
51     {
52         tot=0;
53         for(int i=1;i<=n;i++)
54             head[i]=-1,vis[i]=0,dis[i]=0x3f3f3f3f;
55         for(int i=1,u,v,w;i<=m;i++)
56             scanf("%d%d%d",&u,&v,&w),add(u,v,w);
57         spfa();
58         printf("%d\n",dis[n]);
59     }
60     return 0;
61 }

转载于:https://www.cnblogs.com/weeping/p/6973408.html

poj3159 Candies相关推荐

  1. POJ3159 Candies 差分约束

    一.内容 During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teac ...

  2. POJ3159 Candies(差分约束)

    题意:给n个人分糖果,下标1到n,给出m个约束条件a b c,a的糖果数比b的糖果少的个数不多于c,即 b的糖果-a的糖果<=c.求n的糖果比1的糖果最多多多少. 思路:查分约束系统的第一题,b ...

  3. 专题三:bfs、图论专题(1.cf)

    其他 挑战程序设计竞赛 1.POJ 3259 Wormholes 这道题后来我又想了一个方法,只能说这个方法不是完全正确,还要因题而定.为什么这么说呢?我用的是Bellman-Ford找负圈,可以这么 ...

  4. 【差分约束】POJ3159/LG P1993 小K的农场

    终于把差分约束刷完了!!,这些题的套路都是很类似的 题目描述小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种 ...

  5. K - Candies POJ - 3159(利用了自定义比较操作符)

    K - Candies POJ - 3159 题意: 孩子 A 觉得 B 得到的糖果不能比自己多超过 c,求 n 比 1 最多能多几颗糖果 思路:DJ,松弛条件: sweet[A] > swee ...

  6. AtCoder Beginner Contest 215 G - Colorful Candies 2

    AtCoder Beginner Contest 215 G - Colorful Candies 2 有n个糖果,每个糖果有着一个颜色a[i],每次拿k个糖果期望拿到E(x)个不同颜色的糖果,求出k ...

  7. 575. Distribute Candies 平均分糖果,但要求种类最多

    [抄题]: Given an integer array with even length, where different numbers in this array represent diffe ...

  8. 1431. Kids With the Greatest Number of Candies

    Title 给你一个数组 candies 和一个整数 extraCandies ,其中 candies[i] 代表第 i 个孩子拥有的糖果数目. 对每一个孩子,检查是否存在一种方案,将额外的 extr ...

  9. HDU - 6126 Give out candies

    Give out candies 题解: 第一次遇见这样处理的网络流模型. 将问题转换成最小割问题. 具体的题解参考自:传送门 先将每个人的拆成m个人. 然后s向第1人连边流量为inf.第i个人向第i ...

最新文章

  1. RedHat Enterprise 5.1下OpenLDAP的配置及PAMNSS的配置
  2. Sql面试题之三(难度:简单| 含答案)
  3. C#正则表达式提取HTML中IMG标签的SRC地址(转)
  4. 线性回归——lasso回归和岭回归(ridge regression)
  5. Windows PE第6章 栈与重定位表
  6. linux下的arm仿真,使用QEMU仿真ARM Linux系统
  7. CentOS6.5 将安装光盘作为yum源
  8. LeetCode 2 Keys Keyboard
  9. java压缩成.tar_java压缩tar.gz | 学步园
  10. shell比较运算符
  11. 操作系统期末总复习(题库)
  12. 大数据导论学习日志Day1
  13. 构建者模式和抽象工厂模式的结合使用
  14. matlab求fft频谱峰值程序,用FFT对信号作频谱分析Matlab程序
  15. 什么是BLOB URL,为什么要使用它?
  16. c++编译STL文件反转其法线
  17. word2010使用记录
  18. 配音赚钱的软件有哪些 ?推荐以下这3个平台
  19. 字节跳动-数据分析-实习面经
  20. java识别手写文字_神经网络入门 第6章 识别手写字体

热门文章

  1. spark共享变量(广播变量Broadcast Variable,累加器Accumulators)
  2. 【linux】常用命令总结
  3. optimizer_mode优化器模式
  4. java Hello World程序分析(翻译自Java Tutorials)
  5. winlogon病毒清除
  6. iOS开发 控件不能绑定拖动到视图ViewController连接的解决方法
  7. 如何打印网页版的发票_纸质发票将消失,电子发票如何报销、打印、收集?这一篇就够了...
  8. Mongodb性能监控
  9. 金三银四,那浏览器兼容你知多少?
  10. Flask-----轻量级的框架,快速的搭建程序