当前位置:网站首页>Highways「建议收藏」
Highways「建议收藏」
2022-08-10 11:49:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
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 2Sample Output
1 6
3 7
4 9
5 7
8 3code:
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.cn/129970.html原文链接:https://javaforall.cn
边栏推荐
- LeetCode 369. Plus One Linked List
- 【list合并】多个list合并为一个list
- Flutter气泡框实现
- codevs 2370 Small room tree (LCA)
- dedecms supports one-click import of Word content
- 2016,还是到了最后
- 【集合】HashSet和ArrayList的查找Contains()时间复杂度
- If someone asks you about distributed transactions again, throw this to him
- LT8911EXB MIPI CSI/DSI转EDP信号转换
- LeetCode 109. 有序链表转换二叉搜索树
猜你喜欢

人脸考勤是选择人脸比对1:1还是人脸搜索1:N?

加密游戏:游戏的未来

Does face attendance choose face comparison 1:1 or face search 1:N?

如何培养ui设计师的设计思维?

IDC第一的背后,阿里云在打造怎样的一朵“视频云”?

一文读懂NFT数字藏品为何风靡全球?

2016,还是到了最后

面试美团被问到了Redis,搞懂这几个问题,让你轻松吊打面试官

吃透Chisel语言.36.Chisel实战之以FIFO为例(一)——FIFO Buffer和Bubble FIFO的Chisel实现

If someone asks you about distributed transactions again, throw this to him
随机推荐
嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
mpf6_Time Series Data_quandl_correct kernel PCA_AIC_BIC_trend_log_return_seasonal_decompose_sARIMAx_ADFull
开源的作者,也有个生活问题
jlink 与 swd 接口定义
爱可可AI前沿推介(8.10)
codevs 2370 Small room tree (LCA)
日记16
苹果逆势扩大iPhone 14系列备货,总量或达9500万部
第六届”蓝帽杯“全国大学生网络安全技能大赛半决赛部分WriteUp
蚂蚁金服+拼多多+抖音+天猫(技术三面)面经合集助你拿大厂offer
LeetCode 109. Sorted Linked List Conversion Binary Search Tree
LeetCode 82. Remove Duplicate Elements in Sorted List II
Microchip launched a high-performance 77GHz millimeter-wave radar chip, and has received tens of thousands of orders before mass production
ssm框架搭建过程[通俗易懂]
一文详解 implementation api embed
LeetCode 92. 反转链表 II
配置swagger
LeetCode 61. Rotating linked list
LeetCode 82. 删除排序链表中的重复元素 II
【list合并】多个list合并为一个list