ZOJ 2048(Prim 或者 Kruskal)

时间:2023-03-09 05:23:40
ZOJ 2048(Prim 或者 Kruskal)

Highways


Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

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.

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 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 should be created but it should be empty.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed
by N input blocks. Each input block is in the format indicated in the problem
description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between
output blocks.


Sample Input

1

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

收获:第一次用prim,了解了下prim模板。

方法1:prim

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);
#define maxn 1000
struct Node
{
    int x, y;
};
Node node[maxn];
int vis[maxn];
int n;
int dis[maxn];
int pre[maxn];
int map1[maxn][maxn];
void Prim(){
    int i,j,k,tmp,ans;
    memset(vis, 0, sizeof vis);
    for(i=2;i<=n;i++)
    {
        dis[i] = map1[1][i];
        pre[i] = 1;
    }
    dis[1]=0;
    vis[1]=1;
    for(i=1;i<n;i++){
        tmp=INF; k=1;
        for(j=1;j<=n;j++){
            if(!vis[j]&&tmp>dis[j]){
                tmp=dis[j];
                k=j;
            }//找出最小距离的节点
        }
        vis[k]=1;//把访问的节点做标记
        for(j=1;j<=n;j++){
            if(!vis[j]&&dis[j]>map1[k][j])
            {
                dis[j]=map1[k][j];
                pre[j]=k;
            }//更新与k相邻的最短距离
        }
    }
    for(int i = 2; i <= n; i++)
    {
        if(map1[pre[i]][i] != 0)
        {
            printf("%d %d\n",i, pre[i]);
        }
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        scanf("%d%d", &node[i].x, &node[i].y);
        int a, b;
        int m;
        scanf("%d", &m);
        memset(map1, INF,sizeof map1);
        for(int i = 1; i <= n ; i++)
            for(int j = i+1; j <= n; j++)
            map1[i][j] = map1[j][i] = (node[i].x - node[j].x)*(node[i].x - node[j].x) + (node[i].y - node[j].y)*(node[i].y - node[j].y);
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d", &a, &b);
            map1[a][b] = map1[b][a] = 0;
        }
        Prim();
        if(t)
            puts("");
    }
    return 0;
}

2.Kruskal

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 570000
struct Node
{
int x, y;
};
Node node[maxn];
struct Edge
{
int u, v ,w;
bool operator < (const Edge &a) const
{
return w < a.w;
}
};
Edge edge[maxn];
int root[maxn];
int num;
void addedge(int u, int v)
{
edge[num].u = u;
edge[num].v = v;
edge[num].w = (node[u].x - node[v].x)*(node[u].x - node[v].x) + (node[u].y - node[v].y)*(node[u].y - node[v].y);
num++;
}
int n;
void init_root()
{
for(int i = ; i <= n; i++)
root[i] = i;
}
int find_root(int x)
{
int k,j,r;
r=x;
while(r!=root[r])
r=root[r];
k=x;
while(k!=r)
{
j=root[k];
root[k]=r;
k=j;
}
return r;
}
void uni(int a, int b)
{
int x = find_root(a);
int y = find_root(b);
//printf("%d %d\n", x, y);
if(x == y)
return;
else
root[y] = x;
}
int cnt;
void solve()
{
for(int i = ; i < num; i++)
{
int u = find_root(edge[i].u);
int v = find_root(edge[i].v); if(u != v)
{
root[v] = u;
cnt++;
printf("%d %d\n", edge[i].u, edge[i].v);
}
if(cnt == n-)
break;
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
scanf("%d%d", &node[i].x, &node[i].y);
int a, b;
num = ;
int m;
scanf("%d", &m);
cnt = ;
init_root();
for(int i = ; i < m; i++)
{
scanf("%d%d", &a, &b);
if(find_root(a) != find_root(b))
{
cnt++;
uni(a, b);
}
}
for(int i = ; i <= n ; i++)
for(int j = i+; j <= n; j++)
addedge(i, j);
sort(edge, edge+num);
solve();
if(t)
puts("");
}
return ;
}