1626: [Usaco2007 Dec]Building Roads 修建道路 (生成树)
来源:互联网 发布:mysql innodb表修复 编辑:程序博客网 时间:2024/05/21 03:58
此题求距离时直接平方会爆int。
#include<algorithm>#include<iostream>#include<cstdio>#include<cmath>using namespace std;inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x*f;}struct edge { int x, y; double v;} e[1000001];int n, m, cnt, tot, x[1001], y[1001], fa[1001];bool mp[1001][1001];double ans;inline bool cmp(edge a, edge b) { return a.v < b.v;}inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]);}inline long long sqr(int x) { return (long long) x*x;}inline double dis(int a, int b) { return sqrt(sqr(x[a] - x[b]) + sqr(y[a] - y[b]));}int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) { fa[i] = i; x[i] = read(); y[i] = read(); } for (int i = 1; i <= m; i++) { int a = find(read()), b = find(read()); fa[a] = b; } for (int i = 1; i < n; i++) for (int j = i + 1; j <= n; j++) { e[++cnt] = (edge){i, j, dis(i, j)}; } sort(e + 1, e + cnt + 1, cmp); for (int i = 1; i <= cnt; i++) { int p = find(e[i].x), q = find(e[i].y); if (p != q) { fa[p] = q; ans += e[i].v; tot++; if (tot == n - 1)break; } } printf("%.2lf", ans); return 0;}
0 0
- 1626: [Usaco2007 Dec]Building Roads 修建道路 (生成树)
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路(最小生成树)
- 最小生成树-BZOJ-1626-[Usaco2007 Dec]Building Roads 修建道路
- bzoj1626[Usaco2007 Dec]Building Roads 修建道路
- [BZOJ1626] [Usaco2007 Dec]Building Roads 修建道路
- bzoj1626【Usaco2007 Dec】Building Roads 修建道路
- [Usaco2007 Dec]Building Roads 修建道路
- [Usaco2007 Dec]Building Roads 修建道路
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路
- bzoj 1626[Usaco2007 Dec]Building Roads 修建道路
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路 kruskal
- bzoj1626 [Usaco2007 Dec]Building Roads 修建道路(kruskal裸题)
- [bzoj1626][Usaco2007 Dec][最小生成树]修建道路
- Cpp环境【BZOJ1626】【Usaco2007 Dec】【Vijos 1693】 修建道路
- Building Roads(prim 最小生成树)
- USACO 建造道路 Building Roads
- BZOJ 1626 & luogu 2872 [USACO07DEC]道路建设Building Roads Kruskal
- C# WebService 参数属性
- DevOps=Dev+Ops
- 通向架构师的道路(第七天)之漫谈使用ThreadLocal改进你的层次的划分
- JDK8 部分代码测试
- 项目经验:Query 日期参数的设置方法setTimestamp
- 1626: [Usaco2007 Dec]Building Roads 修建道路 (生成树)
- 用Selenium WebDriver+Grid2实现浏览器端性能测试
- excel使用cube
- TCP 的那些事儿(上)
- 项目经验:Query in 参数设置setParameterList例子
- js this关键字
- 继承和组合
- 陇南妇幼无痛人流多少钱
- 在button中添加两行文字