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.7 文件操作
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 文件操作 # r 只读,默认打开方式,当文件不存在时会报错 # ...
- SQL语句小练习
一.创建如下表结构(t_book) Id 主键 自增一 bookName 可变长 20 Price 小数 Author 可变长20 bookTypeId 图书类 ...
- Python9-进程池-day38
复习 # 信号量 from multiprocessing import Semaphore # 用锁的原理实现的,内置了一个计数器 # 在同一个事件,只能有指定数量的进程执行某一段被控制住的代码 # ...
- Linux学习-透过 systemctl 管理服务
透过 systemctl 管理单一服务 (service unit) 的启动/开机启动与观察状态 一般来说,服务的启动有两个阶段,一 个是『开机的时候设定要不要启动这个服务』, 以及『你现在要不要启动 ...
- Android自动化测试如何获取坐标点?
有以下三种方法: 1.打开开发者选项中的“显示指针位置”: 英文文版本为Settings->Developer option->Show touches(on)->Pointer l ...
- UVa 10564 DP Paths through the Hourglass
从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. #include <cstdio> #include &l ...
- WIN 备份 重装
title: WIN 备份 重装 date: 2018-09-01 22:35:31 updated: tags: [windows,记录,折腾] description: keywords: com ...
- 记一次WMS的系统改造(3)— 行进中的复盘
行进中的波折 革新总会面对一些阻力和风险,一种新的观念.一种新的模式要来替代既有的产品,从来都不是一件简单的事,在WMS改造这件事上我们一开始就提出两种概念货物驱动和任务驱动,并找到一个标杆产品Sla ...
- LRESULT CALLBACK WndProc 窗口程序的 重点
LRESULT CALLBACK WndProc Windows程序所作的一切,都是回应发送给窗口消息处理程序的消息.这是概念上的主要难点之一,在开始写作Windows程序之前,必须先搞清楚. 窗口消 ...
- Selenium WebDriver-获取与切换浏览器窗口的句柄
通过selenium webdriver去切换浏览器的窗口,需要通过句柄,具体代码如下: #encoding=utf-8 import unittest import time import char ...