最小生成树,主要是题目比较难懂。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int Maxn=+;
const int maxn=*+;
int Father[Maxn];
struct Edge
{
int from,to,w;
}edge[maxn];
int n,tot;
char s[maxn][]; int Find(int x)
{
if(x!=Father[x]) Father[x]=Find(Father[x]);
return Father[x];
} void init()
{
for(int i=;i<=n;i++) Father[i]=i;
tot=;
} bool cmp(const Edge&a,const Edge&b)
{
return a.w<b.w;
} int main()
{
freopen("in.txt","r",stdin);
while(~scanf("%d",&n)){
if(!n) break;
init();
for(int i=;i<n;i++)
scanf("%s",s[i]); for(int i=;i<n;i++)
for(int j=i+;j<n;j++)
{
edge[tot].from=i;
edge[tot].to=j;
edge[tot].w=;
for(int k=;k<;k++)
if(s[i][k]!=s[j][k])
edge[tot].w++;
tot++;
} sort(edge,edge+tot,cmp); int W=;
for(int i=;i<tot;i++)
{
int Fu=Find(edge[i].from);
int Fv=Find(edge[i].to);
if(Fu!=Fv)
{
Father[Fu]=Fv;
W=W+edge[i].w;
}
}
printf("The highest possible quality is 1/%d.\n",W);}
return ;
}

ZOJ 2158 POJ 1789 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 (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

  6. poj 1789 Truck History 最小生成树

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

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

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

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

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

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

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

随机推荐

  1. .net后台代码临时表创建

    写法一: var dt = new DataTable(); dt.Columns.Add(new DataColumn("Id", System.Type.GetType(&qu ...

  2. install webapp2 on Linux outside google app engine.

    Reference: https://webapp-improved.appspot.com/tutorials/quickstart.nogae.html Step 1: install pip S ...

  3. JAVA的if用法,比如if(...){} 和if()没有大括号直接写下面的区别是什么

    有大括号的时候 大括号里面所有的 都归if管.只有条件为真的时候 才会执行.没有大括号的时候 只有下面的一句归if管,也就是说 当只有一句的时候 大括号可以省略 其它的 没区别.

  4. 灾情巡视C语言代码

    /*"水灾巡视问题"模拟退火算法.这是一个推销员问题,本题有53个点,所有可能性大约为exp(53),目前没有好方法求出精确解,既然求不出精确解,我们使用模拟退火法求出一个较优解, ...

  5. Oralce生成前N年的年数据

    今天做一个统计报表的时候正好碰到这个问题,原来,一般是通过后台代码来生成.现在直接通过oracle来生成,记录一下. 方法一: SELECT YEAR FROM ( , UNION SELECT TO ...

  6. win10 Incredibuild 兼容

    电脑换了win10,所有的软件都需要重新安装. win10的体验还是很不错的.但是也有一堆的问题.原来的vs2010今天无法再使用分布式编译IncrediBuild. 现象是,虽然可以连上分布式编译的 ...

  7. Linux Date命令学习笔记

    date 根据给定格式显示日期或设置系统日期时间.print or set the system date and time 指令所在路径:/bin/date 命令参数: 参数 描述 -d 显示字符串 ...

  8. form表单直接传文件

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  9. ImageView.ScaleType设置图解

    图文相配很清晰的看出每个属性的效果, 感觉 CENTER_CROP 比较有用,长宽自动适应 ImageView ,整个图片自动缩略填充整个区域且居中显示(高宽不一定是view的尺寸),以前用JS在网页 ...

  10. ios根据文本自适应 然后 搭建类似如下效果

    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tbSecond.size.width, 0)]; hea ...