【POJ 1789】Truck History(最小生成树)
题意:距离定义为两个字符串的不同字符的位置个数。然后求出最小生成树。
- #include <algorithm>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int N=;
- const int M=;
- char code[N][];
- int f[N];//并查集
- struct edge{
- int u,v,w;
- }e[M];
- int n,tot;
- void add(int u,int v,int w){
- e[tot].u=u;e[tot].v=v;e[tot++].w=w;
- }
- bool cmp(edge a,edge b){
- return a.w<b.w;
- }
- int find(int x){
- if(f[x]==-)return x;
- return f[x]=find(f[x]);
- }
- int Kruskal(){
- memset(f,-,sizeof f);
- sort(e,e+tot,cmp);
- int cnt=,ans=;
- for(int i=;i<tot;i++){
- int u=e[i].u,v=e[i].v,w=e[i].w;
- int fu=find(u),fv=find(v);
- if(fu!=fv){
- ans+=w;
- f[fu]=fv;
- cnt++;
- }
- if(cnt==n-)break;
- }
- return ans;
- }
- void solve(){
- for(int i=;i<=n;i++)
- for(int j=i+;j<=n;j++)
- {
- int dis=;
- for(int k=;k<;k++)
- if(code[i][k]!=code[j][k])dis++;
- add(i,j,dis);
- }
- }
- int main(){
- while(scanf("%d ",&n),n){
- tot=;
- for(int i = ; i <= n; i++)
- gets(code[i]);
- solve();
- printf("The highest possible quality is 1/%d.\n", Kruskal());
- }
- return ;
- }
【POJ 1789】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 ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- poj 1789 Truck History
题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...
- 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 (最小生成树)
Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...
- poj 1789 Truck History【最小生成树prime】
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21518 Accepted: 8367 De ...
- POJ 1789 Truck History (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- POJ 1789 Truck History (Kruskal 最小生成树)
题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...
随机推荐
- js点击左右滚动+默认自动滚动类
js点击左右滚动+默认自动滚动类 点击下载
- Centos 检查磁盘读写性能
启动Tomcat发现deploy war的速度明显变慢, 怀疑磁盘出问题 测试写入 [tomcat@localhost ~]$ dd if=/dev/zero of=kwxgd bs=64k coun ...
- Location of several networks in brain
Source: Naci, L., et al. (2014). "A common neural code for similar conscious experiences in dif ...
- css 九宫格
http://jsfiddle.net/webtiki/kQXkt/ http://jsfiddle.net/webtiki/MpXYr/3/embedded/ https://www.web-tin ...
- 【转】如何利用logrotate工具自动切分滚动中的日志文件
FROM : http://www.2cto.com/os/201503/381812.html 在很多实际项目中,应用程序会持续写日志,如果程序代码中没有调用支持自动切分(如按filesize或da ...
- distributed caching for .net applications
distributed caching for .net applications fast, scalable distributed caching with meaningful perform ...
- Cordova - 与iOS原生代码交互1(通过JS调用Swift方法)
在前面的文章中介绍的了如何使用Cordova进行跨平台应用的开发,使用Cordova的话基本上就不需要在写系统原生代码了,只要通过编写html页面和js方法即可. 但在有些特殊情况下,还是是需要htm ...
- noi题库(noi.openjudge.cn) 1.8编程基础之多维数组T21——T25
T21 二维数组右上左下遍历 描述 给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按从左上到右下的对角线顺序遍历整个数组. 输入 输入的第一行上有两个整数,依次为 ...
- hadoop 2.6全分布安装
环境:centos 6.6 + hadoop2.6 虚拟机:(vmware fusion 7.0.0) 虚拟机hostname / IP地址 master / 192.168.187. ...
- oracle: job使用
oracle的job,实际上就是数据库内置的定时任务,类似代码中的Timer功能.下面是使用过程: 这里我们模拟一个场景:定时调用存储过程P_TEST_JOB 向表TEST_JOB_LOG中插入数据 ...