OJ:Bad Cowtractors(最大生成树)
题目描述
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(最大生成树)相关推荐
- 杭电oj题目题型分类(转)
1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 Hero In Maze 广度搜索 ...
- 杭电OJ题目分类(转载)
1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 Hero In Maze 广度搜索 ...
- HD OJ 题目分类
动态规划:2037 2054 2084 hdu题目分类(转) http://blog.csdn.net/vsooda/article/details/7258791 1001 整数求和 水题 1002 ...
- HDOJ题目分类大全
版权声明:本文为博主原创文章,欢迎转载,转载请注明本文链接! https://blog.csdn.net/qq_38238041/article/details/78178043 杭电里面有很多题目, ...
- HDU题目分类大全【大集合】
基础题: 1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.105 ...
- hdoj杭电问题分类
杭电上的题虽然多,但是一直苦于找不到问题分类,网页都是英文的,所以平时做题也没怎么看,今天仔细一看,问题分类竟然就在主页....做了那么久的题居然没发现,表示已经狗带..不要笑,不知道有没有像我一样傻 ...
- 转载:Hdu 题目分类
原址点击 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056. ...
- hdu与poj题目分类
POJ 初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(po ...
- hdu HDOJ 题目分类(转)
动态规划:2037 2054 2084 hdu题目分类(转) http://apps.hi.baidu.com/share/detail/17053154 1001 整数求和 水题 1002 C语言实 ...
- HDOJ ACM 题目
转载 HDOJ 题目分类(转) 1001 整数求和 水题 1002 C语言实验题--两个数比较 水题 1003 1.2.3.4.5... 简单题 1004 渊子赛马 排序+贪心的方法归并 1005 H ...
最新文章
- linux apt-get proxy
- WPF中自定义窗体标题栏
- mysql dns反向解析_Mysql DNS反向解析导致连接超时过程分析(skip-name-resolve)
- .NET的轻量级IOC框架芮双随笔
- lz0-007 读书笔记11
- c语言实验题水仙花数5359,《C语言程序设计》实验报告(实验1-12).doc
- layUI日期格式转换
- word 转 PDF时报错
- Linux系统软件安装
- HTML语言标记详解
- uni-app左右平分九宫格样式
- 首屈一指的全球招聘与薪资支付平台Deel收购Zeitgold,以增强薪资和人工智能服务
- abaqus的python安装文件在哪_python - 在ABAQUS 6.14 python环境中安装熊猫 - 堆栈内存溢出...
- 【GD32L233C-START】1、初识GD32L233C-START板卡
- 海龟交易法则 matlab,【干货】经典的期货策略——海龟交易法则(二)
- 指尖江湖李忘生鸿蒙初开,剑网3指尖江湖李忘生技能搭配攻略
- 如何使用文献管理软件EndNote x9 x9.1高效管理科研相关参考文献 相关论文
- EDA课设:CPU设计
- 合并财务报表及其合并范围的确定
- php百度快照劫持,discuz百度快照被劫持的解决办法