Building Roads(Prim+Kruskal)
来源:互联网 发布:phpstudy mac 编辑:程序博客网 时间:2024/06/08 10:07
Building Roads
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11767 Accepted: 3351
Description
Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.
Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi, Yi) on the plane (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000). Given the preexisting M roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.
Input
- Line 1: Two space-separated integers: N and M
- Lines 2..N+1: Two space-separated integers: Xi and Yi
- Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farm i and farm j.
Output
- Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.
Sample Input
4 1
1 1
3 1
2 3
4 3
1 4
Sample Output
4.00
Source
USACO 2007 December Silver
n个村庄,m条已经修好的道路,接下来n行表示n个村庄的坐标(xi,yi),m行表示两个村庄(a,b)已经修好道路。求n个村庄互相连通需要修建的最短道路。
Prim:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#define INF 0x3f3f3f3fdouble ma[1211][1211];double dist[1211];int v[1211];int n, m;int x[1211], y[1211];double dis(int i, int j){ double xx = x[i] - x[j]; double yy = y[i] - y[j]; return sqrt(xx*xx+yy*yy);}void Prim(){ memset(v, 0, sizeof(v)); for(int i=0;i<n;i++) dist[i] = ma[0][i]; v[0] = 1; int point; double ans = 0, min; for(int i=1;i<n;i++) { point = i; min = INF; for(int j=0;j<n;j++) { if(dist[j]<min&&v[j]==0) { point = j; min = dist[j]; } } ans += min; v[point] = 1; for(int j=0;j<n;j++) { if(dist[j]>ma[point][j]&&v[j]==0) dist[j] = ma[point][j]; } } printf("%.2f\n", ans);}int main(){ scanf("%d %d", &n, &m); for(int i=0;i<n;i++) { scanf("%d %d", &x[i], &y[i]); } for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { ma[i][j] = ma[j][i] = dis(i, j); } } for(int i=0;i<m;i++) { int a, b; scanf("%d %d", &a, &b); ma[a-1][b-1] = ma[b-1][a-1] = 0;//已经修好的清零 } Prim(); return 0;}
Kruskal:
#include <iostream>#include <algorithm>#include <cstring>#include <cmath>#include <cstdio>#define INF 0x3f3f3f3fusing namespace std;double x[121001];double y[121001];struct node{ int x, y; double data;}que[1000000];int n, m, top, cont;int pre[121001];bool cmp(struct node u, struct node b){ return u.data<b.data;}double dis(int i, int j){ double xx = x[i] - x[j]; double yy = y[i] - y[j]; return sqrt(xx*xx+yy*yy);}int root(int a){ while(a!=pre[a]) a = pre[a]; return a;}void Kruskal(){ double ans = 0; for(int i=0;i<=top&&cont!=n-1;i++) { int xx = root(que[i].x); int yy = root(que[i].y); if(xx!=yy) { pre[xx] = yy; ans += que[i].data; cont++;//记录边数 } } printf("%.2f\n", ans);}int main(){ cin>>n>>m; for(int i=1;i<=n;i++) cin>>x[i]>>y[i]; top = -1, cont = 0; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { top++; que[top].x = i; que[top].y = j; que[top].data = dis(i, j); } } for(int i=1;i<=n;i++) pre[i] = i; for(int i=0;i<m;i++) { int a, b; scanf("%d %d", &a, &b); int xx = root(a); int yy = root(b); if(xx!=yy) { pre[xx] = yy; cont++; } } sort(que, que+top+1, cmp); Kruskal(); return 0;}
- Building Roads(Prim+Kruskal)
- poj-3625 Building Roads kruskal
- poj 3625 Building Roads (prim)
- zoj 1406 Jungle Roads(Prim || Kruskal)
- hdu 1102 Constructing Roads (prim 、kruskal)
- HDU-#1102 Constructing Roads(Prim & Kruskal)
- poj 1251 Jungle Roads【prim & kruskal】
- poj 2421 Constructing Roads【kruskal & prim】
- HDU 1102:Constructing Roads【Kruskal & Prim】
- hdu 1102 Constructing Roads(kruskal || prim)
- POJ3625 Building Roads(最小生成树,Kruskal)
- POJ 3625 Building Roads(Prim水题)
- Building Roads(prim 最小生成树)
- POJ 1251 Jungle Roads (Prim算法)&& (Kruskal算法)
- hdu 1301 Jungle Roads &&poj 1251 (prim 或者kruskal)
- POJ 1251 Jungle Roads(Prim or kruskal)
- hdu1102 - Constructing Roads (求最小生成树) (Prim & Kruskal)
- poj 1251 Jungle Roads 解题报告(kruskal+prim)
- EXTJS与java后台交互(一)DWR使用
- Java解析XML
- 从输入网址到显示网页-1-简介
- PCL:将点云写入PCD文件中
- single-number-ii
- Building Roads(Prim+Kruskal)
- mkdir命令
- Java多线程-实例解析
- PostgreSQL之Json类型使用总结
- NMF 非负矩阵分解(Non-negative Matrix Factorization)实践
- easyUI前端分页与后台分页总结
- 【后台】菜单项选择列表增加自定义类型
- 基于GMM-UBM的说话人识别 MSR Identity Toolkit
- python学习——迭代器使用说明