poj 1789 Truck History 最小生成树 prim 难度:0
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 19122 | Accepted: 7366 |
Description
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
Output
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
Source
#include <iostream>
#include <cstring> using namespace std;
char str[2001][9];
int d[2001][2001];
int mind[2001];//mind restore the min distance of the point between the tree when the point hasn't been add into
//rather than the point in the tree to all the point not in the tree
bool vis[2001];
int n;
int ans;
int index;
void setd(){
for(int i=0;i<n;i++){
d[i][i]=0;
for(int j=i+1;j<n;j++){
d[i][j]=d[j][i]=0;
for(int k=0;k<7;k++){
if(str[i][k]!=str[j][k]){
d[i][j]++;
}
}
d[j][i]=d[i][j];
}
}
}
void prim(){
memset(mind,0x3f,sizeof(mind));
memset(vis,0,sizeof(vis));
index=1;
vis[0]=true;
ans=0;
int s=0;
while(index<n){
int minnum=0x3f3f;
int mini=0;
for(int i=0;i<n;i++){
if(!vis[i]&&d[s][i]<mind[i]){
mind[i]=d[s][i];//use i to refresh all the point
}
if(!vis[i]&&mind[i]<minnum){
minnum=mind[i];
mini=i;
}
}
ans+=minnum;
vis[mini]=true;
s=mini;
index++;
}
}
int main(){
ios::sync_with_stdio(false);
while(cin>>n&&n){
for(int i=0;i<n;i++){
cin>>str[i];
}
setd();
prim();
cout<<"The highest possible quality is 1/"<<ans<<".\n";
} return 0;
}
poj 1789 Truck History 最小生成树 prim 难度:0的更多相关文章
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- POJ 1789 Truck History(Prim+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
- POJ 1789 Truck History【最小生成树简单应用】
链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- poj 1789 Truck History【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- POJ 1789 Truck History (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
随机推荐
- 解决 Ubuntu 下 Sublime Text 无法输入中文的问题
解决 Ubuntu 下 Sublime Text 无法输入中文的问题 1. 安装依赖库 sudo apt-get install build-essential sudo apt-get instal ...
- PHP函数处理方法总结
call_user_func_array (PHP 4 >= 4.0.4, PHP 5, PHP 7) call_user_func_array — 调用回调函数,并把一个数组参数作为回调函数的 ...
- python + unittest 做单元测试之学习笔记
单元测试在保证开发效率.可维护性和软件质量等方面有很重要的地位,所谓的单元测试,就是对一个类,一个模块或者一个函数进行正确性检测的一种测试方式. 这里主要是就应用 python + unitest 做 ...
- C#之父
来自为知笔记(Wiz)
- Smarty 模板布局继承
Smarty 模板继承 在覆盖父模板的{block}块以外的地方, 子模板不能定义任何内容.任何在{block}以外的 内容都会被自动忽略. 在子模板和父模板中的{block}内容,可以通过 appe ...
- java Pattern(正则)类
Pattern的静态方法matches 用于快速匹配字符串,该方法适合用于只匹配一次,且匹配全部字符串. Boolean b=Pattern.matches("^((13[0-9])|(15 ...
- centos远程访问mssql数据库
http://blog.path8.net/archives/5921.html http://www.jaggerwang.net/2013/03/18/centos%E4%B8%8B%E5%AE% ...
- la5135 无向图 点-双连通 运用
大白书 P314 #include <iostream> #include <algorithm> #include <string.h> #include < ...
- hdu5057 分块处理,当数值大于数据范围时树状数组 真是巧 将大数据分为小数据来处理
这题说的给了100000个数有100000次操作 询问 L和R 区间内 在D位上为P的个数,用树状数组存 要开[10][10][100000]的int 开不了但是能开 这么大的unsign short ...
- 通过J2EE Web工程添加Flex项目,进行BlazeDS开发
http://www.cnblogs.com/noam/archive/2010/07/22/1782955.html 环境:Eclipse 7.5 + Flex Builder 4 plugin f ...