[POJ] [1789] Truck History

Problem Description

Loading...

Problem Analysis

Essentially, the weight from one string to another is the number of characters that differ between them. Then we need the minimum total weight sum, and output 1/sum as required.

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
#include<stdio.h>
#include<algorithm>
using namespace std;
int e[2200][2200];
char s[2200][7];
int main() {
	int dis[2200];
	bool flag[2200];
	int N,inf=999999;
	while(scanf("%d",&N),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("%s",s[i]);
		for(int i=1; i<=N; i++) {
			for(int j=1; j<=N; j++) {
				if(i==j)
					continue;
				int cnt=0;
				for(int k=0; k<7; k++) {
					if(s[j][k]!=s[i][k])
						cnt++;
				}
				e[i][j]=e[j][i]=cnt;
			}
		}
		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("The highest possible quality is 1/%d.\n",sum);
	}
	return 0;
}

Problem link: POJ 1789 Truck History

Loading...