(最小生成树)Truck History --POJ -- 1789
链接:
http://poj.org/problem?id=1789
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 22133 | Accepted: 8581 |
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.
代码:
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <cmath>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- const int N = ;
- const int INF = 0xfffffff;
- int n;
- int J[N][N], dist[N];
- char s[N][];
- bool vis[N];
- int Len(char a[], char b[])
- {
- int ans=;
- for(int i=; i<; i++)
- {
- if(a[i]!=b[i])
- ans++;
- }
- return ans;
- }
- int Prim()
- {
- int i, j, ans=;
- memset(vis, , sizeof(vis));
- vis[]=;
- dist[]=;
- for(i=; i<=n; i++)
- dist[i]=J[][i];
- for(i=; i<n; i++)
- {
- int MIN=INF;
- int index=;
- for(j=; j<=n; j++)
- {
- if(!vis[j] && dist[j]<MIN)
- {
- index=j;
- MIN=dist[j];
- }
- }
- vis[index]=;
- if(MIN==INF)
- break;
- ans += MIN;
- for(j=; j<=n; j++)
- {
- if(!vis[j] && dist[j]>J[index][j] && J[index][j]> )
- dist[j]=J[index][j];
- }
- }
- return ans;
- }
- int main ()
- {
- while(scanf("%d", &n), n)
- {
- int i, j;
- memset(s, , sizeof(s));
- for(i=; i<=n; i++)
- scanf("%s", s[i]);
- for(i=; i<=n; i++)
- for(j=; j<=i; j++)
- {
- J[i][j]=J[j][i]=Len(s[i], s[j]);
- }
- int ans=Prim();
- printf("The highest possible quality is 1/%d.\n", ans);
- }
- return ;
- }
(最小生成树)Truck History --POJ -- 1789的更多相关文章
- Truck History - poj 1789 (Prim 算法)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20884 Accepted: 8075 Description Ad ...
- Truck History POJ - 1789
题目链接:https://vjudge.net/problem/POJ-1789 思路: 题目意思就是说,给定一些长度为7的字符串,可以把字符串抽象为一个点, 每个点之间的距离就是他们本身字符串与其他 ...
- F - Truck History - poj 1789
有一个汽车公司有很多年的汽车制造历史,所以他们会有很多的车型,现在有一些历史学者来研究他们的历史,发现他们的汽车编号很有意思都是有7个小写字母组成的,而且这些小写字母具有一些特别的意义,比如说一个汽车 ...
- Truck History POJ - 1789 板子题
#include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> u ...
- poj 1789 prime
链接:Truck History - POJ 1789 - Virtual Judge https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...
- 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(prim&&最小生成树)
id=1789">Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17610 ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
随机推荐
- 使用flash导出图集动画到unity
1.选中要导出的元件,元件所有动作要对齐,右键导出Sprite Sheet.. 2.设置如下 3.复制导出的png图片到unity,对图片进行网格裁剪,网格宽高在plist文件中:
- TCP/IP知识总结(TCP/IP协议族读书笔记三)
接下来,总结传输层的两大协议UDP和TCP. 一.UDP(用户数据报协议) 讲UDP之间,先了解两个概念:有连接和无连接. 有连接:通信之前,通信双方必须建立一条通道: 无连接:不需要建立通道,发送方 ...
- Java中static代码块,{}大括号代码块,构造方法代码块执行顺序!
注:下列代码中的注释都是JUnit4单元测试运行结果. 首先,没有父类的(父类是Object)的类A package Static.of; public class A { { System.out. ...
- chrome 调试跨域iframe
相关链接:https://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome https://stacko ...
- The maximum column size is 767 bytes (Mysql)
ERROR app.wsutils 419 INCRON: Error: ('HY000', '[HY000] [MySQL][ODBC 5.2(w) Driver][mysqld-5.7.7-rc ...
- intellij idea 的常见配置
1.视图配置 配置好后如下图: 2.修改字体大小 3.编码修改 4.行号显示 5.控制台字体大小调整 File->Settings->Editor->Colors & F ...
- mydqldump
[mydqldump] One way to create a snapshot of the data in an existing master database is to use the my ...
- 记一次JPA查询分页导致的数据丢失问题
使用JPA查询,共17条数据,每页10条数据. 第一页与第二页有一条重复的数据,导致丢失一条数据 后查明原因发现,该查询使用了排序,排序字段的值在多条数据中相同,比如在3-11条是相同的值.此时跳到第 ...
- ASP.NET中修改从数据库获取的datatable中的值
有些时候,我们从数据库表中获取一个实体的对象,但有些内容并不是最终显示的内容,格式也都是不一样.经过一番尝试,发现datatable中的数值如果跟想要改变的类型不一致,就无法更改,只有添加新列,然后把 ...
- mongo嗅探器mongosniff
mongo嗅探器 在更高版本被mongoreplay取代. 安装: 在Ubuntu直接apt-get install mongodb即包含有. 使用方法 直接--help查看使用方法,一般使用: mo ...