poj 3625 Building Roads-Prim(最小生成树)
来源:互联网 发布:淘宝怎么打不开了 编辑:程序博客网 时间:2024/04/30 17:59
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 preexistingM 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 andYi
* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farmi and farmj.
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 11 13 12 34 31 4
Sample Output
4.00
好吧,。这题比较强大,不过我还是用不变的Prim模版做的。。哈哈。
先输入n,m。下面的n行,是输入n行坐标(x,y),在就是输入m行的(i,j)//
代码:
#include<cstdio>#include<cmath>#include<string>#include<cstring>#include<algorithm>#include<iostream>#include<cstring>using namespace std;double road[1010][1010];double dist[1010];int vis[1010];int n,m,i,j,rec;double d,sum,max;struct node{double x,y;};struct node data[1010];void init(){while(m--){scanf("%d %d",&i,&j);road[i][j]=road[j][i]=0;;}}void solve(){for(i=1;i<=n;i++){for(j=i+1;j<=n;j++){d=sqrt((data[i].x-data[j].x)*(data[i].x-data[j].x)+(data[i].y-data[j].y)*(data[i].y-data[j].y));road[i][j]=road[j][i]=d;}}}double Prim(){memset(vis,0,sizeof(vis));double sum=0.0; int k;for(int i=1;i<=n;i++)dist[i]=road[1][i];vis[1]=1; for(int i=1;i<n;i++){double min=100000000.0;for(int j=1;j<=n;j++)if(!vis[j]&&dist[j]<min){min=dist[j];k=j;}sum+=min;vis[k]=1;for(int j=1;j<=n;j++){if(!vis[j]&&road[k][j]<dist[j])dist[j]=road[k][j];}}return sum;}int main(){while(scanf("%d %d",&n,&m)!=EOF){for(i=1;i<=n;i++)scanf("%lf %lf",&data[i].x,&data[i].y);memset(road,0,sizeof(road));solve();init();printf("%.2lf\n",Prim());}return 0;}
- poj 3625 Building Roads-Prim(最小生成树)
- POJ 3625 Building Roads 最小生成树
- POJ 3625 Building Roads(最小生成树)
- Building Roads(prim 最小生成树)
- poj 3625 Building Roads (最小生成树)
- poj 3625 Building Roads(图论:最小生成树)
- 最小生成树 POJ 3625Building Roads解题报告
- poj 3625 Building Roads(最小生成树)
- poj 3625 Building Roads(最小生成树)
- poj 3625 Building Roads (prim)
- pku 3625 Building Roads(最小生成树)
- poj 1251 Jungle Roads prim最小生成树基础
- poj 2421 Constructing Roads prim最小生成树 基础!!!
- POJ 1251 Jungle Roads(最小生成树裸prim)
- Poj 2421 Constructing Roads(Prim 最小生成树)
- poj 2421 Constructing Roads(最小生成树prim)
- POJ 1251 Jungle Roads (最小生成树 Prim普里姆算法)
- POJ-1251 Jungle Roads 最小生成树(prim算法)
- Boost.Filesystem
- spring bean scope 的几种类型
- 谷歌地图发布海底街景地图
- java 导出word文档
- 字符串笔试题一
- poj 3625 Building Roads-Prim(最小生成树)
- 11.2.0.3+ASM 安装报 ohasd failed to start
- 美国在线地图软件测评:谷歌居首必应次之
- 开源的python web框架:web2py
- 时域和频域
- IO___C基础部分(EOF, 回车换行,getchar(), getch())
- Linux内存管理子系统
- 克服困难
- SEO精华小结