Bad Cowtractors
来源:互联网 发布:网络课程女子礼仪答案 编辑:程序博客网 时间:2024/05/20 11:31
D - Bad CowtractorsTime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %lluDescription
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".Input
* 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.Output
* 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.Sample Input
5 81 2 31 3 72 3 102 4 42 5 83 4 63 5 24 5 17Sample Output
42Hint
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>#include<algorithm>using namespace std;struct node{int u;int v;int w;}e[20010];int f[20010];bool cmp(node a,node b){return a.w>b.w;}int getf(int x){if(x==f[x])return x;f[x]=getf(f[x]);return f[x]; }int merge(int y1,int y2){int b1,b2;b1=getf(y1);b2=getf(y2);if(b1!=b2){f[b1]=b2;return 1;}return 0;}int main(){int n,m,maxcost=0,num=0;scanf("%d %d",&n,&m);int k=m,i,t=0,j;while(k--){scanf("%d %d %d",&e[t].u,&e[t].v,&e[t].w);t++; } sort(e,e+m,cmp); for(i=1;i<=n;i++) f[i]=i; for(j=0;j<m;j++) {if(merge(e[j].u,e[j].v)){maxcost+=e[j].w;num++; } if(num==n-1) { printf("%d",maxcost); return 0;} } printf("-1"); return 0; }
0 0
- Bad Cowtractors
- Bad Cowtractors
- Bad Cowtractors
- Bad Cowtractors
- Bad Cowtractors
- Bad Cowtractors
- PKU 2377 Bad Cowtractors
- EOJ 5 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- poj2377 - Bad Cowtractors
- POJ_2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ 2377 Bad Cowtractors
- POJ-2377-Bad Cowtractors
- POJ 2377 Bad Cowtractors
- poj 2377 Bad Cowtractors
- ffmpeg av_register_all release版 异常
- 算法分析
- DRAM, SRAM, SDRAM的关系与区别
- 弹层相对于浏览器窗口(或屏幕)居中
- Java Web—跳转
- Bad Cowtractors
- 在线代码编辑器 CODEMIRROR 配置说明
- 模仿小米手环 计步数据的展示View
- fir.im Weekly - 暖心的 iOS 持续集成,你值得拥有
- Ajax基本概念以及JS实现Ajax的过程
- Out of Hay
- 线性表1-顺序表
- 网络安全-常见漏洞与分析
- 正视自己的问题