hdu 1217 spfa最短路径

  1. #include <stdio.h>
  2. #include <queue>
  3. #include <iostream>
  4. #include <map>
  5. #include <string>
  6. using namespace std;
  7. #define INF 0xfffff //因为为了辨别是否有负权,所以INF不能开太大
  8. #define MAX 1100
  9. float dist[MAX], pre[MAX], path[MAX][MAX];
  10. bool sign[MAX];
  11. void initialize(int n) //初始化
  12. {
  13. for(int i=1; i<=n; i++)
  14. {
  15. {
  16. //pre[i] = 0;
  17. dist[i] = 0; //将距离开始全变为最大
  18. sign[i] = false;
  19. }
  20. for(int j=1; j<=n; j++)
  21. if(i == j) path[i][j] = 1.0; //图初始
  22. else path[i][j] = 0;
  23. }
  24. }
  25. bool spfa(int n, int start) //无法计算负权
  26. {
  27. /* for (int i=1; i<=n; ++i)//初始化
  28. {
  29. dist[i] = INF;
  30. sign[i] = false;
  31. }*/
  32. queue<int> Q;
  33. dist[start] = 1.0;
  34. sign[start] = true;
  35. Q.push(start);
  36. while (!Q.empty())
  37. {
  38. int temp = Q.front();
  39. Q.pop();
  40. sign[temp] = false;
  41. for (int i=1; i<=n; ++i)
  42. {
  43. if (dist[temp] * path[temp][i] > dist[i])//存在负权的话,就需要创建一个COUNT数组,当某点的入队次数超过V(顶点数)返回。
  44. {
  45. dist[i] = dist[temp] * path[temp][i];
  46. if (!sign[i])
  47. {
  48. Q.push(i);
  49. sign[i] = true;
  50. }
  51. if( dist[start] > 1.0)
  52. return true;
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. void input(int line)
  59. {
  60. map<string, int>list;
  61. for(int i=1; i<=line; i++)
  62. {
  63. string temp; cin >> temp;
  64. list[temp] = i;
  65. }
  66. int count;
  67. scanf("%d", &count);
  68. string a, b;
  69. float weight;
  70. for(int i=0; i<count; i++)
  71. {
  72. cin >> a >> weight >> b;
  73. //if(path[list[a]][list[b]] > weight) //有多条路,保存最短的那条
  74. {
  75. path[list[a]][list[b]] = weight;
  76. //path[list[b]][list[a]] = weight; //无向图双向
  77. }
  78. }
  79. }
  80. int main()
  81. {
  82. int n, T=0;
  83. while(~scanf("%d", &n) && n )
  84. {
  85. initialize(n);
  86. input(n);
  87. int flag =1;
  88. for(int i=1; i<=n; i++)
  89. {
  90. if(spfa(n, i) )
  91. {printf("Case %d: Yes\n", ++T); flag = 0; break;}
  92. }
  93. if(flag) {printf("Case %d: No\n", ++T); }
  94. }
  95. return 0;
  96. }

来自为知笔记(Wiz)

附件列表

时间: 2024-10-12 15:58:17

hdu 1217 spfa最短路径的相关文章

hdu 1217 Arbitrage Floyd||SPFA

转载请注明出处:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1

hdu 1217 Arbitrage (spfa算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个STL中的map容器去处理数据,map<string,int>V,M; 现在我目前的理解是将字符串转换成数字,然后就是根据spfa的模板找最短路了..哇哈哈( ⊙o⊙ )哇 1 #include <iostream> 2 #include <cstdio> 3 #include

HDU 2112 HDU Today,最短路径算法,Dijkstra

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13396    Accepted Submission(s): 3144 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HD

hdu 1217 (Floyd变形)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4430    Accepted Submission(s): 2013 Problem Description Arbitrage is the use of discr

hdu 2833 WuKong(最短路径+记忆化搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=2833 大致题意:给定一个无向图,以及悟空和师傅起点与终点,求它们分别从起点到终点的最短路径中经过相同的点的最大个数. 思路:首先dijkstra求出最短路,那么如果有dis[a] + map[a][b] = dis[b],则边(a,b)一定在最短路径上.根据这一定理可以求出所有最短路径.然后类似于求最长公共子序列求经过的相同点的最大个数. 即若a==b ,dp[a][b] = max(dp[i][j]+1)

HDU 1847(SPFA)

高仿代码: #include <iostream>#include <string.h>#include <queue>#include <vector>#include <utility>#include <cstdio>using namespace std;#define N 205#define M 2005const int inf = 0x3f3f3f3f;int v[M],w[M],next[M],first[N],d[

hdu 4568 spfa 最短路算法+旅行商问题

http://acm.hdu.edu.cn/showproblem.php?pid=4568 Problem Description One day, a hunter named James went to a mysterious area to find the treasures. James wanted to research the area and brought all treasures that he could. The area can be represented a

[ACM] hdu 1217 Arbitrage (bellman_ford最短路,判断是否有正权回路或Floyed)

Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British p

poj 3259Wormholes (spfa最短路径)

#include<stdio.h> #include<string.h> #include<limits.h> #include<queue> using namespace std; #define N 5505 #define M 55000//注意边和点集的数组大小 struct edge { int to,value,next; }edges[M]; int heads[N],len=0; int addedge(int u,int v,int w)