UVa 10397 - Connect the Campus
来源:互联网 发布:nba2kol王朝软件 编辑:程序博客网 时间:2024/06/05 03:15
因为已经有部分建筑用电线连起来了,要求还需要多少电线才可以将全部建筑连起来,只需要将用电线连起来的城市之间的距离设为0,再用Kruskal。
代码如下:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>using namespace std;const int DMAXN = 755;const int EMAXN = 281630;int n, p[DMAXN], u[EMAXN], v[EMAXN], r[EMAXN];double x[DMAXN], y[DMAXN], dis[EMAXN], ans;bool vis[DMAXN][DMAXN];int cmp(const int i, const int j){ return dis[i] < dis[j];}int find(int i){ return p[i] == i ? i : (p[i] = find(p[i]));}void Kruskal(){ ans = 0; sort(r, r+n, cmp); for(int i=0; i<n; i++) { int e = r[i]; int xx = find(u[e]); int yy = find(v[e]); if(xx != yy) { ans += dis[e]; p[xx] = yy; } }}int main(){#ifdef test freopen("sample.txt", "r", stdin);#endif int dot, cable, fx, fy; while(scanf("%d", &dot) != EOF) { n = 0; double d1, d2; memset(vis, false, sizeof(vis)); for(int i=0; i<dot; i++) { scanf("%lf%lf", &x[i], &y[i]); p[i] = i; } scanf("%d", &cable); for(int i=0; i<cable; i++) { scanf("%d%d", &fx, &fy); --fx; --fy; vis[fx][fy] = vis[fy][fx] = true; } for(int i=0; i<dot; i++) for(int j=i+1; j<dot; j++) { u[n] = i; v[n] = j; r[n] = n; if(vis[i][j]) dis[n++] = 0.0; else { d1 = x[i] - x[j]; d2 = y[i] - y[j]; dis[n++] = sqrt(d1*d1+d2*d2); } } Kruskal(); printf("%.2lf\n", ans); } return 0;}
- uva 10397 - Connect the Campus
- UVA 10397 - Connect the Campus
- UVa 10397 - Connect the Campus
- Uva 10397 - Connect the Campus
- UVa 10397 - Connect the Campus
- UVa 10397: Connect the Campus
- Uva - 10397 - Connect the Campus
- UVA - 10397 Connect the Campus
- UVa 10397 - Connect the Campus
- UVa 10397 Connect the Campus
- UVA 10397 - Connect the Campus
- UVa 10397 - Connect the Campus
- UVA - 10397 Connect the Campus kruskal算法
- UVA 10397 - Connect the Campus 翻译
- uva 10397 Connect the Campus kruskal || prim
- 10397 - Connect the Campus
- 10397 - Connect the Campus
- 10397 - Connect the Campus
- HTTP协议
- linux 3.0.8 alsa数据流程分析
- android NDK编译多个so文件
- 我装Windows 8 经历以及用后感
- android Style
- UVa 10397 - Connect the Campus
- Android 进程内存、CPU使用查看
- JSTL标签库
- 求三角形面积。
- 百度词条填充
- Android 属性总结
- 水了这些篇blog也木评论,求评论啊!
- SWITCH交换机与路由器的区别
- 面向对象的三个基本特征(讲解)