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(最小生成树)的更多相关文章

  1. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  2. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  3. HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

  4. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  5. hdu oj 3371 Connect the Cities (最小生成树)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  7. hdoj 3371 Connect the Cities

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  9. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

随机推荐

  1. 常见前端HTML5面试题

    1.H5新标签新特性 新标签:header,nav,footer,aside,article,section,Canvas,audio,video 新特性:localStorag, sessionSt ...

  2. #6392. 「THUPC2018」密码学第三次小作业 / Rsa (exgcd求逆元+快速幂+快速乘)

    题目链接:https://loj.ac/problem/6392 题目大意:给定五个正整数c1,c2,e1,e2,N,其中e1与e2互质,且满足 c1 = m^e1 mod N c2 = m^e2 m ...

  3. 【LeetCode+51nod】股票低买高卖N题

    [121]Best Time to Buy and Sell Stock (2018年11月25日重新复习) 给一个数组代表股票每天的价格,只能有一次交易,即一次买入一次卖出,求最大收益. 题解:用一 ...

  4. Sass--传一个带值的参数

    在 Sass 的混合宏中,还可以给混合宏的参数传一个默认值,例如: @mixin border-radius($radius: 3px) { -webkit-border-radius: $radiu ...

  5. Vue-选项卡切换

    <html> <head> <title>Vue实现tab切换效果</title> <script src="vue.js"& ...

  6. 苹果正制造一款疯狂的“16K”VR耳机,效果到底有多牛?

    苹果一直以来都有传闻称正在研发一副增强现实眼镜,但今天的一份报告显示,他们希望在虚拟现实领域与Google,微软和Facebook竞争. 据CNET报道,苹果公司着眼于2020年发布的将AR和VR技术 ...

  7. C#基础提升系列——C# LINQ

    C# LINQ LINQ(Language Integrated Query,语言集成查询).在C# 语言中集成了查询语法,可以用相同的语法访问不同的数据源. 命名空间System.Linq下的类En ...

  8. NET Core SDK 已安装在VS2017不可见

    本地装了6个版本的net core sdk,但是在vs2017,vs2019 只是显示1.0和1.1: 重装,重启了好几遍也没用,没想到是环境变量PATH顺序问题,将x64的放在x86前,就OK了~:

  9. python每日练习10题2

    163.一个字典 key 是人名.value 是年龄,找出其中年龄 最大的人 d = {"张三":25,"李四":30,"王五":80,&q ...

  10. php pow()函数 语法

    php pow()函数 语法 作用:pow()函数的作用是将一个数进行n次方计算后返回,广东大理石平台 语法:pow(X,Y); 参数: 参数 描述 X 要做处理的数字 Y 指定n次方中的n数值 说明 ...