Truck History - poj 1789 (Prim 算法)

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20884   Accepted: 8075

Description

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company‘s history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as 
1/Σ(to,td)d(to,td)
where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) is the distance of the types. 
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.

Input

The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.

Output

For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 #define INF 0x0fffffff;
 5 char str[2000][7];
 6 int map[2000][2000];
 7 int vis[2000];
 8 int d[2000];
 9 int n;
10 int Prim(){
11     for(int i=0;i<n;i++){
12         d[i]=INF;
13         vis[i]=0;
14     }
15     d[0]=0;
16     int sum=0;
17     for(int i=0;i<n;i++){
18         int mi=INF;
19         int p;
20         for(int j=0;j<n;j++){
21             if(!vis[j]&&mi>d[j]){
22                 mi=d[j];
23                 p=j;
24             }
25         }
26         sum+=mi;
27         vis[p]=1;
28         for(int j=0;j<n;j++){
29             if(!vis[j]){
30                 d[j]= min(d[j],map[p][j]);
31             }
32         }
33     }
34     return sum;
35 }
36 int main() {
37     cin>>n;
38     while(n){
39     for(int i=0;i<n;i++){
40         for(int j=0;j<7;j++){
41             cin>>str[i][j];
42         }
43     }
44     for(int i=0;i<n;i++){
45         for(int j=i+1;j<n;j++){
46             int sum=0;
47             for(int k=0;k<7;k++){
48                 if(str[i][k]!=str[j][k]){
49                     sum++;
50                 }
51             }
52             map[i][j]=map[j][i]=sum;
53         }
54     }
55     int sum=Prim();
56     cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
57     cin>>n;
58     }
59     return 0;
60 }
时间: 2024-12-15 21:26:54

Truck History - poj 1789 (Prim 算法)的相关文章

(最小生成树)Truck History --POJ -- 1789

链接: http://poj.org/problem?id=1789 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 2005; const int INF = 0xfffffff

Truck History POJ 1789

http://poj.org/problem?id=1789 题意:简单来说,就是给出一个 n 行的字符串, 字符串与字符串间的距离为它们相照应的比较有几个不同的字母, 然后求出 n 行字符串直接或间接相连中最短的距离为多少.(生成树模板) 写最小生成树的专题真的比之前其他专题感觉好多了.(%>_<%)   #include<stdio.h> #include<string.h> #include<math.h> #include<queue>

poj1789Truck History(最小生成树prim算法)

题目链接: 啊哈哈,点我点我 思路:根据字符串中不同的长度建图,然后求图的最小生成树.. 题目: Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18272   Accepted: 7070 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vege

POJ #1789 Truck History 最小生成树(MST) prim 稠密图 链式向前星

Description 题目:链接 这道题的数据集网上比较少,提供一组自己手写的数据: INPUT: 3 aaaaaaa baaaaaa abaaaaa OUTPUT: The highest possible quality is 1/2. 思路 题意比较不好理解,简而言之就是有 n 个字符串,设两个字符串之间的差异为 dis,dis 由两个字符串对应位置上不同字母的数量决定.比如串A"aaaaaaa" .串B"baaaaaa" 和串C"abaaaaa&

POJ1789 Truck History 【最小生成树Prim】

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18981   Accepted: 7321 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

POJ-1789 Truck History---最小生成树Prim算法

题目链接: https://vjudge.net/problem/POJ-1789 题目大意: 用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一个编号只能由另一个编号"衍生"出来,代价是这两个编号之间相应的distance,现在要找出一个"衍生"方案,使得总代价最小,也就是distance之和最小. 思路: 最小生成树模板题,这里是稠密图,应该用prim算法 直接在原来模板的基础上稍加改动即可 1 #inclu

Agri-Net - poj 1258 (Prim 算法)

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44373   Accepted: 18127 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your h

Highways - poj 2485 (Prim 算法)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24383   Accepted: 11243 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian g

Kuskal/Prim POJ 1789 Truck History

题目传送门 1 /* 2 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 3 Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Prim.这是最小生成数的裸题,然而题目有点坑爹:( 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <string> 8 #include <algorithm> 9 #include