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 ...
随机推荐
- Java 编辑html模板并生成pdf
1.工具类 import com.hujiang.project.zhgd.Util; import com.itextpdf.text.BaseColor; import com.itextpdf. ...
- java做http接口
问题描述 我要对外提供一个http接口给别人调用...但是我不知道用java怎么做这个接口.请大家详细给我讲讲.从开发到如何发布到服务器.谢谢了 解决方案 如果你这个很简单的话,而且数量也很少,建议直 ...
- poj 1995 快速幂
题意:给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M. 思路:快速幂 实例 3^11 11=2^0+2^1+2^3 => 3^1*3 ...
- UVa 10655 Contemplation! Algebra 矩阵快速幂
题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...
- HDU 5527 Too Rich 贪心
题意: 有\(10\)种面值为\(1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000\)的纸币,现在你要选最多的数量凑成\(p\)块钱. 分析: 同样分析问题的反面 ...
- webdriver高级应用- 高亮显示正在操作的页面元素
#encoding=utf-8 import unittest from selenium import webdriver import time def highLightElement(driv ...
- 大数据学习——akka自定义RPC
实现 package cn.itcast.akka import akka.actor.{Actor, ActorSystem, Props} import akka.actor.Actor.Rece ...
- 基于 FPGA 的图像边缘检测
本文主要内容是实现图像的边缘检测功能 目录 mif文件的制作 调用 ip 核生成rom以及在 questasim 仿真注意问题 灰度处理 均值滤波:重点是3*3 像素阵列的生成 sobel边缘检测 图 ...
- 聊聊、Nginx 安装启动
首先说下安装 Nginx 的步骤: (1)window 下安装 进入 http://nginx.org/en/download.html 下载版本 Mainline version 或者 Stable ...
- webapi同时支持post和get报404错误
文章:webapi设置一个Action同时支持get和post请求 这篇文章,有提供方法.参数前加上[FromUri] [AcceptVerbs("GET", "POST ...