题目描述
Bessie has been hired to build a cheap internet network among Farmer John’s N (2 <= N <= 1,000) barns that are conveniently numbered 1…N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn’t even want to pay Bessie.

Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a “tree”.
输入

  • Line 1: Two space-separated integers: N and M

  • Lines 2…M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
    输出

  • Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
    样例输入 :
    5 8
    1 2 3
    1 3 7
    2 3 10
    2 4 4
    2 5 8
    3 4 6
    3 5 2
    4 5 17
    样例输出:
    42
    提示
    OUTPUT DETAILS:

The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3.

主要用到最大生成树

#include <stdio.h>
int e[1001][1001],dis[1001],vis[1001],sum, N,M;
void Prime()
{int max,k;for(int i=1;i<=N;i++){dis[i]=e[1][i];vis[i]=0;}dis[1]=0;vis[1]=1;for(int i=1;i<N;i++){k=-1;max=0;for(int j=1;j<=N;j++){if(vis[j]==0&&dis[j]>max)max=dis[k=j];}if(k==-1){printf("-1\n");return;}vis[k]=1;sum+=max;for(int j=1;j<=N;j++){if(vis[j]==0&&dis[j]<e[k][j])dis[j]=e[k][j];}}printf("%d\n",sum);
}int main()
{int a,b,c;scanf("%d%d",&N,&M);for(int i=1;i<=N;i++)for(int j=1;j<=N;j++){e[i][j]=0;}for(int i=0;i<M;i++){scanf("%d%d%d",&a,&b,&c);if(c>e[a][b])e[a][b]=e[b][a]=c;}Prime();return 0;
}

OJ:Bad Cowtractors(最大生成树)相关推荐

  1. 杭电oj题目题型分类(转)

    1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 Hero In Maze 广度搜索 ...

  2. 杭电OJ题目分类(转载)

    1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 Hero In Maze 广度搜索 ...

  3. HD OJ 题目分类

    动态规划:2037 2054 2084 hdu题目分类(转) http://blog.csdn.net/vsooda/article/details/7258791 1001 整数求和 水题 1002 ...

  4. HDOJ题目分类大全

    版权声明:本文为博主原创文章,欢迎转载,转载请注明本文链接! https://blog.csdn.net/qq_38238041/article/details/78178043 杭电里面有很多题目, ...

  5. HDU题目分类大全【大集合】

    基础题: 1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.  1032.1037.1040.1048.1056.105 ...

  6. hdoj杭电问题分类

    杭电上的题虽然多,但是一直苦于找不到问题分类,网页都是英文的,所以平时做题也没怎么看,今天仔细一看,问题分类竟然就在主页....做了那么久的题居然没发现,表示已经狗带..不要笑,不知道有没有像我一样傻 ...

  7. 转载:Hdu 题目分类

    原址点击 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056. ...

  8. hdu与poj题目分类

    POJ 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(po ...

  9. hdu HDOJ 题目分类(转)

    动态规划:2037 2054 2084 hdu题目分类(转) http://apps.hi.baidu.com/share/detail/17053154 1001 整数求和 水题 1002 C语言实 ...

  10. HDOJ ACM 题目

    转载 HDOJ 题目分类(转) 1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 H ...

最新文章

  1. linux apt-get proxy
  2. WPF中自定义窗体标题栏
  3. mysql dns反向解析_Mysql DNS反向解析导致连接超时过程分析(skip-name-resolve)
  4. .NET的轻量级IOC框架芮双随笔
  5. lz0-007 读书笔记11
  6. c语言实验题水仙花数5359,《C语言程序设计》实验报告(实验1-12).doc
  7. layUI日期格式转换
  8. word 转 PDF时报错
  9. Linux系统软件安装
  10. HTML语言标记详解
  11. uni-app左右平分九宫格样式
  12. 首屈一指的全球招聘与薪资支付平台Deel收购Zeitgold,以增强薪资和人工智能服务
  13. abaqus的python安装文件在哪_python - 在ABAQUS 6.14 python环境中安装熊猫 - 堆栈内存溢出...
  14. 【GD32L233C-START】1、初识GD32L233C-START板卡
  15. 海龟交易法则 matlab,【干货】经典的期货策略——海龟交易法则(二)
  16. 指尖江湖李忘生鸿蒙初开,剑网3指尖江湖李忘生技能搭配攻略
  17. 如何使用文献管理软件EndNote x9 x9.1高效管理科研相关参考文献 相关论文
  18. EDA课设:CPU设计
  19. 合并财务报表及其合并范围的确定
  20. php百度快照劫持,discuz百度快照被劫持的解决办法

热门文章

  1. 机器学习笔记 - 特殊类型的矩阵和向量
  2. RabbitMQ学习笔记:内存(Memory)|磁盘空间(Disk space)阀值
  3. 如何用matlab画北极熊,中班幼儿美术画动物:水彩画北极熊(步骤图解)
  4. Vue 获取 DOM--实例,页面一打开被focus
  5. Koa2框架的原理解析和实现
  6. 微信小程序使用canvas制作拼图动画
  7. Gershgorin圆盘定理
  8. 性能测试工具Locust和JMeter比较
  9. #C语言-基础篇(Ⅳ)
  10. Android手机输入法按键监听-dispatchKeyEvent