ZOJ 2158 POJ 1789 Truck History
最小生成树,主要是题目比较难懂。
- #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的更多相关文章
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- POJ 1789 -- Truck History(Prim)
POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...
- 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 (Kruskal)
题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...
- poj 1789 Truck History 最小生成树
点击打开链接 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15235 Accepted: ...
- 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 最小生成树 prim 难度:0
Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19122 Accepted: 7366 De ...
随机推荐
- layer1.8UI
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- json 多重嵌套反序列化和序列化
namespace ConsoleApplication1 { class Program { static void Main(string[] args) ...
- Comparer<T> IComparer<T> IComparable<T>
Comparer<T>.Default Property Comparer<T>.Default doesn't use your FooComparer class. It ...
- ie6的png24问题
解决IE6的PNG透明JS插件 DD_belatedPNG 引:http://www.cnblogs.com/cobby/archive/2012/05/11/2495801.html IE6的PNG ...
- sql优化方式-转载
我始终认为,一个系统的性能的提高,不单单是试运行或者维护阶段的性能调优的任务,也不单单是开发阶段的事情,而是在整个软件生命周期都需要注意,进行有效工作才能达到的.所以我希望按照软件生命周期的不同阶段来 ...
- Debian搭建PPTPD
先安装pptpd: apt-get install pptpd 编辑 /etc/pptpd.conf #取消下面两行的注释,在文件的底部. # localip 192.168.0.1 # remote ...
- Ubuntu系统上安装搜狗拼音输入法sogou
1. 首先在百度搜索搜狗拼音输入法Linux就能看到它的官网:http://pinyin.sogou.com/linux/ 下载后直接安装.安装成功后在控制台输入 im-config 如果选中的是fc ...
- struts2防止重复提交的标签
struts2 token 使用说明 --------------------------------------------------------------------------------- ...
- 3DTouch--2
苹果太贱了! 3D Touch 只能在真机上面试,模拟器没办法玩! ------------- 描述有点粗燥.....有6S 在手上玩得童鞋会更加清楚,只有玩过才更加体验到. 首先 有几个要知道的手势 ...
- jQuery 截取double数据 重新赋值
$('.prioritySort').each(function(i){ $(this).text($(this).text().substring(0,$(this).text().indexOf( ...