zoukankan      html  css  js  c++  java
  • POJ1751 Highways

    Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu

    Description

    The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

    Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

    The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length. 

    Input

    The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built. 

    The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of i th town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location. 

    The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway. 

    Output

    Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space. 

    If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty. 

    Sample Input

    9
    1 5
    0 0 
    3 2
    4 5
    5 1
    0 4
    5 2
    1 2
    5 3
    3
    1 3
    9 7
    1 2

    Sample Output

    1 6
    3 7
    4 9
    5 7
    8 3
    

    Source

    最小生成树。图很稠密,用prim大概比较合适。

    然而因为prim里的mini误开成了int,wa了一串233

     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<vector>
     8 using namespace std;
     9 const int mxn=1810;
    10 int read(){
    11     int x=0,f=1;char ch=getchar();
    12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    14     return x*f;
    15 }
    16 struct point{
    17     int x,y;
    18 }p[mxn];
    19 double dist(int a,int b){
    20     return sqrt((double)(p[a].x-p[b].x)*(p[a].x-p[b].x)+(double)(p[a].y-p[b].y)*(p[a].y-p[b].y));
    21 }
    22 double mp[mxn][mxn];
    23 int ans[mxn];
    24 int n,m;
    25 int cm[mxn];
    26 double dis[mxn];
    27 bool vis[mxn];
    28 void Prim(){
    29     int i,j;
    30     for(i=1;i<=n;i++){
    31         dis[i]=mp[1][i];
    32         cm[i]=1;//记录该备选边的出发点 
    33     }
    34     dis[1]=0;vis[1]=1;
    35     for(i=1;i<=n;i++){
    36         double mini=1e9;int pos=0;
    37         for(j=1;j<=n;j++){
    38             if(!vis[j] && dis[j]<mini){
    39                 mini=dis[j];
    40                 pos=j;
    41             }
    42         }
    43         if(!pos)break;
    44         if(mp[cm[pos]][pos])printf("%d %d
    ",cm[pos],pos);
    45         vis[pos]=1;
    46         for(j=1;j<=n;j++){
    47             if(!vis[j] && dis[j]>mp[pos][j]){
    48                 dis[j]=mp[pos][j];
    49                 cm[j]=pos;
    50             }
    51         }
    52     }
    53     return;
    54 }
    55 int main(){
    56     int i,j;
    57     n=read();
    58     for(i=1;i<=n;i++){
    59         p[i].x=read();p[i].y=read();
    60     }
    61     for(i=1;i<n;i++)
    62         for(j=i+1;j<=n;j++)
    63             mp[i][j]=mp[j][i]=dist(i,j);
    64     m=read();
    65     int u,v;
    66     for(i=1;i<=m;++i){
    67         u=read();v=read();
    68         mp[u][v]=mp[v][u]=0;
    69     }
    70     Prim();
    71     return 0;
    72 }
  • 相关阅读:
    vscode 在linux中,切换语言模式快捷键
    转载大神的一篇文章----【如何选择开源许可证?】
    Kibana6.x.x——源码发布
    Kibana6.x.x——【Running "run:optimizeBuild" (run) task】出现警告信息
    linux系统为文件添加执行权限
    Kibana6.x.x——执行yarn build出现的警告信息记录
    Kibana6.x.x——导航权限控制入门
    poj 2187:Beauty Contest(计算几何,求凸包,最远点对)
    poj 2386:Lake Counting(简单DFS深搜)
    蓝桥杯 第三届C/C++预赛真题(10) 取球游戏(博弈)
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6017372.html
Copyright © 2011-2022 走看看