[POJ] [1751] Highways

Problem Description

Loading...

Problem Analysis

Looks like this is my first time encountering a Special Judge problem
That is, the answer isn’t unique… hmm… for this problem, maybe the arrangement of answer positions isn’t unique
By the way, writing such a judging program doesn’t seem easy either…
This problem also has only one test case…

The problem itself is quite simple
It just requires outputting the coordinates of the connections
Here I used a modified version of Prim

I used an array to record the two endpoints of each updated edge
When the edge value is not 0, just output the two endpoints of that edge

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct xy {
	double x,y;
} a[800];
double e[800][800];
int main() {
	double dis[800];

	bool flag[800];
	int vs[800];
	int N,inf=999999;
	scanf("%d",&N);
	for(int i=1; i<=N; i++) {
		dis[i]=inf;
		flag[i]=false;
	}
	for(int i=1; i<=N; i++) {
		for(int j=1; j<=N; j++) {
			e[i][j]=inf;
		}
	}
	for(int i=1; i<=N; i++)
		scanf("%lf %lf",&a[i].x,&a[i].y);
	for(int i=1; i<=N; i++) {
		for(int j=1; j<=N; j++) {
			if(i==j)
				continue;
			else {
				double l=sqrt(pow(a[i].x-a[j].x,2)+pow(a[i].y-a[j].y,2));
				e[i][j]=l;
				e[j][i]=l;
			}
		}
	}
	int n;
	scanf("%d",&n);
	while(n--) {
		int u,v;
		scanf("%d %d",&u,&v);
		e[u][v]=0;
		e[v][u]=0;
	}

	dis[1]=0;
	while(true) {
		int v=-1;
		for(int u=1; u<=N; u++) {
			if(!flag[u]&&(v==-1||dis[u]<dis[v]))
				v=u;
		}
		if(v==-1)
			break;
		flag[v]=true;
		if(v!=1&&e[v][vs[v]]!=0&&e[vs[v]][v]!=0)
			printf("%d %d\n",vs[v],v);
		for(int u=1; u<=N; u++) {
			if(dis[u]>e[v][u])
				vs[u]=v;
			dis[u]=min(dis[u],e[v][u]);
		}
	}


	return 0;
}

Problem link: POJ 1751 Highways

Loading...