题目链接:http://poj.org/problem?id=3159

题意:给出m给 x 与y的关系。当中y的糖数不能比x的多c个。即y-x <= c  最后求fly[n]最多能比so[1]
多多少糖?

差分约束问题, 就是求1-n的最短路,  队列实现spfa
会超时了,改为栈实现,就可以

有负环时,用栈比队列快

数组开小了,不报RE,报超时 ,我晕

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <queue>
  6. #include <stack>
  7. #include <algorithm>
  8. const int N = 210;
  9. const int maxn = 30100;
  10. const int maxm = 200000;
  11. #define FOR(i,a,b) for(int i=a;i<b;i++)
  12. #define init(a) memset(a,0,sizeof(a))
  13. #define MIN INT_MIN
  14. #define MAX INT_MAX
  15. #define LL long long
  16. using namespace std;
  17. int max(int a,int b){if(a>b)return a; else return b;}
  18. int min(int a,int b){if(a<b)return a; else return b;}
  19. const int INF=0x3f3f3f3f;
  20. struct node
  21. {
  22. int v,w;
  23. int next;
  24. }edge[maxm];
  25. int head[maxn];
  26. bool vis[maxn];
  27. int dis[maxn];
  28. int cnt;
  29. void add(int a,int b,int w)//加边
  30. {
  31. edge[cnt].v=b;
  32. edge[cnt].w=w;
  33. edge[cnt].next=head[a];
  34. head[a]=cnt++;
  35. }
  36. void SPFA(int s,int n)
  37. {
  38. //stack<int>stk;
  39. int stk[100000];
  40. int top = 0;
  41.  
  42. //stk.push(s);
  43. stk[top++] = s;
  44. FOR(i,1,n+1)
  45. {dis[i] = INF;
  46. vis[i] = 0;
  47. }
  48. dis[s] = 0;
  49. vis[s] = 1;
  50. while(top!=0)
  51. {
  52. int u=stk[--top];
  53. //stk.pop();
  54. vis[u]=false;
  55. for(int i=head[u];i!=-1;i=edge[i].next)
  56. {
  57. int v=edge[i].v;
  58. if(dis[v]>dis[u]+edge[i].w)
  59. {
  60. dis[v]=dis[u]+edge[i].w;
  61. if(!vis[v])
  62. {
  63. vis[v]=true;
  64. stk[top++] = v;
  65. }
  66. }
  67. }
  68.  
  69. }
  70. }
  71. void initt()
  72. {
  73. cnt=0;
  74. memset(head,-1,sizeof(head));
  75. }
  76. int main()
  77. {
  78. int n,m;
  79. int a,b,c;
  80. while(scanf("%d%d",&n,&m)!=EOF)
  81. {
  82. initt();
  83. while(m--)
  84. {
  85. scanf("%d%d%d",&a,&b,&c);
  86. add(a,b,c);
  87. }
  88. SPFA(1,n);
  89. printf("%d\n",dis[n]);
  90. }
  91. return 0;
  92. }

POJ 3159 Candies(SPFA+栈)差分约束的更多相关文章

  1. (简单) POJ 3159 Candies,Dijkstra+差分约束。

    Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the he ...

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

    During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b ...

  3. POJ 3159 【朴素的差分约束】

    好吧终于知道什么是“高大上”的差分约束了.嗷嗷 题意: 小朋友们分糖果,某个小朋友不想另外一个小朋友分到的糖果数比自己多N块以上. 求编号为N的小朋友最多比编号为1的小朋友多分多少块糖果. 思路: 差 ...

  4. POJ 3159 Candies (栈优化spfa)

    Candies 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/J Description During the kinderga ...

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

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

  6. POJ 3159 Candies (图论,差分约束系统,最短路)

    POJ 3159 Candies (图论,差分约束系统,最短路) Description During the kindergarten days, flymouse was the monitor ...

  7. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

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

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

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

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

随机推荐

  1. struts2类型转换与校验总结

    1.struts2的类型转换分为全部变量转变和局部变量转变. 2.struts2对8中常见的基本类型的属性变量,可以自动转换.如果是User对象,可以手动简历UserAction-coversion. ...

  2. mysql问题Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)的解决方法

    在mysql命令行界面输入 mysql> set old_passwords=0;mysql> SET PASSWORD FOR hhds_test=PASSWORD('hhds_test ...

  3. 干掉cmd:windows下使用linux命令行

    对于喜欢用命令行的朋友们,在windows下面使用cmd窗口是不是很不爽?复制不方便?不能随意放大缩小?如果需要多个控制台要多个窗口?....各种不爽 一.基础工具 如果你也不爽,那就对了,所以给大家 ...

  4. InfiniBand

    Mellanox InfiniBand卡线缆性能延迟性测试程序源码,C源码实现操作mysql库,实现简单的增删改查,代码当前用的是增插入20000条数据 具体见源码 #include <mysq ...

  5. CST和GMT时间的区别

    CST和GMT时间的区别 今天遇到一个奇怪的问题,在服务器端通过 c# 获取当前时间为 Fri Aug 28 09:37:46 CST 2009, 转化为 GMT时间为:28 Aug 2009 01: ...

  6. 【多线程】Java并发编程:Lock(转载)

    原文链接:http://www.cnblogs.com/dolphin0520/p/3923167.html Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized ...

  7. UVALive 5886 The Grille (模拟)

    The Grille 题目链接: http://acm.hust.edu.cn/vjudge/problem/26634 Description http://7xjob4.com1.z0.glb.c ...

  8. svn switch relocate用法

    svn info svn info 得到 Path: . Working Copy Root Path: /Users/chunhuizhao/phpworkspace/buptef_wxpay/tr ...

  9. 安装php5.5

    安装php5.5 ./configure --prefix=/usr/local/php5.5.14/ --with-apxs2=/usr/local/apache2.2.27/bin/apxs -- ...

  10. codeforces 601A The Two Routes(最短路 flody)

    A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...