POJ 1789 Truck History(Prim)
来源:互联网 发布:java log4j用法 编辑:程序博客网 时间:2024/05/22 20:27
Truck History
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18124 Accepted: 7000
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.
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
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
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample Output
The highest possible quality is 1/3.
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#define MAX 99999999using namespace std;int a[2001][2001];char str[2001][10];int b[100001];int v[100001];int n;void Prim(){ int i,j,k; memset(b,0,sizeof(b)); for(i=0;i<n;i++) v[i]=MAX; for(i=0;i<n;i++) v[i]=a[0][i]; v[0]=0; b[0]=1; int min=MAX; int sum=0; for(i=0;i<n-1;i++) { min=MAX; for(j=0;j<n;j++) { if(!b[j]&&v[j]<min) { min=v[j]; k=j; } } sum+=min; b[k]=1; for(j=0;j<n;j++) { if(!b[j]&&v[j]>a[k][j]) v[j]=a[k][j]; } } printf("The highest possible quality is 1/%d.\n",sum);}int main(){ while(scanf("%d",&n),n) { getchar(); int i,j; for(i=0;i<n;i++) gets(str[i]); for(i=0;i<n;i++) { for(j=0;j<n;j++) { a[i][j]=MAX; } a[i][i]=0; } int s=0; int k=0; for(i=0;i<n;i++) { k=0; while(k<n) { for(j=0;j<7;j++) { if(str[i][j]!=str[k][j]) s++; } a[i][k]=s; s=0; k++; } } Prim(); } return 0;}
0 0
- poj 1789 Truck History(prim算法)
- poj 1789 Truck History(prim)
- POJ 1789 Truck History(Prim)
- POJ 1789 Truck History (Prim Algorithm)
- poj 1789 Truck History(prim)
- POJ 1789 Truck History Prim
- poj-1789 Truck History-Prim
- poj 1789 Truck History(Prim)
- POJ--1789 Truck History【prim】
- Truck History Poj 1789(Prim)
- POJ 1789:Truck History 【Prim】
- POJ 1789 Truck History【Prim】
- POJ 1789 Truck History (Prim)
- poj--1789--Truck History(prim)
- poj 1789 Truck History (Prim)
- poj 1789 Truck History(prim)
- POJ - 1789----Truck History(prim)
- Truck History( poj 1789 prim)
- OpenCV学习之例程详解(03):搜索文件夹下所有指定类型图片并逐张显示
- Cocos2d-x 3.1.1 之 MessageBox、LabelTTF、菜单、
- 自定义控件---------ViewPager的一个小案例
- 关于内核空间地址映射问题
- 九度OJ-题目1010:A + B
- POJ 1789 Truck History(Prim)
- linux驱动____LED子系统笔记
- C++笔试题目大全
- POJ 1287 Networking(最小生成树)
- HDU 4902 Nice boat
- C# 图片和byte[]的互相转换
- EBS条形码打印
- 声明与函数、函数指针
- 【HDU 4901 多校联合】The Romantic Hero【DP】