[zoj解题] 1203
#include <stdio.h>
#include <stdlib.h>
#include <math.h> #define MAXN 100
#define MAXM 9900 int N, M; struct Edge {
int u,v;
double weight;
} edge[MAXM]; int vertexs[MAXN];
int parents[MAXN];
double xs[MAXN];
double ys[MAXN]; int edge_cmp(const void* a, const void* b) {
return (((struct Edge*)a)->weight - ((struct Edge*)b)->weight > 0) ? 1:-1;
} int parents_find(int x) {
int s;
for(s = x; parents[s] >= 0; s = parents[s]){
;
}
return s;
} void parents_union(int r1, int r2) {
int s1, s2;
int tmp;
s1 = parents_find(r1);
s2 = parents_find(r2);
tmp = parents[s1] + parents[s2];
if(parents[s1] < parents[s2]) {
parents[s2] = s1;
parents[s1] = tmp;
}
else {
parents[s1] = s2;
parents[s2] = tmp;
}
} void kruskal(int count) {
double sum_weight = 0;
int num = 0;
int i, u, v;
for(i = 0; i < M; i++) {
u = edge[i].u;
v = edge[i].v;
if(parents_find(u) != parents_find(v)) {
num ++;
sum_weight += edge[i].weight;
parents_union(parents_find(u), parents_find(v));
}
if(num == N - 1) {
break;
}
}
printf("Case #%d:\n", count);
printf("The minimal distance is: %.2lf\n", sum_weight);
} int main() {
int i, j;
int index;
double weight;
int count = 1;
int flag = 1; while(1) {
scanf("%d", &N);
if(N == 0) {
break;
}
if(flag == 0) {
printf("\n");
}
flag = 0;
/* init */
for(i = 0; i < N; i++) {
vertexs[i] = i;
parents[i] = -1;
} for(i = 0; i < N; i++) {
scanf("%lf%lf", &xs[i], &ys[i]);
} index = 0;
for(i = 0; i < N; i++) {
for(j = i + 1; j < N; j++) {
weight = sqrt((xs[i] - xs[j])*(xs[i] - xs[j]) + \
(ys[i] - ys[j])*(ys[i] - ys[j]));
edge[index].u = i;
edge[index].v = j;
edge[index].weight = weight;
index ++;
}
}
M = index;
/* sort */
qsort(edge, M, sizeof(edge[0]), edge_cmp);
kruskal(count);
count ++;
} return 0;
}
有kruskal改的,格式很重要
[zoj解题] 1203的更多相关文章
- ZOJ Problem Set - 1025解题报告
ZOJ Problem Set - 1025 题目分类:基础题 原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=10 ...
- ZOJ 1093 Monkey and Banana (LIS)解题报告
ZOJ 1093 Monkey and Banana (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法
主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...
- 【九度OJ】题目1203:IP地址 解题报告
[九度OJ]题目1203:IP地址 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1203 题目描述: 输入一个ip地址串,判断是否合 ...
- 重拾ZOJ 一周解题
ZOJ 2734 Exchange Cards 题目大意: 给定一个值N,以及一堆卡片,每种卡片有一个值value和数量number.求使用任意张卡片组成N的方式. 例如N = 10 ,cards(1 ...
- ZOJ 1203 Swordfish
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- ZOJ 1203 Swordfish(Prim算法求解MST)
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- 【解题报告】牡丹江现场赛之ABDIK ZOJ 3819 3820 3822 3827 3829
那天在机房做的同步赛,比现场赛要慢了一小时开始,直播那边已经可以看到榜了,所以上来就知道A和I是水题,当时机房电脑出了点问题,就慢了好几分钟,12分钟才A掉第一题... A.Average Score ...
- ACM解题之(ZOJ 1094) Matrix Chain Multiplication
题目来源: 点击打开链接 题目翻译: 矩阵乘法问题是动态规划的典型例子. 假设你必须评估一个表达式,如A * B * C * D * E,其中A,B,C,D和E是矩阵.由于矩阵乘法是关联的,乘法运算的 ...
随机推荐
- Mycat 依赖包解读
1.curator - zookeeper开源客户端框架 2.dom4j - xml解析包 3.druid - 阿里巴巴推出的国产数据库连接池,同时具备监控功能,性能优于JDBC和C3P0 4.ehc ...
- UITableView的子控件高度不确定处理
比如,tableView的tableFootView的控件数量是根据网络请求的数据而定的.那么tableView并不能准确的设置其contentSize.处理方法: 在tableFootView的类中 ...
- python注意事项
以下基于python3.4.3 1.python3与python2不兼容 2.python语言正确的缩进很重要!事实上缩进是种语法 C中需要 { } 的的地方,python使用 : +缩进 实现 3. ...
- C语言根据函数名调用对应的函数
通过函数指针定义,调用时加上参数 struct Command { const char *name; const char *desc; // return -1 to force monitor ...
- Arch: Configurations
the original purpose is to show the steps needed to setup i3 in vbox.. easy. alright, it is a bit mi ...
- hdu 1180诡异的楼梯(bfs)
诡异的楼梯 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submis ...
- java 线程的中断
Example12_6.java public class Example12_6 { public static void main(String args[]) { ClassRoom room6 ...
- CenOS下LAMP搭建过程
CentOS虚拟机中安装LAMP: Linux+Apache+MySQL+PHP 安装前先关闭防火墙和Selinux 把所有安装包解压到/lamp下(根目录下的lamp目录) 安装gcc, gcc-c ...
- CodeForces 83B Doctor
二分查找. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT (Advanced Level) 1103. Integer Factorization (30)
暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...