这道题数据范围小,方法比较多。我用floyd和spfa分别写了一下,spfa明显有时间优势。

一个小技巧在于:把城市名称对应到数字序号,处理是用数字。

方法一:spfa

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<cmath>
  7. #include<map>
  8. #include<set>
  9. #include<vector>
  10. #include<algorithm>
  11. #include<stack>
  12. #include<queue>
  13. #include<cctype>
  14. #include<sstream>
  15. using namespace std;
  16. #define pii pair<int,int>
  17. #define LL long long int
  18. const int eps=1e-;
  19. const int INF=;
  20. const int maxn=+;
  21. int n,r,cas=,ton,num,used[maxn],d[maxn],m[maxn][maxn];
  22. char city[maxn][];
  23. char s[],e[];
  24. vector<int>v[maxn];
  25. int index(char *ss)
  26. {
  27. int i;
  28. for(i=;i<num;i++)
  29. {
  30. if(strcmp(ss,city[i])==)
  31. {
  32. return i;
  33. }
  34. }
  35. strcpy(city[num++],ss);
  36. return i;
  37. }
  38. void ini()
  39. {
  40. num=;
  41. for(int i=;i<n;i++)
  42. {
  43. strcpy(city[i],"\0");
  44. v[i].clear();
  45. used[i]=;
  46. d[i]=INF;
  47. }
  48. }
  49. int spfa(int s,int e)
  50. {
  51. queue<int>q;
  52. q.push(s);
  53. used[s]=;
  54. while(!q.empty())
  55. {
  56. int t=q.front();
  57. used[t]=;
  58. q.pop();
  59. int siz=v[t].size();
  60. for(int i=;i<siz;i++)
  61. {
  62. int k=v[t][i];
  63. /*if(used[k]==0) {q.push(k);used[k]=1;}
  64. if(d[k]<INF) d[k]=max(d[k],min(d[t],m[t][k]));
  65. else d[k]=min(d[t],m[t][k]);*/
  66. /*上面注释掉的这种写法不对,一旦有环就会陷入死循环(即使这题不会有
  67. 负边)。还是对spfa理解的不够。需要更新的点才要考虑入队的问题,不需要
  68. 更新的点肯定不入队,最后所有点都不用再更新了队列为空退出循环。而不是
  69. 遇到一个点就入队。*/
  70. if(d[k]==INF)
  71. {
  72. d[k]=min(d[t],m[t][k]);
  73. q.push(k);used[k]=;
  74. }
  75. else if(min(d[t],m[t][k])>d[k])
  76. {
  77. d[k]=min(d[t],m[t][k]);
  78. if(used[k]==) {q.push(k);used[k]=;}
  79. }
  80. }
  81. }
  82. return d[e];
  83. }
  84. int main()
  85. {
  86. //freopen("in8.txt","r",stdin);
  87. //freopen("out.txt","w",stdout);
  88. while(scanf("%d%d",&n,&r)==)
  89. {
  90. if(n==&&r==) break;
  91. printf("Scenario #%d\n",++cas);
  92. ini();
  93. for(int i=;i<r;i++)
  94. {
  95. scanf("%s%s%d",s,e,&ton);
  96. int t1=index(s);
  97. int t2=index(e);
  98. v[t1].push_back(t2);
  99. v[t2].push_back(t1);
  100. m[t1][t2]=m[t2][t1]=ton;
  101. }
  102. scanf("%s%s",s,e);
  103. int t1=index(s);
  104. int t2=index(e);
  105. printf("%d tons\n",spfa(t1,t2));
  106. printf("\n");
  107. }
  108. //fclose(stdin);
  109. //fclose(stdout);
  110. return ;
  111. }

spfa

方法二:floyd

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<cmath>
  7. #include<map>
  8. #include<set>
  9. #include<vector>
  10. #include<algorithm>
  11. #include<stack>
  12. #include<queue>
  13. #include<cctype>
  14. #include<sstream>
  15. using namespace std;
  16. #define pii pair<int,int>
  17. #define LL long long int
  18. const int eps=1e-;
  19. const int INF=;
  20. const int maxn=+;
  21. int n,r,m[maxn][maxn],cas=,ton,num;
  22. char city[maxn][];
  23. char s[],e[];
  24. int index(char *ss)
  25. {
  26. int i;
  27. for(i=;i<num;i++)
  28. {
  29. if(strcmp(ss,city[i])==)
  30. {
  31. return i;
  32. }
  33. }
  34. strcpy(city[num++],ss);
  35. return i;
  36. }
  37. void floyd()
  38. {
  39. for(int k=;k<n;k++)
  40. {
  41. for(int i=;i<n;i++)
  42. {
  43. for(int j=;j<n;j++)
  44. {
  45. m[i][j]=max(m[i][j],min(m[i][k],m[k][j]));
  46. }
  47. }
  48. }
  49. }
  50. void ini()
  51. {
  52. num=;
  53. for(int i=;i<n;i++)
  54. {
  55. strcpy(city[i],"\0");
  56. for(int j=;j<n;j++)
  57. {
  58. if(i==j) m[i][j]=INF;
  59. else m[i][j]=;
  60. }
  61. }
  62. }
  63. int main()
  64. {
  65. //freopen("in8.txt","r",stdin);
  66. //freopen("out.txt","w",stdout);
  67. while(scanf("%d%d",&n,&r)==)
  68. {
  69. if(n==&&r==) break;
  70. printf("Scenario #%d\n",++cas);
  71. ini();
  72. for(int i=;i<r;i++)
  73. {
  74. scanf("%s%s%d",s,e,&ton);
  75. int t1=index(s);
  76. int t2=index(e);
  77. m[t1][t2]=m[t2][t1]=ton;
  78. }
  79. floyd();
  80. scanf("%s%s",s,e);
  81. int t1=index(s);
  82. int t2=index(e);
  83. printf("%d tons\n",m[t1][t2]);
  84. printf("\n");
  85. }
  86. //fclose(stdin);
  87. //fclose(stdout);
  88. return ;
  89. }

floyd

poj2263 zoj1952 Heavy Cargo(floyd||spfa)的更多相关文章

  1. POJ2263 Heavy Cargo

    Heavy Cargo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4004   Accepted: 2124 Descr ...

  2. POJ 2263 Heavy Cargo(Floyd + map)

    Heavy Cargo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3768   Accepted: 2013 Descr ...

  3. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

  4. Heavy Cargo POJ 2263 (Floyd传递闭包)

    Description Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their lat ...

  5. poj 1847( floyd && spfa )

    http://poj.org/problem?id=1847 一个水题,用来熟悉熟悉spfa和floyd的. 题意:有m条的铁路,要从x,到y, 之后分别就是条铁路与其他铁路的交点.第一个输入的为有n ...

  6. K - Heavy Cargo dijkstar

    来源poj2263 Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their lates ...

  7. poj1847 Tram(Dijkstra || Floyd || SPFA)

    题目链接 http://poj.org/problem?id=1847 题意 有n个车站,编号1~n,每个车站有k个出口,车站的出口默认是k个出口中的第一个,如果不想从默认出口出站,则需要手动选择出站 ...

  8. hdoj2544 最短路(Dijkstra || Floyd || SPFA)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路 最短路算法模板题,求解使用的Dijkstra算法.Floyd算法.SPFA算法可以当做求解 ...

  9. [APIO2017]商旅——分数优化+floyd+SPFA判负环+二分答案

    题目链接: [APIO2017]商旅 枚举任意两个点$(s,t)$,求出在$s$买入一个物品并在$t$卖出的最大收益. 新建一条从$s$到$t$的边,边权为最大收益,长度为原图从$s$到$t$的最短路 ...

随机推荐

  1. ModelSim之TCL仿真

    在使用ModelSim时,我们一般都是从界面UI进行操作的,这样也比较直观易学.但是在很多的调试时,发现很多操作都是重复的,修改一下代码就要再次进行相关操作,这样很没有效率.其实,ModelSim是可 ...

  2. mysql数据库补充知识2 查询数据库记录信息之单表查询

    一 单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field LIMIT 限制条数 二 关键 ...

  3. 剑指offer 面试62题

    面试62题: 题目:圆圈中最后剩下的数字 题:0,1,...,n-1这n个数字排成一个圆圈,从数字0开始,每次从这个圆圈里删除第m个数字.求出这个圆圈里剩下的最后一个数字. 解题思路:约瑟夫环问题,可 ...

  4. LDAP注入

    理解LDAP与LDAP注入 0x01 LDAP简介 查阅了一些网上的资料,读了好久还是觉得理解起来很困难,感觉还是不够干,之后看到的一个博客http://www.chinaunix.net/old_j ...

  5. des加密——补齐

    下面这个网址(英文)介绍的比较全面. http://www.di-mgt.com.au/cryptopad.html

  6. jsp导出

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  7. 【leetcode刷题笔记】Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  8. Hearbeat 介绍

    Hearbeat 介绍 Linux-HA的全称是High-Availability Linux,它是一个开源项目,这个开源项目的目标是:通过社区开发者的共同努力,提供一个增强linux可靠性(reli ...

  9. 【P2422】良好的感觉(单调栈优化DP//奇怪的暴力)

    话说正解是单调栈优化DP,然而貌似根据某种玄学的推算,这个题暴力出解貌似也是可以的.首先,我们枚举所有的点作为最小点,然后横向展开,遇到更小的就停止...然后再操作一下,看上去时间O(N^2),然而由 ...

  10. sql server 2005 Express 下载

    简体中文版: SQL Server 2005 Express Edition 简体中文版 链接页面: http://www.microsoft.com/downloads/details.aspx?d ...