POJ 2485 Highways

Source: Internet
Author: User

Highways
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11071 Accepted: 3145 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 have already constructed a number of highways connecting some of the Most important towns. However, there is still some towns that you can ' t reach via a highway. It's necessary to build more highways so it'll be possible to drive between any pair of towns without leaving the Highway System.

Flatopian towns is numbered from 1 to N and town I have a position given by the Cartesian coordinates (xi, Yi). Each highway connects Exaclty and towns. All highways (both the original ones and the ones, so is to being built) follow straight lines, and thus their length is EQ UAL to Cartesian distance between towns. All highways can is used in both directions. Highways can freely cross all other, but a driver can only switch between highways at a town that's located at the end O F both highways.

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

Input

The input consists of the parts. The first part describes all towns in the country, and the second part describes all of the highways that has already bee N built.

The first line of the input file contains a single integer n (1 <= n <=), representing the number of towns. The next N lines each contain of integers, Xi and Yi separated by a space. These values give the coordinates of ITH town (for I from 1 to N). Coordinates'll has an absolute value no greater than 10000. Every town have a unique location.

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

Output

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

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

Sample Input

91 50 0 3 24 55 10 45 21 25 331 39 71 2

Sample Output

1 63 74) 95 7

8 3

Test instructions: There are some villages, giving coordinates, the village order from 1 onwards, and then give a few connected villages! Find the villages that are needed to connect all the villages and the shortest road!

Two kinds of code:

Prim

#include <stdio.h> #include <algorithm> #include <math.h>using namespace std; #define N 1000#define INF 0XFFFFFFF int country,workout;int x[n],y[n],v[n],per[n];d ouble map[n][n];d ouble d (int i,int j) {Double C;return c=sqrt (( X[I]-X[J]) * (X[i]-x[j]) + (Y[i]-y[j]) * (Y[i]-y[j]));} void Prim () {double mincost,cost[n];int i,j,next;for (i=1;i<=country;i++) {cost[i]=map[1][i]; v[i]=0; per[i]=1;// Record the center point at this time} v[1]=1;  for (i=2;i<=country;i++) {mincost=inf;    for (j=1;j<=country;j++)//search for the nearest distance to the village {if (!v[j]&&mincost>cost[j]) {next=j; MINCOST=COST[J];}} if (Map[per[next]][next]) printf ("%d%d\n", Per[next],next), V[next]=1;for (j=1;j<=country;j++) {if (!v[j]& &cost[j]>map[next][j]) {cost[j]=map[next][j];p er[j]=next;//Change Center Point}}}}int main () {scanf ("%d", &country); int I,j;for (i=1;i<=country;i++) {scanf ("%d%d", &x[i],&y[i]);} for (i=1;i<=country;i++) for (j=i+1;j< =country;j++) {Map[j][i]=map[i][j]=d (i,j);} scanf ("%d", &workout); int A,b,c;for (i=1;i<=workout;i++) {scanf ("%d%d", &a,&b); map[b][a]=map[a][b]=0;} prim (); return 0;} 
Kruskal

#include <stdio.h> #include <algorithm> #include <math.h>using namespace std; #define N 1100int Country , Road,workout,set[n];int x[n],y[n];struct line{int bg;int ed;double Dis;} Num[n*n];d ouble d (int i,int j) {Double C;return c=sqrt ((X[i]-x[j]) *1.0* (X[i]-x[j]) + (Y[i]-y[j]) * (Y[i]-y[j]));} int CMP (line A,line b) {return a.dis<b.dis;}     int find (int p)//find {int t;int child=p;while (p!=set[p]) p=set[p];while (child!=p) {t=set[child];set[child]=p;child=t;} Return P;//return P==set[p]?p:set[p]=find (Set[p]);} BOOL Merge (int x,int y)//merge {int fx=find (x); int fy=find (y); if (fx!=fy) {Set[fx]=fy;return true;} return false;} int main () {scanf ("%d", &country), int i,j;for (i=1;i<=country;i++) {set[i]=i; scanf ("%d%d", &x[i],&y[i] ); } int a,b,c; scanf ("%d", &workout);     while (workout--)//If already repaired, direct merge {scanf ("%d%d", &a,&b); C=merge (A, b); } int t=0;     for (i=1;i<country;i++) for (j=i+1;j<=country;j++) {num[t].bg =i;     Num[t].ed =j; Num[t].dis =d (I,J);   t++;   } sort (num,num+t,cmp);  for (i=0;i<t;i++) {if (merge (num[i].bg, Num[i].ed)) {printf ("%d%d\n", num[i].bg, Num[i].ed); }   } }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2485 Highways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.