(最小生成树)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: ...
随机推荐
- @Transactionl注解
spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...&q ...
- fio 测试磁盘
root@rook-test:/# ceph osd status+----+-----------------------------+-------+-------+--------+------ ...
- easyUIDataGrid分页
package com.cn.eport.util; import java.util.List; /** * * * @author zh * */ public class DataGrid im ...
- ubuntu16.04安装virtualbox
download:download.virtualbox.org/virtualbox/5.0.10/virtualbox-5.0_5.0.10-104061~Ubuntu~trusty_amd64. ...
- android mapView
1.Because the Maps library is not a part of the standard Android library, you must declare it in the ...
- 使用ecstore-sdk开发包制作易开店和启明星模板
前言: 尽管商派官网有模板开发教程,但是诸多方面太过笼统.我等平庸之辈,纵使细心研读,潜心修炼,亦未能品味练功境界,领悟其中真谛. 商派有云,此九阳真经不用您挥刀****本人却感觉此教程令人抓狂,无人 ...
- Spring框架的AOP的底层实现之JDK的动态代理(代码了解,理解原理)
1.创建接口UserDao: package com.huida.demo1; public interface UserDao { public void save(); public void u ...
- WCF的例子
Demo的 “Service端”以本机IIS为宿主,“Client端”以WebForm项目为例. 1.新建项目:WCF>WCF Service Application: 2.删除默认文件ISer ...
- C#控制台进度条(Programming Progress bar in C# Consle application)
以下代码从Stack Overflow,觉得以后会用到就收藏一下,我是辛勤的搬运工,咿呀咿呀哟- 1.showing percentage in .net console application(在. ...
- Django之virtualenv下安装xadmin
1.安装xadmin,通过pip 进入virtualenv pip安装xadmin pyyuc:~ yuchao$ source PycharmProjects/mxvenv/bin/activate ...