zoukankan      html  css  js  c++  java
  • hdu 4463 有一条边必须加上 (2012杭州区域赛K题)

    耐克店 和 苹果店必须相连


    Sample Input
    4
    2 3
    0 0
    1 0
    0 -1
    1 -1
    0

    Sample Output
    3.41

     1 # include <iostream>
     2 # include <cstdio>
     3 # include <cstring>
     4 # include <algorithm>
     5 # include <cmath>
     6 # define LL long long
     7 using namespace std ;
     8 
     9 const int INF=0x3f3f3f3f;
    10 const int MAXN=110;
    11 bool vis[MAXN];
    12 double lowc[MAXN];
    13 int n ;
    14 double cost[MAXN][MAXN] ;
    15 
    16 struct poin
    17 {
    18     int x ;
    19     int y ;
    20 }p[MAXN];
    21 
    22 double Prim()//点是0~n-1
    23 {
    24     double ans=0;
    25     memset(vis,false,sizeof(vis));
    26     vis[0]=true;
    27     for(int i=1;i<n;i++)lowc[i]=cost[0][i];
    28     for(int i=1;i<n;i++)
    29     {
    30         double minc=INF;
    31         int p=-1;
    32         for(int j=0;j<n;j++)
    33             if(!vis[j]&&minc>lowc[j])
    34             {
    35                 minc=lowc[j];
    36                 p=j;
    37             }
    38             if(minc==INF)return -1;//原图不连通
    39             ans+=minc;
    40             vis[p]=true;
    41             for(int j=0;j<n;j++)
    42                 if(!vis[j]&&lowc[j]>cost[p][j])
    43                     lowc[j]=cost[p][j];
    44     }
    45     return ans;
    46 }
    47 
    48 int main()
    49 {
    50 
    51    // freopen("in.txt","r",stdin) ;
    52     while(scanf("%d" , &n) != EOF)
    53     {
    54         if (n == 0)
    55             break ;
    56         int i , j ;
    57         int pp , qq ;
    58         scanf("%d %d" , &pp , &qq) ;
    59         for (i = 0 ; i < n ; i++)
    60             scanf("%d %d" , &p[i].x , &p[i].y) ;
    61         for (i = 0 ; i < n ; i++)
    62             for (j = i+1 ; j < n ; j++)
    63             {
    64                 double t = sqrt((double)(p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y)) ;
    65                 cost[i][j] = t ;
    66                 cost[j][i] = t ;
    67             }
    68         double k = cost[pp-1][qq-1] ;
    69         cost[pp-1][qq-1] = 0 ;
    70         cost[qq-1][pp-1] = 0 ;
    71         k += Prim() ;
    72         printf("%.2lf
    " , k) ;
    73 
    74 
    75     }
    76     return 0 ;
    77 }
    View Code
  • 相关阅读:
    使用命令行工具创建.Net Core应用程序
    WinForm--DataGridView复制单元格数据
    WinForm--DataGridView导出数据到CSV文件
    进程和线程(线程是轻量级进程)(下)
    进程和线程(线程是轻量级进程)(中)
    C# 泛型(Generic)
    C# 事件(Event)
    C# 委托(Delegate)
    C# 反射
    C# 程序集(Assembly)
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/4585208.html
Copyright © 2011-2022 走看看