hdu-3371 Connect the Cities---kruskal
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3371
题目大意:
给n个城市,m条路,k组已知路,求最小费用联通所有城市;
解题思路:
kruskal求MST,这道题目有毒,很容易超时,改了一下并查集才过,而且同一个代码有时过又是超时
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
int p[maxn];
int tot;
void init()
{
for(int i = ; i < maxn; i++)p[i] = i;
}
int f(int x)
{
return x == p[x] ? x : p[x] = f(p[x]);
}
void Union(int x, int y)
{
x = f(x);
y = f(y);
if(x != y)
{
p[x] = y;//这里改成p[y] = x;有时过,有时会超时
tot--;//不联通的数目减一
}
}
struct edge
{
int u, v, w;
bool operator < (const edge& a)
{
return w < a.w;
}
}a[maxn * maxn];
int main()
{
int T, n, m, k, t, x, y;
scanf("%d", &T);
while(T--)
{
init();
scanf("%d%d%d", &n, &m, &k);
tot = n - ;//最开始,不连通块数目为n-1
for(int i = ; i <= m; i++)
{
scanf("%d%d%d", &a[i].u, &a[i].v, &a[i].w);
}
sort(a + , a + + m);
while(k--)
{
scanf("%d%d", &t, &x);
t--;
while(t--)
{
scanf("%d", &y);
Union(x, y);
}
}
int ans = ;/*
for(int i = 1; i <= n; i++)cout<<p[i]<<" ";
cout<<endl;*/
for(int i = ; i <= m; i++)
{
int x = a[i].u;
int y = a[i].v;
x = f(x), y = f(y);
if(x != y)
{
p[x] = y;
tot--;//不连通数目减一
ans += a[i].w;
}
}
if(tot == )//全部连通
printf("%d\n", ans);
else
printf("-1\n");
}
return ;
}
hdu-3371 Connect the Cities---kruskal的更多相关文章
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdoj 3371 Connect the Cities
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- POJ:3371 Connect the Cities(最小生成树)
http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...
随机推荐
- Beta冲刺测试
1.项目概述 1.项目名称 微信四则运算小程序 2.项目简介 基于微信小程序,为用户提供一个答题的平台 3.项目预期达到目标 用户通过微信小程序可以在里边答题,模式或者题量的选择为用户匹配到适合他们的 ...
- Python 爬虫笔记
urllib python3 与 python2 的urllib很不一样,之前urllib2的方法基本上都变成了python3里的urllib.request模块中 import urllib.req ...
- Nginx + Tomcat7 + redis session一致性问题
Nginx 作负载均衡时,由于是每次都需要把请求分发到不同的机器,同一个用户在一台机器上创建了 session,下一次的请求很有可能会转发到另外一台机器,会造成 session 丢失.我们可以使用 R ...
- java 正则简单使用
查找是否包含字串 查询是否包含 #{name} 片段 这里有包含所以返回true String context = "select * from t_user where (name = # ...
- Unity 行为树-共享变量
一.引言 有以下小场景: 节点A:发现了 敌人. 节点B:追逐敌人. 对于同一个敌人物体,节点AB之间是如何传递数据 的呢? 行为树节点AB之间,需要一个中间变量Temp来传递数据. A发现了敌人,将 ...
- git 处理 crlf rf
假如你正在 Windows 上写程序,又或者你正在和其他人合作,他们在 Windows 上编程,而你却在其他系统上,在这些情况下,你可能会遇到行尾结束符问题.这是因为 Windows 使用回车和换行两 ...
- Scala_Load csv data to hive via spark2.1_via pass parameters
prepare csv: # vim /tmp/cars.csv year,make,model,comment,blank ","Tesla","S" ...
- 小小粉丝度度熊 二分答案 + two pointer
http://acm.hdu.edu.cn/showproblem.php?pid=6119 发现自己的two pointer能力超弱. 这题是合并时间后,二分答案. 可以知道对于每个时间区间,合法的 ...
- Maven的学习资料收集--(二)安装m2eclipse插件
在Eclipse中可以安装Maven插件,可以更方便的使用: 官网地址:http://www.eclipse.org/m2e/ 可以在线安装或者离线下载,之前在线安装总是失败,可能是网速的原因,找到了 ...
- eclipse配置android开发环境并搭建第一个helloWord工程
一.搭建Android在eclipse下环境 一.JDK(不用安装 下载地址: http://www.xp510.com/xiazai/Application/program/23625.ht ...