POJ-1751 Highways
来源:互联网 发布:linux 计算文件大小 编辑:程序博客网 时间:2024/06/13 15:16
原题链接
Highways
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 12583 Accepted: 3623 Special Judge
Description
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more 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 and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. 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 cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.
Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. 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 cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.
Input
The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.
The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.
The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.
The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of ith town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.
The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.
Output
Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space.
If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.
Sample Input
91 50 0 3 24 55 10 45 21 25 331 39 71 2
Sample Output
1 63 74 95 78 3
prime算法求最小生成树。先把已经相连的点连上,在用prime算法要把一个点加入集合时,把该点所连接的所有点都加入集合。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue> #include <cmath>#define maxn 800#define INF 1<<30using namespace std;typedef long long ll;int d[maxn];int vis[maxn], pre[maxn];int x[maxn], y[maxn], cnt, n;vector<int> v[maxn];int f(int i, int j){return (x[i] - x[j]) * (x[i] - x[j]) + (y[j] - y[i]) * (y[j] - y[i]);}void dfs(int j){vis[j] = 1;cnt++;for(int i = 1; i <= n; i++){if(!vis[i]){int h = f(i, j);if(h < d[i]){d[i] = h;pre[i] = j;}}}for(int i = 0; i < v[j].size(); i++){int h = v[j][i]; if(!vis[h]){dfs(h);}}}int main(){//freopen("in.txt", "r", stdin);while(scanf("%d", &n) == 1){int m, a, b;for(int i = 1; i <= n; i++) v[i].clear();memset(vis, 0, sizeof(vis));for(int i = 1; i <= n; i++) d[i] = INF;for(int i = 1; i <= n; i++){scanf("%d%d", x+i, y+i);}scanf("%d", &m);for(int i = 1; i <= m; i++){scanf("%d%d", &a, &b);v[a].push_back(b);v[b].push_back(a);}cnt = 0;dfs(1);while(cnt != n){int mins = INF;int p;for(int i = 1; i <= n; i++){if(!vis[i])if(d[i] < mins){mins = d[i];p = i;}}printf("%d %d\n", pre[p], p);dfs(p);}}return 0;}
0 0
- poj 1751 Highways
- poj 1751 highways
- Poj 1751 Highways(prim)
- poj 1751 Highways
- poj 1751 Highways MST
- POJ-1751-Highways
- POJ:1751 Highways
- POJ 1751 Highways
- POJ-1751 Highways
- poj-1751-Highways
- POJ 1751 Highways
- poj 1751 Highways
- POJ 1751 Highways
- poj-1751 Highways
- POJ 1751 Highways
- POJ - 1751 Highways
- POJ 1751 Highways
- poj 1751 Highways
- Hadoop学习笔记:MapReduce框架详解
- SQL:find duplicate rows -- using group or having
- Swift Package Manager快速上手指南(一):环境搭建
- 6月的天
- 路由器配置DHCP服务
- POJ-1751 Highways
- 谷歌技术"三宝"之MapReduce
- Hello,服务端 Swift
- 通过Nginx部署Django(基于ubuntu)
- 谷歌技术"三宝"之谷歌文件系统
- 谷歌技术"三宝"之BigTable
- http://blog.csdn.net/change518/article/details/8004827
- 装好Centos7后提示Initial setup of CentOS Linux 7 (core)
- Shader 妖怪手表放大镜 20160619