解题思路:典型的Kruskal,不能用floyed(会超时),上代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. #define inf 0x3f3f3f3f
  6. const int maxn = ;
  7. int father[maxn];
  8.  
  9. struct node{
  10. int x, y, w;
  11. }p[maxn*maxn]; //第一次开maxn,RE了一次
  12.  
  13. int cmp(node A, node B)
  14. {
  15. return A.w > B.w; //权值从大到小排序
  16. }
  17.  
  18. int Find(int x)
  19. {
  20. return father[x] == x ? x : father[x] = Find(father[x]);
  21. }
  22.  
  23. void Union(int x, int y)
  24. {
  25. int rootx = Find(x);
  26. int rooty = Find(y);
  27. if(rootx != rooty) father[rootx] = rooty;
  28. return ;
  29. }
  30.  
  31. int main()
  32. {
  33. int t, n, m, kase = ;
  34. scanf("%d", &t);
  35. while(t--)
  36. {
  37. scanf("%d %d", &n, &m);
  38. for(int i = ; i <= n; i++) father[i] = i;
  39. for(int i = ; i <= m; i++)
  40. scanf("%d %d %d", &p[i].x, &p[i].y, &p[i].w);
  41. sort(p+, p++m, cmp); //注意这里是p+1开始
  42.  
  43. int ans = inf; //初始化ans为最大值
  44. for(int i = ; i <= m; i++)
  45. {
  46. int rootx = Find(p[i].x);
  47. int rooty = Find(p[i].y);
  48. if(rootx != rooty)
  49. {
  50. Union(rootx, rooty);
  51. //ans保存路径中最小的权值
  52. if(p[i].w < ans) ans = p[i].w;
  53. //如果1和n已经连接,则直接跳出
  54. if(Find() == Find(n)) break;
  55. }
  56. }
  57. //注意输出格式
  58. printf("Scenario #%d:\n%d\n\n", kase++, ans);
  59. }
  60. return ;
  61. }

POJ1797 Heavy Transportation的更多相关文章

  1. [poj1797]Heavy Transportation<最大生成树prim&kruskal>

    题目链接:http://poj.org/problem?id=1797 题意:给定n个点,m条边,每条边连接两点切有权值.求点1到点n的路径的上的最小边的值最大... 翻别人博客找到的题,方法挺多的, ...

  2. POJ--1797 Heavy Transportation (最短路)

    题目电波: POJ--1797 Heavy Transportation n点m条边, 求1到n最短边最大的路径的最短边长度 改进dijikstra,dist[i]数组保存源点到i点的最短边最大的路径 ...

  3. POJ1797 Heavy Transportation 【Dijkstra】

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted:  ...

  4. (Dijkstra) POJ1797 Heavy Transportation

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 53170   Accepted:  ...

  5. POJ1797 Heavy Transportation —— 最短路变形

    题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K T ...

  6. [POJ1797] Heavy Transportation(最大生成树 || 最短路变形)

    传送门 1.最大生成树 可以求出最大生成树,其中权值最小的边即为答案. 2.最短路 只需改变spfa里面的松弛操作就可以求出答案. ——代码 #include <queue> #inclu ...

  7. POJ 1797 Heavy Transportation (最大生成树)

    题目链接:POJ 1797 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter pro ...

  8. (POJ 1797) Heavy Transportation 最大生成树

    题目链接:http://poj.org/problem?id=1797 Description Background Hugo Heavy is happy. After the breakdown ...

  9. POJ1797 Heavy Transportation (堆优化的Dijkstra变形)

    Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...

随机推荐

  1. svn / git SourceTree

    开发使用SourceTree 忽略文件这块老弄错,这次专门博客一下,使用CocoaPods 开发项目, 忽略步骤如下:  忽略文件内容如下 *.xcworkspace xcuserdata *.loc ...

  2. Mysql5.7 用户与授权

    mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...

  3. Android 电池关机充电

    android 电池(一):锂电池基本原理篇 android 电池(二):android关机充电流程.充电画面显示 android 电池(三):android电池系统 android电池(四):电池 ...

  4. 20162326 齐力锋 2017-2018学期 Bag类的补写博客

    要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进行单元测试(JUnit), ...

  5. spring集成spring mvc 和hibernate详解

    1.配置IOC容器 <!-- 配置IOC容器 --> <context-param> <param-name>contextConfigLocation</p ...

  6. 制作基于Buildbot的自动化测试系统Docker镜像

    Buildbot in Docker 前言 最近使用Buildbot做了一个自动测试的框架,为了部署方便,可以把测试框架做成Docker镜像,方便部署.这里记录下过程,供大家参考. 项目介绍 项目是一 ...

  7. Java Swing窗体小工具实例 - 原创

    Java Swing窗体小工具实例 1.本地webserice发布,代码如下: 1.1 JdkWebService.java package server; import java.net.InetA ...

  8. 2017 湘潭邀请赛&JSCPC G&J

    训练的时候对G想了一个假算法..也有很大可能是写错了.. 下来一看别人的G 看起来很奇妙.. 开始把所有的左括号翻成右括号,然后cost*=-1 这样在优先队列中就是最优的 然后for每一段 如果前缀 ...

  9. javascript页面打印

    打印本身比较简单,但要考虑到具体的需求.比如 1. 多浏览器: if (isIE()) { //打印预览 WebBrowser1.execWB(7, 1); } else { window.print ...

  10. php将科学计算法得出的结果转换成原始数据

    由于php最大只支持显示 15位因的数据运算,大于15位的2数加减乘除的数据的结果,会直接用科学计数法显示, 但在现实生活中,科学计数法不利于普通人识别,所以,本函数将:科学计数法的出的结果转换成原始 ...