【POJ - 1751】Highways (最小生成树)

题干:

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

题目大意:

     首行给出N,代表有1~N共N个点。接下来N行,每行两个数x,y,代表第i个点的坐标。接着给出M,接着M行,每行两个数x,y,代表第x个点和第y个点已经联通(即x到y的权值为0),建立最小生成树,输出生成树中权值不为0的边的两端的点的编号。

思路:最坏的情况需要遍历图中的每一条边,很明显的稠密图,优先选用普利姆算法。 
建图:根据坐标建立无向图,权值设为距离的平方即可,这样可以避免sqrt后权值变为double型,避免精度损失。对于已联通的两点,更新这两点的权值为0即可。 
输出边:技巧看代码。

AC代码:

 prim算法:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
//普利姆,注意建图技巧
const int maxn=751;
const int INF=0x3f3f3f3f;
int map[maxn][maxn];
int dis[maxn];
int vis[maxn];
int Edge[maxn];//i到Edge[i]是一条生成树内的边
struct node
{int x;int y;
} Point[maxn]; //第i个点的坐标
int N;//点的数量
int M;//更新边的数量
void init()
{scanf("%d",&N);for(int i=1; i<=N; i++)//建图{scanf("%d%d",&Point[i].x,&Point[i].y);for(int j=1; j<i; j++)//为什么这里不取sqrt,因为完全没必要map[i][j]=map[j][i]=(Point[i].x-Point[j].x)*(Point[i].x-Point[j].x)+(Point[i].y-Point[j].y)*(Point[i].y-Point[j].y);map[i][i]=INF;//自己不可能到自己}scanf("%d",&M);int x,y;while(M--)//更新图{scanf("%d%d",&x,&y);map[x][y]=map[y][x]=0;}memset(vis,0,sizeof(vis));vis[1]=1;for(int i=1; i<=N; i++){dis[i]=map[i][1];Edge[i]=1;//初始化为存储i到1的边}
}
void Prim()
{for(int i=1; i<N; i++){int minn=INF;int point_minn;for(int j=1; j<=N; j++)if(vis[j]==0&&minn>dis[j]){minn=dis[j];point_minn=j;}vis[point_minn]=1;for(int k=1; k<=N; k++)if(vis[k]==0&&dis[k]>map[point_minn][k]){Edge[k]=point_minn;//这里是输出方式的技巧dis[k]=map[point_minn][k];}if(map[Edge[point_minn]][point_minn])printf("%d %d\n",Edge[point_minn],point_minn);}
}
int main()
{init();Prim();return 0;
}
/*
题意:首行给出N,代表有1~N共N个点。接下来N行,每行两个数x,y,代表第i个点的坐标。接着给出M,接着M行,每行两个数x,y,代表第x个点和第y个点已经联通(即x到y的权值为0),建立最小生成树,输出生成树中权值不为0的边的两端的点的编号。思路:最坏的情况需要遍历图中的每一条边,很明显的稠密图,优先选用普利姆算法。 
建图:根据坐标建立无向图,权值设为距离的平方即可,这样可以避免sqrt后权值变为double型,避免精度损失。对于已联通的两点,更新这两点的权值为0即可。 
输出边:技巧看代码。
*/ 

 

克鲁斯卡尔:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int f[10000 + 5];struct Node {double x,y;
} node[10000 + 5];struct Node2 {int s,e;double c;
} node2[1000000 + 5] ;
int n,m;
int cnt;int getf(int v) {return v==f[v]?v:f[v]=getf(f[v]);
}
void merge(int u,int v) {int t1=getf(u);int t2=getf(v);if(t1!=t2) {f[t2]=t1;}
}
bool cmp(const Node2 & a,const Node2 & b) {return a.c<b.c;
}
double dis(const Node & a,const Node & b)
{return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int main()
{int u,v;cin>>n;for(int i = 1; i<=n; i++) f[i]=i;//初始化	for(int i = 1; i<=n; i++) {scanf("%lf %lf",&node[i].x,&node[i].y);}cin>>m;while(m--) {scanf("%d %d",&u,&v);if(getf(u) != getf(v)) {			cnt++;//这是不对滴  比如 1 2   2 3   }merge(u,v);}int top=0;//top从1开始一直到n相当于模拟 for(int top = 1; top<=n; top++) 的过程只不过最后top==n for(int i = 1; i<=n; i++) {//那道 需要用i的值 然后排序 的题在哪来着? for(int j = i + 1; j<=n; j++) {if(getf(i) == getf(j) ) continue;//写getf(v) == getf(u)写顺手了? ++top;node2[top].s = i;node2[top].e = j;node2[top].c = dis(node[i], node[j]);//fabs(node[i].x-node[j].x)+fabs(node[i].y-node[j].y);//abs(node[u].x-node[v].x)+abs(node[u].y-node[v].y);}}sort(node2+1,node2+top+1,cmp);
//	printf("top = %d\n",top);
//	for(int i = 1; i<=top; i++) {
//		printf("%d  %d   %f\n",node2[i].s,node2[i].e,node2[i].c);
//	}for(int i = 1; i<=top; i++) {if(getf(node2[i].s) == getf(node2[i].e ) )continue;//这一步别忘!只有他俩尚未连通才能有下面的操作 merge(node2[i].s,node2[i].e); printf("%d %d\n",node2[i].s,node2[i].e);cnt++; if(cnt== n-1) break;} return  0 ;
}

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442068.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jupyter怎么安装jieba_AI工具:Anaconda中Jupyter不能import已安装module问题解决

jupyter模式下写代码时,通过pip install package命令行安装package完成之后,无法在jupyter模式下import &#xff0c;这是个通用的问题&#xff0c;我这里遇到的是import jieba&#xff0c;可能import 别的package也会出现&#xff0c;记录下&#xff0c;也花了点时间排查。。。…

Sql Server数据库设置一个账户只能看到一个数据库

1 新建登录名&#xff0c;注意不要设置用户映射&#xff0c;服务器角色只选择public&#xff08;默认必选&#xff0c;无法去掉&#xff0c;可以添加其他服务器角色&#xff0c;但是不要添加查看所有数据库的权限&#xff0c;接下来会去掉public的查看所有数据库权限&#xff0…

boot lib分离 spring_spring boot + gradle打包bootJar分离lib

以前项目打包一直是用的maven&#xff0c;最近新开一个项目&#xff0c;使用的是spring boot 2.11 gradle 4.10.3&#xff0c;在打包的时候分离lib折腾了好几天&#xff0c;网上找了很多方法都不成功&#xff0c;老是卡在configurations.compile这里&#xff0c;总是获取不到正…

【POJ - 3494】Largest Submatrix of All 1’s(加一点思维后化成 单调栈)

题干&#xff1a; Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements. Input The input contains multiple test cases. Each test case begins with m and n (1 ≤ m…

如何在修改计算机设置时,不再弹出提示框?

1 打开控制面板&#xff0c;找到安全与维护&#xff1b; 2 进入安全与维护&#xff0c;在安全中找到“用户账户控制UAC”&#xff0c;点击打开“更改设置” 3 进行设置&#xff0c;“从不通知”设置之后&#xff0c;针对计算机的所有更改将不再提示

doe五步法_DOE方法介绍

DOE, Design of Experiment。就是试验设计。想知道对于某个过程的产生影响的诸多因素中对输出结果影响最关键的因素有哪些&#xff0c;就可以用DOE方法来设计一系列试验&#xff0c;获知关键影响因素。通常的确定对输出变量最关键影响因子的试验做法有三种。1. 经验猜测法。首先…

【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)

题干&#xff1a; Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a…

matlab两张图片合成一张_11. 图像合成与图像融合

本文同步发表在我的微信公众号“计算摄影学”&#xff0c;欢迎扫码关注【转载请注明来源和作者】我们终于进入了新的篇章。这一次我来给大家介绍一下图像合成与融合。我们经常看到一些很奇妙的PS技术&#xff0c;例如下面这张&#xff0c;它把1928年的一位叫做Frankie Yale的黑…

C#学习,Web界面打开winform程序

1 Web打开winform程序&#xff0c;需要使用的标签为a标签 <a href"SelfName://">连接提示</a> //SelfName 是自己的自定义协议2 Winform程序一般不需要做任何操作&#xff0c;如果需要参数传递的情况下&#xff0c;可以将Winform的Program.cs程序修…

【POJ - 2349】【UVA - 10369】 Arctic Network(最小生成树求权值第k大的边)(内附两种算法)

题干&#xff1a; The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver a…

asp.net MVC结合Blazor开发学习

1建立MVC项目&#xff08;.net 6&#xff09;; 2 在项目启动文件Program.cs中添加Blazor框架&#xff1b; var builder WebApplication.CreateBuilder(args);// Add services to the container. builder.Services.AddControllersWithViews(); builder.Services.AddServerSid…

澄海口袋机器人_汕头市澄海区在2019年汕头市中小学智能机器人竞赛上取得优异成绩...

4月14日&#xff0c;2019年汕头市中小学智能机器人竞赛在汕头市蓬鸥中学举行。本次活动由汕头市教育局和汕头市科协联合主办&#xff0c;全市93所学校的249个队伍503名选手报名参赛&#xff0c;大赛设“高铁时代”机器人现场拼装赛、丛林任务挑战赛、超级轨迹赛、综合技能比赛、…

【POJ - 1703】Find them, Catch them(带权并查集之--种类并查集 权为与父节点关系)

题干&#xff1a; Find them, Catch them Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 36176 Accepted: 11090 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Ga…

h5前端如何实现F11全屏功能

网站全部实现小demo&#xff1a;兼容谷歌和Edge {Layout null; }<!DOCTYPE html><html> <head><meta name"viewport" content"widthdevice-width" /><title>Index</title><script src"~/Content/jquery/jque…

于小c三国语言_云顶之弈:三国成最强打工羁绊 校长教学顺滑转九五

虎牙校长是九五阵容的专业户&#xff0c;虽然版本在不断地更新&#xff0c;云顶的整体走向也在不断地发生变化&#xff0c;但九五至尊的强度一直都是TOP0级别&#xff0c;一旦成型第四保底&#xff0c;吃鸡都变得轻而易举。而整套九五的运营关键在于如何连胜、保证自身血量健康…

【HDU - 3172】Virtual Friends(带权并查集--权为集合元素个数)

题干&#xff1a; These days, you can do all sorts of things online. For example, you can use various websites to make virtual friends. For some people, growing their social network (their friends, their friends friends, their friends friends friends, and …

java null转换jason_常见java问题及解决办法汇总(干货可收藏)

Java Exception&#xff1a;1、Error2、Runtime Exception 运行时异常3、Exception4、throw 用户自定义异常异常类分两大类型&#xff1a;Error类代表了编译和系统的错误&#xff0c;不允许捕获&#xff1b;Exception类代表了标准Java库方法所激发的异常。Exception类还包含运行…

asp.net 网站开发,word导出

曾经有一个word导出案例&#xff1a; web保存word资源 后来发现可以使用MVC的File方法保存成相应的文件&#xff0c;特别做个记录。 1 前端 //网页 <form method"post" action"/Home/WordExport" id"wordTmp"><input type"text&…

【HDU - 3038】How Many Answers Are Wrong (带权并查集--权为区间和)

题干&#xff1a;&#xff08;&#xff09; TT and FF are ... friends. Uh... very very good friends -________-b FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a s…

drop sqlite 多个表_SQLite简介与安装

SQLite简介&#xff1a;SQLite是一款轻型的数据库&#xff0c;是遵守ACID的关系型数据库管理系统&#xff0c;它包含在一个相对小的C库中&#xff0c;实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。就像其他数据库&#xff0c;SQLite 引擎不是一个独立的进…