Problem Description
Problem Analysis
The difficulty of this problem is
that the problem has already connected some villages
and asks for the rest
but we can just set the distance between the corresponding villages to 0.
(-.- It is said that someone thought of Union-Find and so on, and then got TLE…)
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
| #include<stdio.h>
#include<algorithm>
using namespace std;
int main() {
int dis[120];
int e[120][120];
bool flag[120];
int N,inf=999999;
while(scanf("%d",&N)!=EOF) {
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++) {
for(int j=1; j<=N; j++) {
if(i==j) {
int t;
scanf("%d",&t);
} else
scanf("%d",&e[i][j]);
}
}
int Q;
scanf("%d",&Q);
while(Q--) {
int n,m;
scanf("%d %d",&n,&m);
e[n][m]=0;
e[m][n]=0;
}
int sum=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;
sum+=dis[v];
for(int u=1; u<=N; u++) {
dis[u]=min(dis[u],e[v][u]);
}
}
printf("%d\n",sum);
}
return 0;
}
|
Problem address:POJ 2421 Constructing Roads