题意:

就是裸的最小生成树(MST), 完全图, 边长是实数。

分析:

算是复习一下MST把

方法一: prim 复杂度(n^2)

 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + ;
const int inf = 0x3f3f3f3f;
double G[maxn][maxn], dis[maxn];
bool vis[maxn];
int x[maxn], y[maxn];
int N;
double p2pdis(int x1, int y1, int x2, int y2){
double dis = sqrt((x1-x2) * (x1 - x2) + (y1-y2) * (y1-y2));
return dis;
}
int main(){
#if LOCAL
freopen("1.txt","r",stdin);
#endif // LOCAL while(scanf("%d", &N) && N){
memset(G,,sizeof(G));
memset(vis,,sizeof(vis));
for(int i = ; i < N; i++){
scanf("%d %d", &x[i], &y[i]);
} for(int i = ; i < N; i++){
for(int j = ; j < N; j++){
if(j != i){
G[i][j] = p2pdis(x[i],y[i],x[j],y[j]);
}
}
} double ans = ; vis[] = ;
for(int i = ; i < N; i++){
dis[i] = G[][i]; //一开始起点加入树中, 所以可以将dis初始化为起点的出边
} for(int i = ; i < N - ;i++){//除去起点, 还剩n-1个点不在生成树中
double mind = inf;
int j,sel;
for(j = ; j < N; j++){
if(!vis[j] && dis[j] < mind){
mind = dis[j];
sel = j;
}
}
vis[sel] = ;
ans += dis[sel];
for(int k = ; k < N; k++){
if(!vis[k] && dis[k] > G[sel][k]){
dis[k] = G[sel][k]; //注意这里 prim是更新dis到选中点出边距离, dij是更新dis[选中点] + 选中点出边距离
}
}
}
printf("%.2f\n", ans);
}
}

方法二: kruskal 复杂度(MlogM)

 #include <bits/stdc++.h>
using namespace std;
int N;
const int maxn = 1e3 + ;
const int inf = 0x3f3f3f3f; struct edge{
int u;
int v;
double d;
friend bool operator< (edge n1,edge n2){
return n1.d<n2.d;
}
}
;
edge m[maxn*maxn]; double dis[maxn];
bool vis[maxn];
int x[maxn], y[maxn], f[maxn]; int getf(int v){
if(f[v] == v)
return v;
else{
f[v] = getf(f[v]);
return f[v];
}
}
int mer(int v, int u){
int t1, t2;
t1 = getf(v);
t2 = getf(u);
if(t1 != t2){
f[t2] = t1;
return ;
}
return ;
} double p2pdis(int x1, int y1, int x2, int y2){
double dis = sqrt((x1-x2) * (x1 - x2) + (y1-y2) * (y1-y2));
return dis;
}
int main(){ while(scanf("%d", &N) && N){
memset(vis,,sizeof(vis));
for(int i = ; i < N; i++){
scanf("%d %d", &x[i], &y[i]);
} int mcnt = ;
for(int i = ; i < N; i++){
for(int j = ; j < N; j++){
if(j != i){
m[mcnt].u = i;
m[mcnt].v = j;
m[mcnt++].d = p2pdis(x[i],y[i],x[j],y[j]);
}
}
}
sort(m,m+mcnt); //从小到大排序边 for(int i = ; i < N; i ++){//并查集初始化
f[i] = i;
}
double ans = ;
int cnt = ;
for(int i = ; i < mcnt; i++){//从小到大枚举
if(mer(m[i].u, m[i].v)){
// printf("u: %d v: %d\n", m[i].u, m[i]. v);
cnt ++;
ans += m[i].d;
}
if( cnt == N - )//用了n-1条边后退出
break;
}
printf("%.2f\n", ans);
}
return ;
}

UvaLive 4872 Underground Cables (最小生成树)的更多相关文章

  1. UVALive 4872 Underground Cables 最小生成树

    题目链接: 题目 Underground Cables Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %ll ...

  2. POJ 2075 Tangled in Cables 最小生成树

    简单的最小生成树,不过中间却弄了很久,究其原因,主要是第一次做生成树,很多细节不够熟练,find()函数的循环for判断条件是 pre[i]>=0,也就是遇到pre[i]==-1时停止,i就是并 ...

  3. 图论常用算法之一 POJ图论题集【转载】

    POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:h ...

  4. poj2075

    Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6348   Accepted: 2505 ...

  5. UVALive - 2515 (最小生成树 kruskal)

    You are assigned to design network connections between certain points in a wide area. You are given ...

  6. 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

    layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...

  7. ZOJ2326Tangled in Cables(最小生成树)

    Tangled in Cables Time Limit: 2 Seconds      Memory Limit: 65536 KB You are the owner of SmallCableC ...

  8. 最小生成树求最大比率 UVALive - 5713

    题目链接:https://vjudge.net/problem/UVALive-5713 题意:给出t组数据,每组数据第一行给出一个n,表示点的数量,接下来n行,每行有三个数字,分别是点的坐标x,y和 ...

  9. 最小生成树 prime算法 UVALive - 6437

    题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...

随机推荐

  1. 递推DP UVA 590 Always on the run

    题目传送门 题意:题意难懂,就是一个小偷在m天内从城市1飞到城市n最小花费,输入的是每个城市飞到其他城市的航班. 分析:dp[i][j] 表示小偷第i天在城市j的最小花费.状态转移方程:dp[i][j ...

  2. matlab实现算术编解码 分类: 图像处理 2014-06-01 23:01 357人阅读 评论(0) 收藏

    利用Matlab实现算术编解码过程,程序如下: clc,clear all; symbol=['abc']; pr=[0.4 0.4 0.2]; %各字符出现的概率 temp=[0.0 0.4 0.8 ...

  3. Android 线程池系列教程(3) 创建线程池

    Creating a Manager for Multiple Threads 上一课  下一课 1.This lesson teaches you to Define the Thread Pool ...

  4. CentOS 7 下用 firewall-cmd / iptables 实现 NAT 转发供内网服务器联网

    自从用 HAProxy 对服务器做了负载均衡以后,感觉后端服务器真的没必要再配置并占用公网IP资源. 而且由于托管服务器的公网 IP 资源是固定的,想上 Keepalived 的话,需要挤出来 3 个 ...

  5. R Programming week1-Data Type

    Objects R has five basic or “atomic” classes of objects: character numeric (real numbers) integer co ...

  6. (转) 淘淘商城系列——CMS内容管理系统工程搭建

    http://blog.csdn.net/yerenyuan_pku/article/details/72825801 淘淘商城系列——CMS内容管理系统工程搭建 上文我们一起搭建了表现层中的商城门户 ...

  7. codeforces_1075_C. The Tower is Going Home

    http://codeforces.com/contest/1075/problem/C 题意:一个长宽均为1e9的棋盘,n个垂直障碍在x列无限长,m个水平障碍在第y行从第x1列到x2列.可以水平和垂 ...

  8. bootstrap 整理

    form-control   输入框,独占一行,占满. form-group  增加下边距 <label> 字体会加粗,不是独占一行 .col-xs-  超小屏 .col-sm- 平板 . ...

  9. hibernate 离线查询(DetachedCriteria)

    离线查询使用DetachedCriteria对象设置限制条件,然后再通过session获取Criteria对象. 使用场景: 例如Biz类和Dao类,在Dao类中利用session操作CRUD,如果你 ...

  10. 使用VS Code调试Flutter(检查用户页面)

    官方提供的是Flutter Widget Inspector,详见https://flutterchina.club/inspector/ 我用的是另外一种好用的调试工具 Dart DevTools ...