hdu 1217 dijkstra

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. using namespace std;
  6. #define INF 0xfffff
  7. #define MAX 1100
  8. float dist[MAX], path[MAX][MAX];
  9. bool sign[MAX];
  10. /* 注意相应权值不能为负,且时间复杂度较高 */
  11. /*算法步骤如下:
  12. 1. 初始时令 S={V0},T={其余顶点},T中顶点对应的距离值
  13. 若存在<V0,Vi>,d(V0,Vi)为<V0,Vi>弧上的权值
  14. 若不存在<V0,Vi>,d(V0,Vi)为∞
  15. 2. 从T中选取一个其距离值为最小的顶点W且不在S中,加入S
  16. 3. 对其余T中顶点的距离值进行修改:若加进W作中间顶点,从V0到Vi的距离值缩短,则修改此距离值
  17. 重复上述步骤2、3,直到S中包含所有顶点,即W=Vi为止
  18. */
  19. void initialize(int n) //初始化
  20. {
  21. for(int i=1; i<=n; i++)
  22. {
  23. {
  24. //pre[i] = 0;
  25. dist[i] = 0; //将距离开始全变为最大
  26. sign[i] = false;
  27. }
  28. for(int j=1; j<=n; j++)
  29. if(i == j) path[i][j] = 1.0; //图初始
  30. else path[i][j] = 0;
  31. }
  32. }
  33. bool dijkstra(int n, int source )
  34. {
  35. for(int i=1; i<=n; i++)
  36. {
  37. dist[i] = path[source][i]; //将与源点有关的点的距离加入dist
  38. sign[i] = false;
  39. //if(dist[i] == INF) //确定有关系的点的前驱,无则为0
  40. //pre[i] = 0;
  41. //else
  42. // pre[i] = source;
  43. }
  44. dist[source] = 1.0; //源点自身长度为0
  45. sign[source] = 1;
  46. /*
  47. 依次将未放入sign集合的结点中,取dist[]最小值的结点,放入结合sign中
  48. 一旦sign包含了所有n中顶点,dist就记录了从源点到所有其他顶点之间的最短路径长度
  49. */
  50. for(int i=2; i<=n; i++)
  51. {
  52. //int min = INF;
  53. int current = source;
  54. for(int j=1; j<=n; j++) //找出当前未使用点j的dist[j]的最小值
  55. {
  56. if( (!sign[j]) && dist[j] * path[current][j] > dist[j] )
  57. {current = j; /*min = dist[j];*/}
  58. }
  59. sign[current] = true; //表示当前点最短距离已经找到
  60. if( dist[source] > 1.0) return true;
  61. for(int j=1; j<=n; j++) //更新当前点到未找到点的距离
  62. //if( (!sign[j]) && path[current][j] < INF)
  63. {
  64. int newdist = dist[current] * path[current][j];
  65. if(newdist > dist[j] )
  66. {dist[j] = newdist; /*pre[j] = current;*/}
  67. }
  68. }return false;
  69. }
  70. /*void search_path(int n, int start, int end)
  71. {
  72. int road[MAX];
  73. int total = 1;
  74. road[total++] = end; //从后向前查找
  75. int current = pre[end]; //路径存在pre中
  76. while( current != start) //递归查找,类似并查集
  77. {
  78. road[total++] = current;
  79. current = pre[current];
  80. }
  81. road[total] = start; //最后的开始点存入
  82. for(int i=total; i>=1; i--) //输出
  83. {
  84. if( i!=1)
  85. printf("%d ->", road[i]);
  86. else
  87. printf("%d\n", road[i]);
  88. }
  89. }*/
  90. void input(int line)
  91. {
  92. map<string, int>list;
  93. for(int i=1; i<=line; i++)
  94. {
  95. string temp; cin >> temp;
  96. list[temp] = i;
  97. }
  98. int count;
  99. scanf("%d", &count);
  100. string a, b;
  101. float weight;
  102. for(int i=0; i<count; i++)
  103. {
  104. cin >> a >> weight >> b;
  105. //if(path[list[a]][list[b]] > weight) //有多条路,保存最短的那条
  106. {
  107. path[list[a]][list[b]] = weight;
  108. //path[list[b]][list[a]] = weight; //无向图双向
  109. }
  110. }
  111. }
  112. int main()
  113. {
  114. int n, T=0;
  115. while(~scanf("%d", &n) && n )
  116. {
  117. initialize(n);
  118. input(n);
  119. int flag =1;
  120. for(int i=1; i<=n; i++)
  121. {
  122. if(dijkstra(n, i) )
  123. {printf("Case %d: Yes\n", ++T); flag = 0; break;}
  124. }
  125. if(flag) {printf("Case %d: No\n", ++T); }
  126. }
  127. return 0;
  128. }

来自为知笔记(Wiz)

附件列表

时间: 2024-11-14 12:29:34

hdu 1217 dijkstra的相关文章

hdu 1548 (dijkstra解法)(一次AC就是爽)

恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部) <BestCoder用户手册>下载 A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11670    Accepted Submission(s): 4430 Problem Description There is

HDU 2112 HDU Today (Dijkstra算法)

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13952    Accepted Submission(s): 3264 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

hdu 2680 (Dijkstra)

快放暑假了,训练又要开始了.先从熟悉的图论开始做吧. 题意:一张有向图中有若干起点一个终点,让你算最短路,方法很简单只需人为加一个起点指向所有起点让后距离为0即可. 代码如下: 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <set> 5 #include <iostream> 6 #include <queue> 7 #inc

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

[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

HDU 1217 Arbitrage 【最短路,map+spfa】

Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6985    Accepted Submission(s): 3212 Problem Description Arbitrage is the use of discrepancies in currency exchange rates to transform

HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 BritishPound BritishPound 10.0 FrenchFranc FrenchFranc 0.21 USDollar 可以通过US->Br->French->US这样转换,把1美元变成1*0.5*10*0.21=1.05美元赚取%5的利润. 解题思路:其实就相当于bellman-