UVA 10397 - Connect the Campus
来源:互联网 发布:洗车哪个软件好 编辑:程序博客网 时间:2024/06/05 09:24
最小生成树问题,注意消去固定边时的方法:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#define MAXN 800#define MAXN1 300000double x[MAXN], y[MAXN], w[MAXN1];int n, p1, m, p[MAXN], u[MAXN1], v[MAXN1], r[MAXN1], vis[1010][1010];int cmp(const void *_i, const void *_j){ int *i = (int *)_i; int *j = (int *)_j; return w[*i] > w[*j] ? 1 : -1;}int find(int x) {return p[x] == x ? x : p[x] = find(p[x]);}double kruskal(){ double ans = 0; for(int i = 0; i <= n; i ++) p[i] = i; for(int i = 0; i < p1; i ++) r[i] = i; qsort(r,p1,sizeof(r[0]),cmp); for(int i = 0; i < p1; i ++) { int e = r[i]; int x1 = find(u[e]); int y1 = find(v[e]); if(x1 != y1){ans += w[e]; p[x1] = y1;} } return ans;}void init(){ while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i ++) scanf("%lf%lf",&x[i], &y[i]); scanf("%d",&m); for(int i = 0; i < m; i ++) { int z, c; scanf("%d%d",&z, &c); if(z > c) vis[c][z] = 1; else vis[z][c] = 1; } p1 = 0; for(int i = 1; i <= n; i ++) { for(int j = i+1; j <= n; j ++) { if(vis[i][j]) w[p1] = 0; else w[p1] = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j])); u[p1] = i; v[p1] = j; p1 ++; } } printf("%.2f\n",kruskal()); }}int main(){ init(); return 0;}
0 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
- Linux监测日志调试利器tail命令详细使用
- g729源码分析-1-lpc分析
- UVA 567 - Risk
- UVA 10034 - Freckles
- UVA 10048 - Audiophobia
- UVA 10397 - Connect the Campus
- UVA 10369 - Arctic Network
- 每日一题 隐式图 倒水问题
- 利用Lucene打造站内搜索引擎的思路
- Files Operation in Python
- UVA 10154 - Weights and Measures
- UVA 11151 - Longest Palindrome
- Android实现音频录制的两种方式
- UVA 10635 - Prince and Princess