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 ...
随机推荐
- Confluence 6 链接到其他应用
应用链接(Application Links)有时候也被称为 "AppLinks" 是一系列测插件能够允许你在 Atlassian 的应用中互相链接.链接 2 个应用能够允许你在 ...
- 【sqli-labs】Less11~Less16
学习sqli-labs的笔记,前面的笔记内容比较详细.后面的只记录关键点了. Less11: POST注入, 有回显,有错误提示 从11题起是POST注入,发现有两个输入框.用firefox的F12查 ...
- JumpServer里的sftp功能报错说明
JumpServer里sftp默认的家目录是/tmp下 修改默认家目录: vim /usr/local/coco/coco/sftp.py class SFTPServer(paramiko.SFTP ...
- 步步为营102-Css样式加个版本
背景:当系统发布后修改了css样式,由于浏览器有缓存,所以会造成css样式无效.可通过在css中添加版本号来解决 1 修改css引用 <link rel="stylesheet&quo ...
- 目标检测之选择性搜索-Selective Search
一.滑动窗口检测器 一种用于目标检测的暴力方法就是从左到右,从上到下滑动窗口,利用分类识别目标.为了在不同观察距离处检测不同的目标类型,我们可以使用不同大小和宽高比的窗口 得到窗口内的图片送入分类器, ...
- Vim设计
像 IDE 一样使用 vim 免费的编程中文书籍索引
- Task任务的屏障机制
Barrier 是 .Net 提供的一直并发的机制,它允许多个任务同步他们不同阶段的并发工作. 这里的关键点是[多个任务]和[不同阶段]. 假设有4个相同的任务(Task),每个任务都有4个阶段(Ph ...
- 一脸懵逼学习MapReduce的原理和编程(Map局部处理,Reduce汇总)和MapReduce几种运行方式
1:MapReduce的概述: (1):MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题. (2):MapReduce由两个阶段组成:Map和Red ...
- [转] 2016 JavaScript 发展现状大调查
有人认为JavaScript是最好的语言,有人认为它一团糟.可按照C++之父的话来讲: 世界上只有两种编程语言:一种是天天被人喷的,另一种是没人用的. 不论你喜欢承认与否,JavaScript已经一天 ...
- [转] 使用babel-plugin-react-css-modules简化CSS Modules的使用
在我们的产品中,均使用CSS Modules来作为样式解决方案,大致的代码是这样的: import React from 'react'; import styles from './table.cs ...