poj--1789--Truck History(prim)
| Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
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 t
o is the original type and t d the type derived from it and d(t
o,t d) 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
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
Sample Input
4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0
Sample Output
The highest possible quality is 1/3.
Source
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int map[1010][1010];
int vis[1010];
char str[1010][9];
int n;
int prim()
{
int sum=0;
int minn,k;
for(int i=1;i<n;i++)
{
minn=100000;
for(int j=2;j<=n;j++)
{
if(!vis[j]&&map[1][j]<minn)
{
minn=map[1][j];
k=j;
}
}
vis[k]=1;
sum+=minn;
for(int j=2;j<=n;j++)
{
if(!vis[j]&&map[k][j]<map[1][j])
map[1][j]=map[k][j];
}
}
return sum;
}
int main()
{
while(scanf("%d",&n),n)
{
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
memset(str,'\0',sizeof(str));
for(int i=1;i<=n;i++)
scanf("%s",str[i]);
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
for(int k=0;k<7;k++)
{
if(str[i][k]!=str[j][k])
map[i][j]++;
}
map[j][i]=map[i][j];
}
}
printf("The highest possible quality is 1/%d.\n",prim());
}
return 0;
}
poj--1789--Truck History(prim)的更多相关文章
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- 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 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- 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 (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- 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(Prim+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
随机推荐
- angular-Then的用法
then怎么使用(主要是如何从中提取出我们需要的后台返回的数据):then(fn) 方法中带一个参数,这个参数就是要被执行的函数,并且,这个作为参数的函数本身有一个参数,这个参数就是我们需要的数据,这 ...
- angular-基础
AngularJs特点: 1.依赖注入 2.模块化 3.双向绑定 4.语义化标签 当网页加载完毕,AngularJS 自动开启. ng-app 指令告诉 AngularJS,<div> 元 ...
- hello world to php( mac 配置 xmapp virtual host)
一.安装xmapp.安装完以后查看,服务是否都能启动(数据库和server) 二.配置自己的virtualhost 1.系统host文件加入server的域名(在浏览器中输入域名后会先通过 ...
- leetcode第一刷_Subsets II
要求子集,有很现成的方法.N个数.子集的个数是2^N.每一个元素都有在集合中和不在集合中两种状态,这些状态用[0,pow(2,N)]中每一个数来穷举,假设这个数中的第i位为1,说明当前集合中包括源数组 ...
- IIS身份验证的配置
前4者配置:localhost applicationHost.config <location path=""> 后2者配置:web.config 要点: 这6项尽管 ...
- HDU 1051: Wooden Sticks(贪心)
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- JAVA设计模式之【外观模式】
通过引入一个外观角色来简化客户端与子系统之间的交互. 顾客无需直接和茶叶.茶具.开水等交互,整个泡茶过程由服务员来完成,顾客只需与服务员交互即可. 通过引入一个外观角色可以降低原有系统的复杂度,同时降 ...
- pgsql数据库备份还原记
今天又搞了一个pgsql 的备份还原,差一点没有成功,以前总是想当然的用,没认真想背后的东西,也没对过程中的疑问做记录,所以后面也没什么印象,常见常新,这次既然又遇到就总结一下. 之前操作pgsql数 ...
- pjlib深入剖析和使用详解
1. PJSIP简介 PJSIP的实现是为了能在嵌入式设备上高效实现SIP/VOIP.其主要特征包括: 1).极具移植性.(Extremely portable) ...
- [poj 3349] Snowflake Snow Snowflakes 解题报告 (hash表)
题目链接:http://poj.org/problem?id=3349 Description You may have heard that no two snowflakes are alike. ...