[POJ] [1287] Networking

Problem Description

Loading...

Problem Analysis

After finishing the hard ones…
this problem feels really trivial…
Just note that there are duplicate edges
so you need to check for them

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
#include<stdio.h>
#include<algorithm>
using namespace std;
int main() {
	int dis[55];
	int e[55][55];
	bool flag[55];
	int N,inf=999999;
	while(scanf("%d",&N),N) {
		int T;
		scanf("%d",&T);
		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;
			}
		}
		while(T--) {
			int n,m,t;
			scanf("%d %d %d",&n,&m,&t);
			if(t<e[n][m]) {
				e[n][m]=t;
				e[m][n]=t;
			}
		}
		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 link: 【POJ】[1287]Networking

Loading...