博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2377 Bad Cowtractors
阅读量:4126 次
发布时间:2019-05-25

本文共 2350 字,大约阅读时间需要 7 分钟。

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17336   Accepted: 7026

Description

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 17

Sample Output

42

Hint

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.
题意:求最大生成树,无结果输出-1

题解:kruskal算法把排序反过来写即可,最后判断并查集的父结点是否唯一,不唯一说明无解。

AC代码:

#include 
#include
using namespace std;#define _for(i,a,b) for(int i=a;i<=b;i++)const int maxn = 20007;int n,m,sum,fa[maxn];struct node{ int x,y,length;}a[maxn];bool cmp(node a,node b){ return a.length>b.length;}int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]);}int main(int argc, char const *argv[]){ cin>>n>>m; _for(i,1,m) { cin>>a[i].x>>a[i].y>>a[i].length; } _for(i,1,n)fa[i]=i; sort(a+1,a+1+m,cmp); _for(i,1,m) { int p = find(a[i].x); int q = find(a[i].y); if(p!=q) { fa[p]=q; sum+=a[i].length; } } int ok = 0; _for(i,1,n)if(fa[i]==i)ok++; if(ok==1)cout<
<

转载地址:http://gnhpi.baihongyu.com/

你可能感兴趣的文章
解决“fatal error: dynlink_nvcuvid.h: 没有那个文件或目录#include <dynlink_nvcuvid.h>“问题
查看>>
Ubuntu 18.04安装OpenCV
查看>>
Ubuntu 18.04 安装labelImg与使用
查看>>
解决“WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!”问题
查看>>
在Linux服务器上安装Anaconda步骤
查看>>
Linux服务器安装Python3.7.5心路历程
查看>>
为Linux服务器conda添加清华镜像
查看>>
解决“The channel is not accessible or is invalid”问题
查看>>
解决下载Github项目速度过慢的问题
查看>>
在Linux服务器上安装Pytorch1.5.1版本
查看>>
向Linux服务器传输文件
查看>>
解决“tensorboard报错”问题
查看>>
配置服务器CV实验环境之缺啥装啥
查看>>
解决“RuntimeError: expected backend CUDA and dtype Float but got backend CUDA and dtype Half”问题
查看>>
解决“E505:“/usr/bin/yum“ is read-only(add !to override)”问题
查看>>
解决“ImportError: libGL.so.1”问题
查看>>
解决“ImportError: libgthread-2.0.so.0”问题
查看>>
解决“./clash-linux-amd64-v1.3.0: 权限不够”问题
查看>>
解决“ImportError: cannot import name ‘_validate_lengths‘”问题
查看>>
解决“TypeError: can‘t convert cuda:0 device type tensor to numpy. ......”问题
查看>>