zoukankan      html  css  js  c++  java
  • tzoj4679 Building Roads(最小生成树+Prim算法)

    时间限制(普通/Java):1000MS/3000MS     内存限制:65536KByte
    总提交: 245            测试通过:79

    描述

     

    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 (XiYi) on the plane (0 ≤ X≤ 1,000,000; 0 ≤ Y≤ 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.

    输入

     

    * Line 1: Two space-separated integers: N and M
    * Lines 2..N+1: Two space-separated integers: Xand 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.

    输出

     

    * 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.

    样例输入

    4 1
    1 1
    3 1
    2 3
    4 3
    1 4

    样例输出

     4.00

    题目来源

    USACO 2007 December Silver

     
     
    tzoj卡掉了Kruskal算法,只能交Prim
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int n,m;
     5 const int MAX=1005;
     6 const double INF=0x3f3f3f3f;
     7 struct Point{
     8     double x,y;
     9 }p[MAX];
    10 double g[MAX][MAX];
    11 bool vis[MAX];
    12 double mind[MAX];
    13 
    14 inline double DST(Point a,Point b){
    15     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    16 }
    17 
    18 double prim(){
    19     double res=0,minn;
    20     int k;
    21     vis[1]=true;
    22     for(int i=1;i<=n;i++){
    23         mind[i]=g[1][i];
    24     }
    25     for(int i=1;i<=n-1;i++){
    26          minn=INF;
    27          for(int j=1;j<=n;j++){
    28             if(!vis[j]&&mind[j]<minn){
    29                 minn=mind[j];
    30                 k=j;
    31             }
    32          }
    33          vis[k]=true;
    34          res+=minn;
    35          for(int j=1;j<=n;j++){
    36             if(!vis[j]){
    37                 mind[j]=min(mind[j],g[k][j]);
    38             }
    39          }
    40     }
    41     return res;
    42 }
    43 
    44 int main(){
    45     int u,v;
    46     scanf("%d%d",&n,&m);
    47     for(int i=1;i<=n;i++){
    48         scanf("%lf%lf",&p[i].x,&p[i].y);
    49     }
    50     for(int i=1;i<=n;i++){
    51         for(int j=i+1;j<=n;j++){
    52             g[j][i]=g[i][j]=DST(p[i],p[j]);
    53         }
    54     }
    55     for(int i=0;i<m;i++){
    56         scanf("%d%d",&u,&v);
    57         g[u][v]=g[v][u]=0;
    58     }
    59     printf("%.2f
    ",prim());
    60 }
     
  • 相关阅读:
    centos7 计划任务 定时运行sh
    Nginx负载均衡配置 域名和IP 访问时从java request.getServerName()获取不同问题解决
    windows2012激活方式 2016激活方式 windows10激活方式
    CentOS7 安装Chrome
    使用 go-cqhttp 在龙芯和其他平台搭建qq机器人
    KDE桌面无故特效消失和图标黑底
    SDUST 小学期飞机大战简述
    SDUST 小学期飞机大战简述
    宝塔中极速安装的PHP如何使用AMQP连接RabbitMQ
    Electron登录注册桌面应用源码+安装文件的打包方法
  • 原文地址:https://www.cnblogs.com/ChangeG1824/p/11740765.html
Copyright © 2011-2022 走看看