您的位置:首页 > 其它

POJ - 1751 Highways(最小生成树 prim算法)

2018-03-17 20:24 579 查看
Highways
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18470 Accepted: 5434 Special Judge
DescriptionThe 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. 
InputThe 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. 
OutputWrite 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. 
Sample Input9
1 5
0 0
3 2
4 5
5 1
0 4
5 2
1 2
5 3
3
1 3
9 7
1 2Sample Output1 6
3 7
4 9
5 7
8 3
Code#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define N 1100
#define INF 0x3f3f3f3f

using namespace std;

int map

;
int dis
;
int vis
;

struct node
{
int x, y;
} s
;

int getDist(int i, int j)
{
///因为不求具体权值和,所以不需要sqrt,只要有大小区分即可
return ((s[i].x - s[j].x) * (s[i].x - s[j].x) + (s[i].y - s[j].y) * (s[i].y - s[j].y));
}

void prim(int n)
{
int i,j,pos,edge
;
int min;
for(i=1; i<=n; i++)
{
dis[i] = map[1][i];
vis[i] = 0;
edge[i] = 1;
}
vis[1] = 1;
for(i=1; i<=n; i++)
{
min = INF;
for(j=1; j<=n; j++)
{
if(!vis[j] && dis[j] < min)
{
min = dis[j];
pos = j;
}
}
if(min != INF)
{
vis[pos] = 1;
for(j=1; j<=n; j++)
{
if(!vis[j] && dis[j] > map[pos][j])
{
dis[j] = map[pos][j];
edge[j] = pos;
}
}
if(map[edge[pos]][pos])
{
printf("%d %d\n",edge[pos],pos);
}
}
}
}

int main()
{
int i,j,n,m,u,v;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf("%d %d",&s[i].x, &s[i].y);
}
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
map[i][j] = map[j][i] = getDist(i,j);
}
}
scanf("%d",&m);
for(i=1; i<=m; i++)
{
scanf("%d %d",&u,&v);
map[u][v] = map[v][u] = 0;    ///已有公路连接
}
prim(n);
return 0;
}
反思:题目大意:Flatopia是一个(因为地表太平坦可以看做二维图的)岛屿国家,有N个城镇,打算建立一些高速公路来连通城市,并且政府希望成本尽量低,所以要使所有公路造价最低。给出城市数目N和N组城市的坐标,之后是已经建立的公路M条和它们连接的两个城市。最小生成树prim做法,并不求最低造价,改求公路连接的两个城市。已经有公路连通的两个城市之间距离设为0,因为要记录前驱城市,另开一个edge数组来存。因为这题有Special Judge,只要把连通的城市都输出即可,不需要和样例一样~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: