题目链接>>>

题目大意:

谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大

解题思路:

最大生成树
用kruskal在最小生成树的基础上,将排序从大到小排序,这样就是一个最大生成树了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; struct EDGE{
int x,y;
long long c;
}edge[];
int father[];
int n,m; bool cmp(EDGE a,EDGE b){
return a.c>b.c;
}
int find(int x){
if(father[x]==x)return x;
father[x]=find(father[x]);
return father[x];
}
void Kruskal(){
long long sum=,s=;
sort(edge+,edge++m,cmp);
for(int i=;i<=m;i++){
int f1=find(edge[i].x);
int f2=find(edge[i].y);
if(f1!=f2){
father[f2]=f1;
sum+=edge[i].c;
s++;
}
if(s==n-)break;
}
if(s==n-)printf("%lld\n",sum);
else printf("-1\n");
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++)father[i]=i;
for(int i=;i<=m;i++){
scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].c);
}
Kruskal();
}
return ;
}

2018-04-01

POJ2387 Til the Cows Come Home【Kruscal】的更多相关文章

  1. POJ2387 Til the Cows Come Home 【Dijkstra】

    题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...

  2. POj2387——Til the Cows Come Home——————【最短路】

    A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & ...

  3. POJ2387 Til the Cows Come Home (最短路 dijkstra)

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  4. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  5. (Dijkstra) POJ2387 Til the Cows Come Home

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 81024   Accepted ...

  6. POJ 2387 Til the Cows Come Home 【最短路SPFA】

    Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...

  7. poj2387 Til the Cows Come Home 最短路径dijkstra算法

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  8. poj2387 Til the Cows Come Home(Dijkstra)

    题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...

  9. POJ-2387 Til the Cows Come Home ( 最短路 )

    题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

随机推荐

  1. CentOS6文件系统思维导图

  2. Confluence 6 已经存在的 Confluence 安装配置一个数据源连接

    如果你希望在使用 JDBC 直接方式的应用中切换到使用数据源: 停止 Confluence. 备份下面的文件,以防止你可能需要重新恢复你的配置: <installation-directory& ...

  3. Ubuntu下Mongodb和Robo3T的安装与使用

    Mongodb的安装:https://blog.csdn.net/Canhui_WANG/article/details/78995388 Robo3T的安装:https://www.jianshu. ...

  4. numpy:dot与multiply

    http://blog.csdn.net/iamzhangzhuping/article/details/52370241

  5. vue之自行实现派发与广播-dispatch与broadcast

    要解决的问题 主要针对组件之间的跨级通信 为什么要自己实现dispatch与broadcast? 因为在做独立组件开发或库时,最好是不依赖第三方库 为什么不使用provide与inject? 因为它的 ...

  6. OSError: cannot identify image file

    OSError: cannot identify image file <_io.BytesIO object at 0x00000236DD598BF8> 说一下为什么会出现OSErro ...

  7. select下拉框的数据回显

    需求描述:select框,下拉后又很多的选项,选择一个,根绝后台代码做查询,完成之后,页面上的select框还是之前选的那个值 解决思路:select本质就是 value和text一一对应,根据你的s ...

  8. 用来表达更复杂的sql语句!!!!!extra 原声sql

    extra 用来表达更复杂的sql语句!!!!! extra可以指定一个或多个 参数,例如 select, where or tables. 这些参数都不是必须的,但是你至少要使用一个!要注意这些额外 ...

  9. jQuery绑定或删除绑定事件

    <!DOCTYPE html><html lang="en" class="loading"><head> <meta ...

  10. java数据

    因为曾经干了啥事儿,才印象特别深刻. 将byte存入String的后果 String res = ""; res += (char) 0xc3; byte[] bytes = re ...