Highways「建议收藏」

Highways「建议收藏」HighwaysTimeLimit:1000MS MemoryLimit:10000KTotalSubmissions:14613 Accepted:4211 SpecialJudgeDescriptionTheislandnationofFlatopiaisperfectlyflat.

大家好,又见面了,我是你们的朋友全栈君。

Highways
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14613   Accepted: 4211   Special Judge

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

code:

prim算法

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
const int maxn=755;
const int INF=1000000;
double graph[maxn][maxn];
double lowcost[maxn];/*lowcost表示每个点的最小花费;*/
int closet[maxn];/*closet表示最小花费对应相连的点*/
int visited[maxn];/*visited区分两个集合*/
int n;/*n个点*/

struct dot{
    double x,y;
}a[maxn];

double f(dot p,dot q){
    return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
}

void createGraph(){
    memset(graph,0,sizeof(graph));
    memset(lowcost,0,sizeof(lowcost));
    memset(closet,0,sizeof(closet));
    memset(visited,0,sizeof(visited));
    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++){
        if(i==j) graph[i][j]=INF;
        else graph[i][j]=graph[j][i]=f(a[i],a[j]);
    }
}

void prim(){
    visited[0]=1;/*选中第一个点*/
    for(int i=0;i<n;i++){
        lowcost[i]=graph[i][0];/*每个点与第一个点的权值*/
        closet[i]=0;/*与i点相连的是第一个点*/
    }
    for(int i=1;i<n;i++){  /*剩下n-1个点*/
        int k=0;
        double minn=lowcost[0];
        for(int j=0;j<n;j++){
            if(!visited[j] && lowcost[j]<minn){
                minn=lowcost[j];
                k=j;
            }
        }
        if(graph[k][closet[k]]!=0) printf("%d %d\n",k+1,closet[k]+1);
        visited[k]=1;
        for(int t=0;t<n;t++){  /*松弛操作*/
            if(!visited[t] && lowcost[t]>graph[t][k]){
                lowcost[t]=graph[t][k];
                closet[t]=k;
            }
        }
    }
}

int main()
{
 //   freopen("input.txt","r",stdin);
    int m;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%lf%lf",&a[i].x,&a[i].y);
    }
    createGraph();
    scanf("%d",&m);
    int b,c;
    while(m--){
        scanf("%d%d",&b,&c);
        graph[b-1][c-1]=graph[c-1][b-1]=0;
    }
    prim();
}

Kruskal算法:注意要用G++提交

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
const int MAXN = 755; /*结点数目上限*/
int pa[MAXN];    /*pa[x]表示x的父节点*/
int rank[MAXN];    /*rank[x]是x的高度的一个上界*/
int flag;
struct node{
    int x,y;
    double w;
}edge[MAXN*MAXN];

struct dot{
    double x,y;
}a[MAXN];

bool cmp(node p,node q){
    return p.w<q.w;
}

double f(dot p,dot q){
    return sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
}

/*创建一个单元集*/
void make_set(int x)
{
    pa[x] = x;
    rank[x] = 0;
}

/*带路径压缩的查找*/
int find_set(int x)
{
    if(x != pa[x])
        pa[x] = find_set(pa[x]);
    return pa[x];
}

/*按秩合并x,y所在的集合*/
void union_set(int xx, int yy,double w)
{
    int x = find_set(xx);
    int y = find_set(yy);
    if(x == y)return ;
    if(rank[x] > rank[y])/*让rank比较高的作为父结点*/
    {
        pa[y] = x;
    }
    else
    {
        pa[x] = y;
        if(rank[x] == rank[y])
            rank[y]++;
    }
    if(w!=0){
        printf("%d %d\n",xx,yy);
        flag=1;
    }
}

int main()
{
   // freopen("input.txt","r",stdin);
    int n,m;
    scanf("%d",&n);
    for(int i=0;i<MAXN;i++)
        make_set(i);
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&a[i].x,&a[i].y);
    }
    int k=0;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            edge[k].x=i;
            edge[k].y=j;
            edge[k].w=f(a[i],a[j]);
            k++;
        }
    }
    scanf("%d",&m);
    int b,c;
    while(m--){
        scanf("%d%d",&b,&c);
        union_set(b,c,0);
    }
    sort(edge,edge+k,cmp);
    flag=0;
    for(int i=0;i<k;i++){
        union_set(edge[i].x,edge[i].y,edge[i].w);
    }
    if(!flag) printf("\n");
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/129970.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 五种常用手机Java编程软件[通俗易懂]

    五种常用手机Java编程软件[通俗易懂]越来越多的朋友都对编程感兴趣,编程需要工具,所以大家也想知道有哪些好用的java编程工具,接下来系哦啊吧就为大家介绍几款常用的相关编程工具。1.JDKJDK-java开发工具包JDK是Java开发工具包,基本上每个研究java的人都首先在机器上安装JDK,那么他有哪些部件呢?下面是运行java时真正工作的四个文件夹:bin、include、lib、jrebin:是最重要的是编译器包括:java和jvm与头文件lib:类库jre:java运行时环境的交互一般用于java程序的开发,而jre只运行类而不编译

    2022年7月21日
    12
  • matlab Lasso回归

    matlab Lasso回归Lasso回归clc,clear;closeall;data=[1.541.611.621.661.711.721.731.861.9222.212.292.342.382.422.442.572.642.712.852.933.013.143.223.343.493.553.793.994.1220.120.120.320.420.420.520.620.720.921.121.321.521

    2022年5月22日
    91
  • 关于iPhone尺寸与分辨率[通俗易懂]

    浅谈不同型号iPhone的尺寸与不同的分辨率首先谈谈编者对分辨率这个概念的认知,分辨率与清晰度挂钩,同样尺寸的视图,分辨率越高清晰度越好。另外还要引出一个重要的概念:PPI(pixelsperinch)PPI是图像分辨率的单位,图像PPI值越高,画面的细节就越丰富,因为单位面积的像素数量越多,一般PPI>300人眼难以分辨出来。分辨率分为水平和垂直两种,

    2022年4月17日
    146
  • 错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap

    错误: 在类中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)否则 JavaFX 应用程序类必须扩展javafx.ap最近在使用eclipse编写java程序时遇到这样一个问题:错误在类中找不到main方法,请将main方法定义为publicstaticvoidmain(String[]args)否则JavaFX应用程序类必须扩展javafx.application.Application看到这样的问题让我一头雾水,因为main方法已经写出解决这个问题可以点开eclipse-&amp;gt…

    2022年5月31日
    38
  • JavaScript页面后退或关闭

    JavaScript页面后退或关闭后退方法history.go(-1)如果无法后退时,会返回一个undefined,利用这一点来判断是否可以后退,不能后退时执行window.close();if(!history.go(-1)){window.close();}window.close();对于火狐浏览器经常会无法关闭。因为火狐浏览器只能关闭通过JS新建的窗口,即有target=”_blank”属性的标签,或者

    2022年7月25日
    47
  • HTTP和HTTPS有什么区别? 什么是SSL证书?使用ssl证书优势?

    HTTP和HTTPS有什么区别? 什么是SSL证书?使用ssl证书优势?

    2021年10月25日
    49

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号