好题。这题可以有三种解法:1.Dijkstra   2.优先队列   3.并查集

我这里是优先队列的实现,以后有时间再用另两种方法做做。。方法就是每次都选当前节点所连的权值最大的边,然后BFS搜索。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <string>
  8. #include <vector>
  9. #include <map>
  10. #include <queue>
  11. #include <functional>
  12. using namespace std;
  13. #define N 100007
  14.  
  15. struct node
  16. {
  17. int ind,wt;
  18. bool operator < (const node &a)const
  19. {
  20. return wt<a.wt;
  21. }
  22. };
  23. int start,endi,n,m,res;
  24. int way[][],vis[][];
  25. map<string,int> mp;
  26. priority_queue<node> que;
  27.  
  28. void BFS()
  29. {
  30. memset(vis,,sizeof(vis));
  31. int i,maxi = ,id;
  32. node now,next;
  33. for(i=;i<=n;i++)
  34. {
  35. if(way[start][i] > maxi)
  36. {
  37. maxi = way[start][i];
  38. id = i;
  39. }
  40. }
  41. now.ind = id;
  42. now.wt = maxi;
  43. que.push(now);
  44. //printf("%d %d\n",start,endi);
  45. //printf("%d %d\n",now.ind,now.wt);
  46. res = ;
  47. while(!que.empty())
  48. {
  49. now = que.top();
  50. que.pop();
  51. if(now.ind == endi)
  52. {
  53. if(now.wt > res)
  54. res = now.wt;
  55. while(!que.empty())
  56. que.pop();
  57. return;
  58. }
  59. for(i=;i<=n;i++)
  60. {
  61. if(way[now.ind][i] && !vis[now.ind][i])
  62. {
  63. vis[now.ind][i] = vis[i][now.ind] = ;
  64. next.ind = i;
  65. next.wt = min(now.wt,way[now.ind][i]);
  66. que.push(next);
  67. //printf("%d %d\n",next.ind,next.wt);
  68. }
  69. }
  70. }
  71. }
  72.  
  73. int main()
  74. {
  75. int cs = ,i,j,w,num;
  76. string city1,city2;
  77. node ka,kb;
  78. while(scanf("%d%d",&n,&m)!=EOF && (n||m))
  79. {
  80. mp.clear();
  81. num = ;
  82. memset(way,,sizeof(way));
  83. for(i=;i<m;i++)
  84. {
  85. cin>>city1;
  86. cin>>city2;
  87. scanf("%d",&w);
  88. if(!mp[city1])
  89. mp[city1] = num++;
  90. if(!mp[city2])
  91. mp[city2] = num++;
  92. way[mp[city1]][mp[city2]] = w;
  93. way[mp[city2]][mp[city1]] = w;
  94. }
  95. cin>>city1>>city2;
  96. start = mp[city1];
  97. endi = mp[city2];
  98. BFS();
  99. printf("Scenario #%d\n",cs++);
  100. printf("%d tons\n\n",res);
  101. }
  102. return ;
  103. }

POJ 2263 Heavy Cargo 多种解法的更多相关文章

  1. POJ 2263 Heavy Cargo(Floyd + map)

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

  2. POJ 2263 Heavy Cargo(ZOJ 1952)

    最短路变形或最大生成树变形. 问 目标两地之间能通过的小重量. 用最短路把初始赋为INF.其它为0.然后找 dis[v]=min(dis[u], d); 生成树就是把最大生成树找出来.直到出发和终点能 ...

  3. POJ2263 Heavy Cargo

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

  4. poj 1797 Heavy Transportation(最大生成树)

    poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...

  5. 多种解法解决n皇后问题

    多种解法解决n皇后问题 0x1 目的 ​ 深入掌握栈应用的算法和设计 0x2 内容 ​ 编写一个程序exp3-8.cpp求解n皇后问题. 0x3 问题描述 即在n×n的方格棋盘上,放置n个皇后,要求每 ...

  6. 【BZOJ4555】求和(多种解法混合版本)

    [BZOJ4555]求和(多种解法混合版本) 题面 BZOJ 给定\(n\),求 \[f(n)=\sum_{i=0}^{n}\sum_{j=0}^{i}S(i,j)\times 2^j \times ...

  7. POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...

  8. POJ.1797 Heavy Transportation (Dijkstra变形)

    POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...

  9. 巴塞尔问题(Basel problem)的多种解法

    巴塞尔问题(Basel problem)的多种解法——怎么计算\frac{1}{1^2}+\frac{1}{2^2}+\frac{1}{3^2}+\cdots112+122+132+⋯ ? (PS:本 ...

随机推荐

  1. (旧)子数涵数·C语言——让C帮你做计算

    之前,我们学过了我们的第一个C程序--hello World.现在开始进一步学习,想一想如何让C帮你做计算. 我们先来看代码(我没有新建,还是用之前的hello world.cpp): 好,因为之前在 ...

  2. 多准则决策模型-TOPSIS方法

    多准则决策–Multiple Criteria Decision Making 多准则决策–Multiple Criteria Decision Making 多准则决策是指在具有相互冲突.不可共度的 ...

  3. ASP.NET Web API 通过Authentication特性来实现身份认证

    using System; using System.Collections.Generic; using System.Net.Http.Headers; using System.Security ...

  4. C++模板元编程

    ABC

  5. CSS之绝对定位那些事

    1.垂直居中 有时我们会使用margin: 0 auto;作居中使用.但有的时候我们需要垂直居中,例如在div里面垂直居中显示一张加载中的gif图. 下面这种写法就可以完美实现: 垂直居中的子容器 { ...

  6. 访问SAP的Domain的Value Range

    访问Domain的Value Range有两种方法: 1.直接访问表 dd07l和dd07T     select * from dd07l            where domname   = ...

  7. Android根据APP包名启动应用

    public void openApp(String packageName, Context context) { PackageManager packageManager = context.g ...

  8. 实战1--应用EL表达式访问JavaBean的属性

    (1)编写index.jsp页面,用来收集用户的注册信息 <%@ page language="java" pageEncoding="GBK"%> ...

  9. wxPython简单入门

    wxPython简介 wxPython 是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的.功能键全的  GUI 用户界面. wxPython 是作为优秀 ...

  10. 【读书笔记】iOS-NSDictionary与NSArray的比较

    有时候为什么不用数组存储然后在数组里查询数值呢?字典(也称为散列表或关联数组)使用的是键查询的优化存储方式.它可以立即找出要查询的数据,而不需要遍历整个数组进行查找.对于频繁的查询和大型的数据集来说, ...