Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 18470 | Accepted: 5434 | Special Judge |
Description
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 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
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
9 1 5 0 0 3 2 4 5 5 1 0 4 5 2 1 2 5 3 3 1 3 9 7 1 2
Sample Output
1 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[N][N];
int dis[N];
int vis[N];
struct node
{
int x, y;
} s[N];
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[N];
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,只要把連通的城市都輸出即可,不需要和樣例一樣~