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 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
随机推荐
- JavaScript 正则表达式中的特殊字符
正则表达式中的特殊字符 字符 含义 \ 依照下列规则匹配: 在非特殊字符之前的反斜杠表示下一个字符是特殊字符,不能按照字面理解.例如,前面没有 "\" 的 "b" ...
- HDU4089/Uva1498 Activation 概率DP(好题)
题意:Tomato要在服务器上激活一个游戏,一开始服务器序列中有N个人,他排在第M位,每次服务器会对序列中第一位的玩家进行激活,有四种结果: 1.有p1的概率会激活失败,这时候序列的状态是不变的.2. ...
- Java programming language does not use call by reference for objects!
Instead, object references are passed by value! A method cannot modify a parameter of a primitive ty ...
- [原创] Delphi Create(Application) 和 Create(nil) 的区别
Delphi Create(Application) 和 Create(nil) 的区别: 例如: Form1:=TForm1.Create(Application); Form1:=TForm1.C ...
- Raspbian 在虚拟机上运行,运行Flask,供宿主机访问
Raspbian 在虚拟机上运行,启动Flask,供宿主机访问 参考ref 1, 在virtualbox上跑起来Raspbian OS 参考ref 2, 在Raspbian上安装并运行Falsk, 注 ...
- python每日练习10题2
163.一个字典 key 是人名.value 是年龄,找出其中年龄 最大的人 d = {"张三":25,"李四":30,"王五":80,&q ...
- RabbitMQ之交换机
1. 交换机类型 rabbitmq常见有四种交换机类型: direct, topic, fanout, headers. 一般headers都不用,工作中用得较多的是fanout,它会将消息推送到所有 ...
- Python爬虫之抓取豆瓣影评数据
脚本功能: 1.访问豆瓣最受欢迎影评页面(http://movie.douban.com/review/best/?start=0),抓取所有影评数据中的标题.作者.影片以及影评信息 2.将抓取的信息 ...
- STM32时钟设置
一.使用外部时钟,并设置为72MHz void SetSysClockToHSE(void) { ErrorStatus HSEStartUpStatus; /* SYSCLK, HCLK, PCLK ...
- hashmap:cr:csdn
HashMap相关问题 1.你用过HashMap吗?什么是HashMap?你为什么用到它? 用过,HashMap是基于哈希表的Map接口的非同步实现,它允许null键和null值,且HashMap依托 ...