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?

  题目就是差分约束问题,但是这个题卡SPFA。。。。。。

代码如下:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13.  
  14. using namespace std;
  15.  
  16. const int MaxN=;
  17. const int MaxM=;
  18. const int INF=10e9+;
  19.  
  20. struct Node
  21. {
  22. int id,val;
  23.  
  24. Node(int _id=,int _val=):id(_id),val(_val) {}
  25.  
  26. bool operator < (const Node & a) const
  27. {
  28. return val>a.val;
  29. }
  30. };
  31.  
  32. struct Edge
  33. {
  34. int to,next,cost;
  35. };
  36.  
  37. Edge E[MaxM];
  38. int head[MaxN],Ecou;
  39. int vis[MaxN];
  40.  
  41. void Dijkstra(int lowcost[],int N,int start)
  42. {
  43. priority_queue <Node> que;
  44. Node temp;
  45. int u,v,c;
  46.  
  47. for(int i=;i<=N;++i)
  48. {
  49. lowcost[i]=INF;
  50. vis[i]=;
  51. }
  52.  
  53. que.push(Node(start,));
  54. lowcost[start]=;
  55.  
  56. while(!que.empty())
  57. {
  58. temp=que.top();
  59. que.pop();
  60.  
  61. u=temp.id;
  62.  
  63. if(vis[u])
  64. continue;
  65.  
  66. vis[u]=;
  67.  
  68. for(int i=head[u];i!=-;i=E[i].next)
  69. {
  70. v=E[i].to;
  71. c=E[i].cost;
  72.  
  73. if(lowcost[v]>lowcost[u]+c && !vis[v])
  74. {
  75. lowcost[v]=lowcost[u]+c;
  76. que.push(Node(v,lowcost[v]));
  77. }
  78. }
  79. }
  80. }
  81.  
  82. void init(int N)
  83. {
  84. for(int i=;i<=N;++i)
  85. head[i]=-;
  86. Ecou=;
  87. }
  88.  
  89. void addEdge(int u,int v,int c)
  90. {
  91. E[Ecou].to=v;
  92. E[Ecou].cost=c;
  93. E[Ecou].next=head[u];
  94. head[u]=Ecou++;
  95. }
  96.  
  97. int ans[MaxN];
  98.  
  99. int main()
  100. {
  101. //freopen("in.txt","r",stdin);
  102. //freopen("out.txt","w",stdout);
  103.  
  104. int N,M;
  105. int a,b,c;
  106.  
  107. scanf("%d %d",&N,&M);
  108.  
  109. init(N);
  110.  
  111. while(M--)
  112. {
  113. scanf("%d %d %d",&a,&b,&c);
  114. addEdge(a,b,c);
  115. }
  116.  
  117. Dijkstra(ans,N,);
  118.  
  119. printf("%d\n",ans[N]);
  120.  
  121. return ;
  122. }

(简单) POJ 3159 Candies,Dijkstra+差分约束。的更多相关文章

  1. POJ 3159 Candies 【差分约束+Dijkstra】

    <题目链接> 题目大意: 给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的糖果数<= c .最后求n 比 1 ...

  2. POJ 3159 Candies(差分约束,最短路)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 20067   Accepted: 5293 Descrip ...

  3. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  4. POJ 3159 Candies(差分约束+最短路)题解

    题意:给a b c要求,b拿的比a拿的多但是不超过c,问你所有人最多差多少 思路:在最短路专题应该能看出来是差分约束,条件是b - a <= c,也就是满足b <= a + c,和spfa ...

  5. POJ 3159 Candies(差分约束)

    http://poj.org/problem?id=3159 题意:有向图,第一行n是点数,m是边数,每一行有三个数,前两个是有向边的起点与终点,最后一个是权值,求从1到n的最短路径. 思路:这个题让 ...

  6. poj 3159 Candies (差分约束)

    一个叫差分约束系统的东西.如果每个点定义一个顶标x(v),x(t)-x(s)将对应着s-t的最短路径. 比如说w+a≤b,那么可以画一条a到b的有向边,权值为w,同样地给出b+w2≤c,a+w3≤c. ...

  7. POJ 3159 Candies 还是差分约束(栈的SPFA)

    http://poj.org/problem?id=3159 题目大意: n个小朋友分糖果,你要满足他们的要求(a b x 意思为b不能超过a x个糖果)并且编号1和n的糖果差距要最大. 思路: 嗯, ...

  8. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  9. poj 3159 Candies dijkstra + queue

    题目链接: http://poj.org/searchproblem 题目大意: 飞天鼠是班长,一天班主任买了一大包糖果,要飞天鼠分发给大家,班里面有n个人,但是学生A认为学生B比自己多的糖果数目不应 ...

随机推荐

  1. 【SQL】SQL

    SQL基础 本文参照:http://www.w3school.com.cn/sql/ SQL 结构化查询语言(Structured Query Language). 对于大小写不敏感. SQL 使用单 ...

  2. 【prim + kruscal 】 最小生成树模板

    来源:dlut oj 1105: Zhuo’s Dream Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 40 Solved: 14[Submit][St ...

  3. 两个数组各个数相加或相乘变成一个矩阵求第K大

    input 1<=T<=20 1<=n<=100000,1<=k<=n*n a1 a2 ... an 0<ai<=10000 b1 b2 ... bn ...

  4. dirname(_file_) DIRECTORY_SEPARATOR

    <?php echo __FILE__ ; // 取得当前文件的绝对地址,结果:D:\www\test.php echo dirname(__FILE__); // 取得当前文件所在的绝对目录, ...

  5. 华哥倒酒<区间标记,二分>

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; t ...

  6. GameUnity 2.0 文档(二) 纸片人系统

    本想快速的 把 之前写的类库,一股脑的 给大家 ,但又觉得,如 msdn那样的 文档,并不能给 初学者 所能接受. 因为 大部分人 对 api 还是比较陌生,也不愿意 去研究和组合. 那么 今天我选用 ...

  7. java 子类与父类继承关系

    People.java public class People { int age,leg = 2,hand = 2; protected void showPeopleMess() { System ...

  8. Android 学习 之 无需类名启动其他程序

    在网上搜索了一会相关的实现代码,发现所有的文章都说是需要包名和类名.但是人家的程序,我们怎么可能知道哪个是第一个启动的Activity?所以,真正用在项目上,那种方法基本上没什么用的.于是查看官方文档 ...

  9. JS页面延迟执行一些方法(整理)

    一般在JS页面延迟执行一些方法.可以使用以下的方法 jQuery.delay()方法简介 http://shawphy.com/2010/11/jquery-delay.html jQuery中que ...

  10. 安卓开发之探秘蓝牙隐藏API(转)

    源:http://www.cnblogs.com/xiaochao1234/p/3793172.html 上次讲解Android的蓝牙基本用法,这次讲得深入些,探讨下蓝牙方面的隐藏API.用过Andr ...