POJ 1789 Truck History(Prim+邻接矩阵)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std; const int MAXN=2010;
const int INF=10e8;
int n,k,minn;
char str[MAXN][10];
int c[MAXN][MAXN],lc[MAXN];
bool vis[MAXN]; int get_distance(char s1[],char s2[])
{
int dis=0;
for(int i=0;i<7;i++)
if(s1[i]!=s2[i])
dis++;
return dis;
} int Prim()
{
int ans=0;
memset(vis,0,sizeof(vis));
vis[1]=1; for(int i=1;i<=n;i++)
lc[i]=c[1][i];
for(int i=1;i<=n-1;i++)
{
k=-1,minn=INF;
for(int j=1;j<=n;j++)
if(!vis[j]&&minn>lc[j])
{
minn=lc[j];k=j;
}
if(k==-1) break;
ans+=minn;vis[k]=1;
for(int j=1;j<=n;j++)
if(!vis[j]&&lc[j]>c[k][j])
lc[j]=c[k][j];
}
return ans;
} int main()
{
while(scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
{
scanf("%s",str[i]);
c[i][i]=0;
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
c[i][j]=c[j][i]=get_distance(str[i],str[j]);
printf("The highest possible quality is 1/%d.\n",Prim());
}
return 0;
}
POJ 1789 Truck History(Prim+邻接矩阵)的更多相关文章
- 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 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- 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 (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- 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 ...
随机推荐
- orm2
数据库连接 var orm = require("orm"); orm.connect("mysql://username:password@host/database& ...
- C#重写url
string url = Request.Url.LocalPath; Context.RewritePath(url + "?id=1111&name=gdwy"); 或 ...
- HDU 1272 小希的迷宫(乱搞||并查集)
小希的迷宫 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有 ...
- [转]Python跳过第一行读取文件内容
from itertools import islice file_name='XXXX' input_file = open(file_name) for line in islice(input_ ...
- zabbix 布署实践【3 proxy安装】
使用openstack在生产环境创建的一台虚拟机 环境 CentOS7 4核4G内存40G硬盘 IP:10.120.150.150 镜像默认关闭防火墙,selinux ,NetworkManage ...
- 使用 voluptuous 校验数据
在 Python 中,我们经常需要对参数进行校验,这是我们有好多种方法,例如写很多 if 啊,或者写正则表达式啊等等,技巧高的人可以写得很巧妙,而技巧一般的人呢,可能会写得很冗长,例如我,经常就不能很 ...
- Vimperator技巧
Vimperator技巧 什么是Vimperator?Firefox的一个插件,模拟vim操作. 1. 用]]打开"下一页"链接,[[打开"上一页"Vimper ...
- 3、href和src的区别
href:将现在的页面连接到新的页面 src:将需要引用的东西添加到现在的页面 <a href="mailto:youremailaddress@host.com">C ...
- Ecstore启用www.ecstore.com和m.ecstore.com域名
Ecstore启用www.ecstore.com和m.ecstore.com域名 修改config/mapper.php if($_SERVER['SERVER_NAME']=='www.ecstor ...
- 关于去除input type='file'改变组件的默认样式换成自己需要的样式的解决方案
在工作中时常会遇到如需要上传功能的按钮,而不像需要系统默认的样式时候,可以采取以下的解决方案: <img onclick="getElementById('file').click() ...