POJ2387 Til the Cows Come Home【Kruscal】
题目链接>>>
题目大意:
谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大
解题思路:
最大生成树
用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】的更多相关文章
- POJ2387 Til the Cows Come Home 【Dijkstra】
题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...
- POj2387——Til the Cows Come Home——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- 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 ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- (Dijkstra) POJ2387 Til the Cows Come Home
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 81024 Accepted ...
- 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 ...
- 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 ...
- poj2387 Til the Cows Come Home(Dijkstra)
题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...
- 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 ...
随机推荐
- JAVA 语言如何进行异常处理,关键字: throws,throw,try,catch,finally分别代表什么意义? 在try块中可以抛 出异常吗?
Java通过面向对象的方法进行异常处理,把各种不同的异常进行分类, 并提供了良好的接口. 在 Java中,每个异常都是一个对象,它是 Throwable 类或其它子类的实例.当一个方法出 ...
- Confluence 6 workbox 包含从 Jira 来的通知
如果你的 Confluence 站点链接了一个 Jira 应用,你可以包含从 Jira 应用来的通知,例如 Jira 软化或 Jira 服务器桌面. 希望包含有从 Jira 应用来的通知: 你的 Ji ...
- vue-cli 3配置接口代理
vue.config.js vue.config.js是一个可选的配置文件,新建该文件,存放在项目根目录(将自动加载)中 // 作为配置文件,直接导出配置对象即可 module.exports = { ...
- django中数据库的配置及相关增删改查
ORM ORM是什么?:(在django中,根据代码中的类自动生成数据库的表也叫--code first) ORM:Object Relational Mapping(关系对象映射) 类名对应---- ...
- flask 面试题
1,什么是Flask,有什么优点?概念解释Flask是一个Web框架,就是提供一个工具,库和技术来允许你构建一个Web应用程序.这个Web应用程序可以是一些Web页面,博客,wiki,基于Web的日里 ...
- 二.Rsync备份服务
自己动手部署一遍 期中架构-第二章-备份服务笔记====================================================================== 01. 课 ...
- dubbo源码之服务消费
消费端启动初始化过程: 消费端的代码解析也是从配置文件解析开始的,服务发布对应的<dubbo:service,解析xml的时候解析了一个ServiceBean,并且调用ServiceConfig ...
- Python编程:从入门到实践(选记)
本文参考< Python 编程:从入门到实践>一书,作者: [ 美 ] Eric Matthes 第1章 起步 1.1 搭建python环境 在不同的操作系统中, Python 存 ...
- 【sqli-labs】Less5~Less6
双注入原理: 来源: http://www.myhack58.com/Article/html/3/7/2016/73471.htm (非常详细的说明了原理,good) http://www.2cto ...
- tarjan求lca :并查集+dfs
//参考博客 https://www.cnblogs.com/jsawz/p/6723221.html#include<bits/stdc++.h> using namespace std ...