博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
acm algorithm practice Dec. 27 MST
阅读量:7112 次
发布时间:2019-06-28

本文共 2413 字,大约阅读时间需要 8 分钟。

what makes me amused joyful is I use emacs coding this algorithm..:)

although i am not proficient in it at all.

besides, today I installed aqumacs in place of the built-in emacs of Mac.   thereby(thus)  reviewed  the bash command and the profile .bash_profile

------------------------------------------------

poj 1258   Agri-Net

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

------------------------------------------------------

it's a basic MST problem using Kruskac's algorithm.   (since Prime algorithm is kinda like djstrak algorithm which is practiced yesterday :)).   

both the data structure and the algorithm are quite simple.    

the key of K algorithm is Union-find  &  union

the code is:   

代码
 
edges.sort(cmp);
for
(list
<
edge
>
::iterator it
=
edges.begin(); it
!=
edges.end(); it
++
){
//
if find set
int
x
=
(
*
it).from;
int
y
=
(
*
it).to;
while
(vertices[x].parent
!=
x){
x
=
vertices[x].parent;
//
climb up
}
while
(vertices[y].parent
!=
y){
y
=
vertices[y].parent;
//
climb up
}
if
(x
==
y){
//
reject
continue
;
}
else
{
//
put the edge into A
A
+=
(
*
it).weight;
//
flip, now the x, y is the ROOT !!!!!
if
(vertices[x].size
<=
vertices[y].size){
vertices[x].parent
=
y;
//
x's parent is set to y
vertices[y].size
=
vertices[x].size
+
vertices[y].size;
}
else
{
vertices[y].parent
=
x;
//
y's parent is set to x
vertices[x].size
=
vertices[x].size
+
vertices[y].size;
}
}
//
else do nothing
}

 

the caveat doesn't belong to the algorithm issue... at first,  I can not pass poi until I change the input 

 
while
(cin
>>
N) {}

 

.  the instruction itself doesn't mention this input format :( and thank God I didn't change my original implementation of algorithm, or it would be quite a waste of time ....

---------------------------------------------

 

 

 

 

转载于:https://www.cnblogs.com/ggppwx/archive/2010/12/28/1918948.html

你可能感兴趣的文章
打破医院围墙 数字化平台之上的想象力
查看>>
Teradata首席分析官Bill Franks:数据分析变革犹如一场工业革命
查看>>
Linux下安装并使用Java开发opencv的配置
查看>>
AdTime: DMC量身定制的企业数据分析师
查看>>
《数字逻辑设计与计算机组成》一2.3 规范表达式
查看>>
借道大数据 互联网基金再探“蓝海”
查看>>
浙江医疗综合体获批 医疗资源也可共享
查看>>
3G/4G调制解调器曝漏洞:可致设备被完全控制
查看>>
你知道你的Mac摄像头正在偷窥你吗?这款工具或许能帮你
查看>>
超干货!一套完整的设计分析思路应该是怎样的?
查看>>
关于视频流的各种问题,后续整理
查看>>
从零开始,我的上云路
查看>>
【Spark Summit East 2017】R与Spark:如何使用RStudio的 Sparklyr和H2O的 Rsparkling分析数据...
查看>>
FIS源码-fis release概览
查看>>
鹰眼跟踪、EDAS燎原, 看高性能服务框架EDAS的架构实践
查看>>
使用LogHub进行日志实时采集
查看>>
使用jackson-mapper-lgpl序列化和反序列化
查看>>
Windows环境下在Oracle VM VirtualBOX下克隆虚拟机镜像(克隆和导入)
查看>>
iOS开发之使程序在后台运行
查看>>
MySQL修改密码和加密
查看>>