Truck History(最小生成树)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27703 | Accepted: 10769 |
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<cstdlib> #include<stdio.h> #include<cstring> #include<iostream> #include<algorithm> #define N 2001 using namespace std; struct Edge { int x,y,z; }edge[N*N]; int n,m,fa[N],ans,sum,tot; ]; int cmp(Edge a,Edge b) { return a.z<b.z; } int found(int x) { return fa[x]==x?x:fa[x]=found(fa[x]); } int main() { while(scanf("%d",&n),n) { memset(a,,sizeof(a)); ans=; ;i<=n;i++) cin>>a[i]; tot=; ;i<n;i++) ;j<=n;j++) { sum=; ;k<;k++) if(a[i][k]!=a[j][k]) sum++; edge[++tot].x=i; edge[tot].y=j; edge[tot].z=sum; } sort(edge+,edge++tot,cmp); ;i<=n;i++) fa[i]=i; sum=; ;i<=tot;i++) { int x=edge[i].x,y=edge[i].y; int fx=found(x),fy=found(y); if(fx!=fy) { fa[fy]=fx; sum++; ans+=edge[i].z; } ) break; } printf("The highest possible quality is 1/%d.\n",ans); } ; }
Truck History(最小生成树)的更多相关文章
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- poj 1789 Truck History 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- poj1789 Truck History最小生成树
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20768 Accepted: 8045 De ...
- POJ 1789 Truck History (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- POJ 1789:Truck History(prim&&最小生成树)
id=1789">Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17610 ...
- 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【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- POJ1789 Truck History 【最小生成树Prim】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18981 Accepted: 7321 De ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
随机推荐
- python3爬取豆瓣top250电影
需求:爬取豆瓣电影top250的排名.电影名称.评分.评论人数和一句话影评 环境:python3.6.5 准备工作: 豆瓣电影top250(第1页)网址:https://movie.douban.co ...
- LeetCode(306) Additive Number
题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...
- stm32启动地址
理论上,CM3中规定上电后CPU是从0地址开始执行,但是这里中断向量表却被烧写在0x0800 0000地址里(Flash memory启动方式),那启动时不就找不到中断向量表了?既然CM3定下的规矩是 ...
- Linux和 Mac下git pull/push 免输入密码和账号
linux下面可以直接创建.git-credential文件,命令如下: 创建文件,进入文件,输入内容: cd ~ touch .git-credentials vim .git-credential ...
- 2015四川省赛 D Vertex Cover 搜索
题意: 给出一个\(n\)个点\(m\)条边的无向图,现在要给若干个点染色,使得每条边都至少邻接一个被染色的顶点.问至少要给多少各点染色才能满足条件. 分析: 注意到题目中有一个很特殊的条件: 对于图 ...
- Socketserver详解
Python3中的SocketServer socket并不能多并发,只能支持一个用户,socketserver 简化了编写网络服务程序的任务,socketserver是socket的在封装.sock ...
- 面试中注意3个javascript的问题
JavaScript 是所有现代浏览器的官方语言.因此,各种语言的开发者面试中都会遇到 JavaScript 问题. 本文不讲最新的 JavaScript 库,通用开发实践,或任何新的 ES6 函数. ...
- bootstrap3兼容ie8浏览器
bootstrap3 兼容IE8浏览器 2016-01-22 14:01 442人阅读 评论(0) 收藏 举报 分类: html5(18) 目录(?)[+] 近期在使用bootstrap这 ...
- 优秀的缓存请求库,快速请求接口和图片:WTRequestCenter
WTRequestCenter 方便缓存的请求库无需任何import和配置,目前实现了基础需求如果有其他需要请在issue 上提出,谢谢! 使用方法 Usage 注意:所有的请求都是缓存的 GET 请 ...
- Tomcat 禁用PUT方法, 404/500错误重定向, 网站图标
(1) Tomcat禁用Put等不安全方法. <security-constraint> <web-resource-collection> <web-resource- ...