Poj 1789 truck history

Source: Internet
Author: User
Truck historytime limit: 2000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 1789
64-bit integer Io format: % LLD Java class name: Main 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 (role t for the first truck type which was not derived from any other type ). the quality of a derivation plan was then defined
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 shocould find the highest possible quality of a derivation plan.
Inputthe 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. outputfor each test case, your program shocould output the text "the highest possible quality is 1/Q. ", where 1/Q is the quality of the best derivation plan. sample Input
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample output
The highest possible quality is 1/3.
Sourcectu open 2003: Minimum Spanning Tree. The distance between any two strings is the number of characters in the corresponding positions of the two strings.
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 int n,mp[2010][2010],d[2010];18 char s[2010][10];19 bool done[2010];20 priority_queue< pii,vector< pii >,greater< pii > >q;21 int calc(int i,int j){22     int tmp = 0;23     for(int k = 0; k < 7; k++)24         if(s[i][k] != s[j][k]) tmp++;25     return tmp;26 }27 int prim(){28     int ans = 0;29     for(int i = 0; i <= n; i++){30         done[i] = false;31         d[i] = INF;32     }33     while(!q.empty()) q.pop();34     d[1] = 0;35     q.push(make_pair(d[1],1));36     while(!q.empty()){37         int u = q.top().second;38         int w = q.top().first;39         q.pop();40         if(done[u]) continue;41         done[u] = true;42         ans += w;43         for(int i = 1; i <= n; i++){44             if(d[i] > mp[u][i]){45                 d[i] = mp[u][i];46                 q.push(make_pair(d[i],i));47             }48         }49     }50     return ans;51 }52 int main() {53     int i,j;54     while(scanf("%d",&n),n){55         for(i = 0; i <= n; i++){56             for(j = 0; j <= n; j++)57                 mp[i][j] = INF;58         }59         for(i = 1; i <= n; i++)60             scanf("%s",s[i]);61         for(i = 1; i <= n; i++){62             for(j = i+1; j <= n; j++)63                 mp[i][j] = mp[j][i] = calc(i,j);64         }65         printf("The highest possible quality is 1/%d.\n",prim());66     }67     return 0;68 }
View code

 

Poj 1789 truck history

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.