HDU3371 Connect the Cities
题目描述:
有n个小岛,其中有的小岛之间没有通路,要修这样一条通路需要花费一定的钱,还有一些小岛之间是有通路的。现在想把所有的岛都连通起来,求最少的花费是多少。
输入:
第一行输入T,代表多少组数据。
第二行输入三个整数n , m , k,分别代表一共有n个小岛,m条路径可供选择,k表示有连通岛的个数。
接下来的m行,每行三个整数p , q , c ,代表建设p到q的通路需要花费的钱为c。
接下的k行,每一行的数据输入如下:先输入Q,代表这个连通分量里面有Q个小岛,然后输入Q个小岛,代表这Q个小岛是连通的。
输出:
输出最少花费为多少,如果无法修建出此路,输出"-1"。
其实这道题属于并查集的入门题目,解法如下:
先用并查集把还在连通的k个分量给合并起来,此时记录好连通分量的个数。然后把Q条路径排一遍序,然后从最小的路径开始枚举,对每一条路径判断起点和终点是否在同一个连通分量内,如果不在的话将这两点合并,连通分量的个数减一,加上修建这条路的花费,如此枚举,知道连通分量的个数为1。如果最后的连通分量大于1,则说明无法连接,输出-1。
可能是解法不太好吧,最后890ms险过,这几天做的并查集都是险过QAQ...
- #include <iostream>
- #include <iomanip>
- #include <stdio.h>
- #include <stdlib.h>
- #include <algorithm>
- #include <functional>
- #include <vector>
- #include <cmath>
- #include <string>
- #include <stack>
- #include <queue>
- using namespace std;
- int k , n , m , father[];
- struct P{
- int start , end , cost;
- }p[+]; //路径
- bool cmp(P a , P b)
- {
- return b.cost > a.cost;
- }
- void init()
- {
- for(int i = ; i <= n ; i++) {
- father[i] = i;
- }
- }
- int getf(int v)
- {
- return father[v] = (v == father[v]) ? v : getf(father[v]);
- }
- void merge(int x , int y)
- {
- int a , b;
- a = getf(x);
- b = getf(y);
- if(a != b)
- father[b] = a;
- }
- int main()
- {
- int T , i , j;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%d %d %d",&n,&m,&k);
- for(i = ; i <= m ; i++)
- scanf("%d %d %d",&p[i].start,&p[i].end,&p[i].cost);
- init();
- for(i = ; i <= k ; i++) {
- int cnt;
- scanf("%d",&cnt);
- int *tmp = new int[cnt];
- for(j = ; j < cnt ; j++) {
- scanf("%d",&tmp[j]);
- if(j != )
- merge(tmp[j-] , tmp[j]);
- }
- delete []tmp;
- }
- sort(p+ , p+m+ , cmp);
- int sum = ; //连通分量的个数
- int ans = ;
- for(i = ; i <= n ; i++)
- if(father[i] == i)
- sum++;
- for(i = ; i <= m ; i++) {
- if(sum == ) //连通分量的个数为1时,说明这时候已经完成
- break;
- if(getf(p[i].start) != getf(p[i].end)) {
- merge(p[i].start , p[i].end);
- ans += p[i].cost;
- sum--; //连上一条路后连通分量-1
- }
- }
- if(sum > ) {
- printf("-1\n");
- } else {
- printf("%d\n",ans);
- }
- }
- return ;
- }
HDU3371 Connect the Cities的更多相关文章
- hdu3371 Connect the Cities (MST)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Connect the Cities(hdu3371)并查集(附测试数据)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- hdoj 3371 Connect the Cities
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities(prime)
Connect the Cities Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- c#入门学习-Action和Func的使用
我的理解就是:Action和Func就是官方声明好的代理using System; namespace funcActionDemo{ class MainClass { p ...
- 转:基础篇|PHP如何解决网站大流量和高并发
基础篇 高并发架构基础概念和优化思路 高并发架构相关概念 并发,在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程 ...
- 洛谷P3147 [USACO16OPEN]262144
P3147 [USACO16OPEN]262144 题目描述 Bessie likes downloading games to play on her cell phone, even though ...
- CF986A Fair
题目描述 Some company is going to hold a fair in Byteland. There are n n n towns in Byteland and m m m t ...
- 洛谷P4018 Roy&October之取石子
题目背景 \(Roy\)和\(October\)两人在玩一个取石子的游戏. 题目描述 游戏规则是这样的:共有\(n\)个石子,两人每次都只能取\(p^k\)个(\(p\)为质数,\(k\)为自然数,且 ...
- sql数据库发布、订阅同步方式操作
Sql数据库发布订阅分为两个步骤:1.发布.2.订阅.首先在数据源数据库服务器上对需要同步的数据进行发布,然后在目标数据库服务器上对上述发布进行订阅. 一.发布. 发布需要用实际的服务器名称,不能使用 ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- C++之struct
C++的struct对C做和很多补充,里面可以放函数(构造函数和虚函数),也可以同struct和class的继承,在C++中也完全可以替代class.他们之间的主要区别: 1 calss默认是私有继承 ...
- c#文件相关笔记
1.将*.txt文件内容转换为一个字符串str FileStream fs = new FileStream("路径\\*.txt", FileMode.Open); Stream ...
- Kotlin基础知识
1. 改进点/基础 //安全判空 val length = text?.length; //类型转换 if (object is Car) { var car = object as Ca } //操 ...