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 ...
随机推荐
- Altium Designer入门学习笔记4:PCB设计中各层的含义
阻焊层:solder mask,是指板子上要上绿油的部分:因为它是负片输出,所以实际上有solder mask的部分实际效果并不上绿油,而是镀锡,呈银白色! 助焊层:paste mask,是机器贴片时 ...
- LeetCode(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- PAT Basic 1073
1073 多选题常见计分法 批改多选题是比较麻烦的事情,有很多不同的计分方法.有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到 50% 分数:如果考生选择了任何一 ...
- Service 回顾
绑定本地service需要实现onBind()方法
- Java-创建一个线程
第一种继承Thread类 package com.tj; public class BasicThread1 extends Thread { public void run() { System.o ...
- python-高级编程-02
[yield 详解 协同程序 生成器表达式] 1> yield def res (): for i in range(10): x = yield i r = res() print r.nex ...
- [python 测试框架学习篇] 分享 uiautomator测试框架
uiautomator测试框架 :https://testerhome.com/topics/4194
- Wannafly模拟赛2
Contest 时间限制:1秒 空间限制:131072K 题目描述 n支队伍一共参加了三场比赛. 一支队伍x认为自己比另一支队伍y强当且仅当x在至少一场比赛中比y的排名高. 求有多少组(x,y),使得 ...
- java Logger 的使用与配置
原文来自:http://blog.csdn.net/nash603/article/details/6749914 Logger所对应的属性文件在安装jdk目录下的jre/lib/logging.pr ...
- No change while using CSS <form target="blank">
I want to open another Window when I use <form> to request a query. So I used target="bla ...