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. elasticsearch 基础 —— 集群原理

    空集群 如果我们启动了一个单独的节点,里面不包含任何的数据和 索引,那我们的集群看起来就是一个 图 1 "包含空内容节点的集群". 图 1. 包含空内容节点的集群 一个运行中的 E ...

  2. Tesseract-OCR识别中文与训练字库

    转自:https://www.cnblogs.com/lcawen/articles/7040005.html 关于中文的识别,效果比较好而且开源的应该就是Tesseract-OCR了,所以自己亲身试 ...

  3. Tutorial1

    一 Introduction to tf2 本部分是关于tf2简单介绍,比如tf2能做什么,并使用一个turtlesim的例子来显示tf2在多机器人中的一些能力.同时也包括一些工具的使用,比如tf2_ ...

  4. Codeforces Round #394 (Div. 2) - B

    题目链接:http://codeforces.com/contest/761/problem/B 题意:给定一个环形跑道.里面有n个障碍,跑道长度为L.然后有两个人在两个起点(起点可能相同),每个人都 ...

  5. Sass函数-join()函数

    join() 函数是将两个列表连接合并成一个列表. >> join(10px 20px, 30px 40px) (10px 20px 30px 40px) >> join((b ...

  6. CentOS7.2安装Oracle12.1.0.2

    Centos7.2环境安装(安装桌面) #安装界面 #查看版本 cat /etc/redhat-release #查看连接 ifconfig eth0 echo "127.0.0.1 tes ...

  7. find命令查找目录

    find <path> -type d -name "dir_name" -type d是查找目录的参数,如果是查找其他的: -type b: Block specia ...

  8. Activity(工作流-1)

    1.activity自带数据表的含义(23张表) (1)资源库流程规则表 1)act_re_deployment 部署信息表 2)act_re_model  流程设计模型部署表 3)act_re_pr ...

  9. 【加密】RSA验签及加密

    通过OpenSSL生成公私钥文件(如果没有OpenSSL工具建议下载Cmder工具自带OpenSSL指令) 1.生成RSA密钥的方法 genrsa -out private-rsa.key 2048 ...

  10. Hadoop编程调用HDFS(JAVA)

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 Hadoop环境: Cloudera QuickStart 2.GITHUB地址 http ...