【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 2Sample 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 ; }?
?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【POJ - 1751】Highways (最小生成树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跑分低了6%:微软免费杀软Defende
- 下一篇: 算法笔记 -- 离散化