POJ 2421 Constructing Roads (最小生成树)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
System Crawler (2015-05-27)
Description
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
Sample Input
- 3
- 0 990 692
- 990 0 179
- 692 179 0
- 1
- 1 2
Sample Output
- 179
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <queue>
- #include <vector>
- #include <map>
- #include <algorithm>
- #include <cstring>
- #include <cctype>
- #include <cstdlib>
- #include <cmath>
- #include <ctime>
- using namespace std;
- const int SIZE = ;
- int N,M,NUM;
- int FATHER[SIZE];
- struct Node
- {
- int from,to,cost;
- }G[SIZE * SIZE];
- void ini(void);
- int find_father(int);
- void unite(int,int);
- bool same(int,int);
- bool comp(const Node &,const Node &);
- int kruskal(void);
- int main(void)
- {
- int from,to,cost;
- while(scanf("%d",&N) != EOF)
- {
- ini();
- for(int i = ;i <= N;i ++)
- for(int j = ;j <= N;j ++)
- {
- scanf("%d",&cost);
- if(i == j)
- continue;
- if(i < j)
- {
- G[NUM].from = i;
- G[NUM].to = j;
- G[NUM].cost = cost;
- NUM ++;
- }
- }
- scanf("%d",&M);
- while(M --)
- {
- scanf("%d%d",&from,&to);
- unite(from,to);
- }
- sort(G,G + NUM,comp);
- printf("%d\n",kruskal());
- }
- return ;
- }
- void ini(void)
- {
- NUM = ;
- for(int i = ;i <= N;i ++)
- FATHER[i] = i;
- }
- int find_father(int n)
- {
- if(n == FATHER[n])
- return n;
- return FATHER[n] = find_father(FATHER[n]);
- }
- void unite(int x,int y)
- {
- x = find_father(x);
- y = find_father(y);
- if(x == y)
- return ;
- FATHER[x] = y;
- }
- bool same(int x,int y)
- {
- return find_father(x) == find_father(y);
- }
- bool comp(const Node & a,const Node & b)
- {
- return a.cost < b.cost;
- }
- int kruskal(void)
- {
- int ans = ,count = ;
- for(int i = ;i < NUM;i ++)
- if(!same(G[i].from,G[i].to))
- {
- unite(G[i].from,G[i].to);
- ans += G[i].cost;
- count ++;
- if(count == N - )
- break;
- }
- return ans;
- }
POJ 2421 Constructing Roads (最小生成树)的更多相关文章
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
- Poj 2421 Constructing Roads(Prim 最小生成树)
题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...
- POJ - 2421 Constructing Roads(最小生成树&并查集
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- poj 2421 Constructing Roads 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...
随机推荐
- AWstat(linux下)
一.AWstat统计信息 AWstat是一个非常简洁而且功能强大的统计工具.它可以统计你站点的如下信息: 1 访问量.访问次数.页面浏览量.点击数.数据流量等精确到每月.每日.每小时 2 访问者国家. ...
- iisapp 命令 弹出 iisschlp.wsc [88,25] 属性值无效 progid
iisapp 命令 弹出 iisschlp.wsc [88,25] 属性值无效 progid 在执行iisapp.vbs时,可能会提示如下错误:Windows Script Component - f ...
- UI:页面传值、单例模式传值、属性传值、NSUserDefaults 数据持久化
<单页面传值> 页面传值,从前向后传值,使用属性,在后一个页面定义属性,在前一个页面,用点语法,获得值,在适当的时候传值 页面传值,从后向前面传值,使用协议和代理,在后一个页面指定协议,定 ...
- js 控制DIV 预览打印
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- MFC 学习 之 工具栏的添加(一)
最终实现的效果图: 步骤一:接下来在资源视图中添加一个ToolBar工具栏(具体怎么添加在这儿就不详细讲解了!)添加后的ToolBar以及工具栏中每个按钮 所命名的ID如下:(可以自定义,只要不重名就 ...
- Java数据结构之线性表
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- 14的路 MySQL的btree索引和hash索引的区别
http://www.cnblogs.com/vicenteforever/articles/1789613.html ash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tr ...
- Java多线程技术学习笔记(二)
目录: 线程间的通信示例 等待唤醒机制 等待唤醒机制的优化 线程间通信经典问题:多生产者多消费者问题 多生产多消费问题的解决 JDK1.5之后的新加锁方式 多生产多消费问题的新解决办法 sleep和w ...
- 使用visual studio 2013 快速搭建phonegap开发环境
前一段时间开发了一款简单的Phonegap应用,遇到了很多坑,其中有一个坑就是在搭建开发环境上.由于Phonegap 2.x 与3.x 区别比较大,导致了开发环境也有所不同.2.x 是这样的http: ...
- leanchat-android
Original: https://github.com/lzwjava/leanchat-android Backup: https://github.com/eltld/leanchat-andr ...