hdoj_1102Constructing Roads(最小生成树)&& poj_2485Highways
来源:互联网 发布:阿里云主机租赁 编辑:程序博客网 时间:2024/05/18 00:04
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10143 Accepted Submission(s): 3778
Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
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.
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
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
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.
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
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
30 990 692990 0 179692 179 011 2
Sample Output
179
#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>using namespace std;#pragma warning(disable : 4996)#define MAX 10000typedef struct edge{int x, y;int w;}edge;edge e[MAX];int father[MAX], ranks[MAX];bool cmp(edge a,edge b){return a.w < b.w;}void Make_Set(int n){for(int i = 1; i <= n; i++){father[i] = i;ranks[i] = 0;}}int Find_Set(int x){if(x != father[x])father[x] = Find_Set(father[x]);return father[x];}void Merge_Set(int x, int y){x = Find_Set(x);y = Find_Set(y);if(x == y) return;if(ranks[x] > ranks[y]){father[y] = x;}else if(ranks[x] < ranks[y]){father[x] = y;}else {ranks[y]++;father[x] = y;}}int main(){freopen("in.txt", "r", stdin);int n, x, y, q, count = 0;while (scanf("%d", &n) != EOF){Make_Set(n);count = 0;for (int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){scanf("%d", &x);if(i != j){e[count].x = i;e[count].y = j;e[count++].w = x;}}}sort(e, e + count, cmp);scanf("%d", &q);while (q--){scanf("%d %d", &x, &y);Merge_Set(x, y);}int sum = 0;for (int i = 0; i < count; i++){x = Find_Set(e[i].x);y = Find_Set(e[i].y);if(x != y){sum += e[i].w;Merge_Set(e[i].x, e[i].y);}}printf("%d\n", sum);}return 0;}
Highways
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18016 Accepted: 8372
Description
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system.
Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.
The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
Input
The first line of input is an integer T, which tells how many test cases followed.
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.
The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.
Output
For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
Sample Input
130 990 692990 0 179692 179 0
Sample Output
692
#include <iostream>#include <algorithm>#include <cstdio>#include <cmath>using namespace std;#pragma warning(disable : 4996)#define MAX 100000typedef struct edge{int x, y;int w;}edge;edge e[MAX];int father[MAX], ranks[MAX];bool cmp(edge a,edge b){return a.w < b.w;}void Make_Set(int n){for(int i = 1; i <= n; i++){father[i] = i;ranks[i] = 0;}}int Find_Set(int x){if(x != father[x])father[x] = Find_Set(father[x]);return father[x];}void Merge_Set(int x, int y){x = Find_Set(x);y = Find_Set(y);if(x == y) return;if(ranks[x] > ranks[y]){father[y] = x;}else if(ranks[x] < ranks[y]){father[x] = y;}else {ranks[y]++;father[x] = y;}}int main(){freopen("in.txt", "r", stdin);int t, n, x, y, count = 0;scanf("%d", &t);while(t--){scanf("%d", &n);Make_Set(n);count = 0;for (int i = 1; i <= n; i++){for(int j = 1; j <= n; j++){scanf("%d", &x);if(i != j){e[count].x = i;e[count].y = j;e[count++].w = x;}}}sort(e, e + count, cmp);int ans;for (int i = 0; i < count; i++){x = Find_Set(e[i].x);y = Find_Set(e[i].y);if(x != y){ans = e[i].w;Merge_Set(e[i].x, e[i].y);}}printf("%d\n", ans);}return 0;}
- hdoj_1102Constructing Roads(最小生成树)&& poj_2485Highways
- Constructing Roads(最小生成树)
- Jungle Roads(最小生成树)
- HDU1102 Constructing Roads(最小生成树)
- HDU1102 Constructing Roads(最小生成树)
- hdoj1102_Constructing Roads(最小生成树)
- hdoj Constructing Roads(最小生成树)
- Constructing Roads(最小生成树)
- poj2421Constructing Roads(最小生成树)
- Constructing Roads (最小生成树 Kruskal)
- POJ11251---Jungle Roads(最小生成树)
- Roads and Libraries(最小生成树)
- Jungle Roads 最小生成树(kruskal)
- Constructing Roads (最小生成树)
- Building Roads(prim 最小生成树)
- Jungle Roads(最小生成树)
- HDU1102_Constructing Roads(最小生成树)
- POJ1251_Jungle Roads(最小生成树)
- 清理c盘
- 联表查询
- Hibernate 延迟加载介绍
- Byte Order
- Linux常用缩写
- hdoj_1102Constructing Roads(最小生成树)&& poj_2485Highways
- c# vsto-格式文本内容控件自身短板
- 『精品素材』35套免费的 Web 和 Moible 素材套件《上集》
- 常用和不常用端口一览表
- Spring中常用的hql查询方法(getHibernateTemplate())
- __builtin_prefetch()
- AndEngine 学习系列1-开始
- NSObject的奥秘
- csdn推荐文章 & firefox同步