本题链接:点击打开链接

题目大意:

输入n表示卡车辆数,输入每辆卡车编号。即长度为7的字符串,每辆卡车编号均可由其他类型编号衍生过来,求由当中一辆衍生出其他全部的最小衍生次数(有一个字符不同就需衍生一次)。

解题思路:

本题能够看做求最小生成树的问题,关键是图的构建。把每两辆车衍生次数作为权值,然后使用prime算法求解最小生成树,详细请參见代码:

#include<stdio.h>
#include<string.h>
#define INF 0xffffff
int map[2020][2020];
int mark[2020],lowcost[2020];
char str[2020][10];
void prime(int n)
{
int sum=0;
int vir=1;
memset(mark,0,sizeof(mark));
for(int i=1;i<=n;i++)
if(i!=vir)
lowcost[i]=map[vir][i];
lowcost[vir]=0;
mark[vir]=1;
for(int i=2;i<=n;i++)
{
int min=INF;
for(int j=1;j<=n;j++)
{
if(!mark[j]&&lowcost[j]<min)
{
min=lowcost[j];
vir=j;
}
}
mark[vir]=1;
sum+=min;
for(int k=1;k<=n;k++)
{
if(!mark[k]&&lowcost[k]>map[vir][k])
{
lowcost[k]=map[vir][k];
}
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
memset(map,0,sizeof(map));
for(int i=1;i<=n;i++)
scanf("%s",str[i]);
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
for(int k=0;k<7;k++)
{
if(str[i][k]!=str[j][k])
{
map[i][j]++;
map[j][i]++;
}
}
}
}
prime(n);
}
return 0;
}

poj 1879 Truck History的更多相关文章

  1. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  2. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

  3. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  4. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  5. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

  6. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  7. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

  8. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

  9. POJ 1789 Truck History (Kruskal 最小生成树)

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

随机推荐

  1. svn提交代码忘写注释怎么办,我想补充上去?

    propset --revprop -r 24288--force "svn:log" "一级采购人在填写申报书,汇总批量删除二级采购人申报书时报错" http ...

  2. 编译Opencv的GPU,利用CUDA加速

    首先检查自己的机器是否支持,否则都是白搭(仅仅有NVIDIA的显卡才支持.可在设备管理器中查看) 假设不用GPU.能够直接官网下载预编译好的库 环境: 1 VS2013 2 Opencv2.4.9 3 ...

  3. [转]关于适配iphone5,Invalid Launch Image的退信

    关于适配iphone5,Invalid Launch Image的退信 本人xcode 4.3,所开发客户端新版本准备提交,应用中做了关于iphone5的适配,然后打包提交.在提交审核的时候被拒,收到 ...

  4. no scheme 问题

    用xcode4打开xcode3建立的工程,有时候,不能自动转换版本,就会显示no scheme. 这个是由于XXX..xcodeproj包中xcuserdata文件夹中user.xcuserdatad ...

  5. SpreadSheet数据导出为DataTable z

    devexpress也提供了一种将excel数据,而且可以指定区域进行转换,用起来相当灵活,本人将其解决方法分享如下(代码):          private DataTable export(Wo ...

  6. UILabel的缩放动画效果

    UILabel的缩放动画效果 效果图 源码 https://github.com/YouXianMing/Animations // // ScaleLabel.h // Animations // ...

  7. [翻译] The Amazing Audio Engine

    The Amazing Audio Engine https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine The Amazing ...

  8. 修改Eclipse/MyEclipse项目的默认编码

    应该是中文操作系统的原因,eclipse默认的新项目的编码是GBK,出于对编码支持的考虑,项目组中最好统一要求是UTF-8编码进行开发. 修改eclipse的配置,可以使得eclipse的新建项目的默 ...

  9. LDAP Error Codes

    Error / Data Code Error Description 0 LDAP_SUCCESS Indicates the requested client operation complete ...

  10. 触发器系列(2) DataTrigger

    触发条件: DataTrigger是以控件DataContext的属性作为触发条件 本例效果说明: <!--当值为1时将外边框的背景色改为黑色--> <!--当值为2时 改变透明度达 ...