POJ:3371 Connect the Cities(最小生成树)
AC代码:
/**
/*@author Victor
/* C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=+;
const int MOD=1e9+;
const double PI = acos(-1.0);
const double EXP = 1E-;
const int INF = 0x3f3f3f3f; const int maxn=+;
struct Edge
{
int from,to,dist;
Edge(int f,int t,int d):from(f),to(t),dist(d) {}
bool operator <(const Edge& a)
{
return dist<a.dist;
}
};
vector<Edge>edges;
int pre[maxn],T[maxn];
//并查集
int find(int x)
{
int i=x;
while(pre[i]!=i)
i=pre[i];
int j=x,k;
while(j!=pre[j])
{
k=pre[j];
pre[j]=i;
j=k;
}
return i;
}
void joint(int x,int y)
{
if(find(x)!=find(y))
pre[find(x)]=find(y);
}
int kruskal()
{
int sum=;
sort(edges.begin(),edges.end());
for(int i=; i<edges.size(); i++)
{
int x=find(edges[i].from),y=find(edges[i].to);
if(x!=y)
{
sum+=edges[i].dist;
pre[x]=y;
}
}
return sum;
}
int main()
{
int n,m,k,Case;
scanf("%d",&Case);
while(Case--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; i++)
pre[i]=i;
edges.clear();
for(int i=; i<m; i++)
{
int f,t,d;
scanf("%d%d%d",&f,&t,&d);
edges.push_back(Edge(f,t,d));
}
for(int i=; i<k; i++)
{
int t;
scanf("%d",&t);
for(int j=; j<t; j++)
scanf("%d",&T[j]);
for(int j=; j<t; j++)
joint(T[],T[j]);
}
int ans=kruskal();
//通过并查集判断是否联通
bool mark=true;
for(int i=; i<=n; i++)
if(find()!=find(i))
{
mark=false;
break;
}
if(mark==true)
printf("%d\n",ans);
else
printf("-1\n");
}
return ;
}
最小生成树模板
/*
Kruskal 基本模板*/
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3;
struct Edge{
int from,to,dist;
Edge(int f,int t,int d):from(f),to(t),dist(d){}
bool operator <(const Edge& a){
return dist<a.dist;
}
};
vector<Edge>edges;
int pre[maxn];
int find(int x){
int i=x;
while(pre[i]!=i)i=pre[i];
int j=x,k;
while(j!=pre[j]){
k=pre[j];
pre[j]=i;
j=k;
}
return i;
}
void joint(int x,int y){
if(find(x)!=find(y))pre[find(x)]=find(y);
}
int kruskal(){
int sum=;
sort(edges.begin(),edges.end());
for(int i=;i<edges.size();i++){
int x=find(edges[i].from),y=find(edges[i].to);
if(x!=y){
sum+=edges[i].dist;
pre[x]=y;
}
}
return sum;
}
int main(){
int v,e;
while(~scanf("%d%d",&v,&e)){
for(int i=;i<=v;i++)pre[i]=i;
edges.clear();
for(int i=;i<e;i++){
int f,t,d;
scanf("%d%d%d",&f,&t,&d);
edges.push_back(Edge(f,t,d));
}
int ans=kruskal();
printf("%d\n",ans);
}
return ;
}
POJ:3371 Connect the Cities(最小生成树)的更多相关文章
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- hdoj 3371 Connect the Cities
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
随机推荐
- nodejs,事件轮询总结
宏任务 script,setTimeoout,setInterval,setlmmediate(node 独有),I/o,render渲染 微任务 process.nextTick(),promise ...
- smbrun - smbd和外部程序间的接口程序。
总览 SYNOPSIS smbrun shell-command 描述 DESCRIPTION 此程序是samba套件的一部分. smbrun是个非常小的“粘合”程序,用于为smbd守护程序smbd( ...
- Kvm--03 kvm克隆,桥接网络,热添加
目录 1.Kvm克隆 1). 完整克隆 2). 链接克隆 2.kvm虚拟机的桥接网络 3.在线热添加网卡,cpu 1). 热添加网卡 2). 热添加内存 3). 热添加cpu参数 1.Kvm克隆 1) ...
- EBCDIC to ASCII
EBCDIC to ASCII https://www.ibm.com/support/knowledgecenter/SSZJPZ_11.7.0/com.ibm.swg.im.iis.ds.parj ...
- Sass函数:值列表函数length
length() 函数主要用来返回一个列表中有几个值,简单点说就是返回列表清单中有多少个值: >> length(10px) 1 >> length(10px 20px (bo ...
- 前端每日实战:67# 视频演示如何用纯 CSS 创作单元素点阵 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/YvBvBr 可交互视频 此视频是可以 ...
- Vue $ref 的用法
<div id="app"> <cpn $ref="item"></cpn> <cpn></cpn> ...
- paper 160:python 知识点概要 更新ing
1.python json http://www.runoob.com/python/python-json.html Python的json模块提供了一种很简单的方式来编码和解码JSON数据. 其 ...
- ADSL(Asymmetric Digital Subscriber Loop)技术
上行带宽,下行带宽 宽带上行下行是指一般ADSL上网方式上行与下行速率,上行就是从电脑上传的速度,下行就是从网络上的主机下载速度,一般下行速率比较高! ADSL(Asymmetric Digital ...
- PPT技巧
1.秋叶个人的PPT三分钟教程 http://www.pptfans.cn/315656.html 2.<说服力-让你的PPT会说话>秋叶 3.<三体> https://w ...