2013
11-10

# 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.

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 ith 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.

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.

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

1 6
3 7
4 9
5 7
8 3


//* @author: 82638882@163.com
import java.io.*;
public class Main
{
public static void main(String[] args) throws NumberFormatException, IOException
{
String[] ss;
int[] ax=new int[b];
int[] ay=new int[b];
int[][] p=new int[b][b];
for(int i=0;i< b;i++)
{
ax[i]=Integer.parseInt(ss[0]);
ay[i]=Integer.parseInt(ss[1]);
}
for(int i=0;i< b;i++)
{
for(int j=i+1;j< b;j++)
{
int juli=(ax[i]-ax[j])*(ax[i]-ax[j])+(ay[i]-ay[j])*(ay[i]-ay[j]);
p[i][j]=p[j][i]=juli;
}
}
for(int i=0;i< a;i++)
{
int u1=Integer.parseInt(ss[0])-1;
int u2=Integer.parseInt(ss[1])-1;
p[u1][u2]=p[u2][u1]=0;
}
int[] low=new int[b];
low[0]=-1;
int[] near=new int[b];
for(int i=1;i< b;i++)
low[i]=p[0][i];
for(int i=1;i< b;i++)
{
int min=9999999,tag=0;
for(int j=0;j< b;j++)
{
if(low[j]!=-1&&low[j]< min)
{
min=low[j];
tag=j;
}
}
if(min!=0) System.out.println((near[tag]+1)+" "+(tag+1));
low[tag]=-1;
for(int j=0;j< b;j++)
{
if(p[tag][j]< low[j])
{
low[j]=p[tag][j];
near[j]=tag;
}
}
}
}
}

1. 5.1处，反了；“上一个操作符的优先级比操作符ch的优先级大，或栈是空的就入栈。”如代码所述，应为“上一个操作符的优先级比操作符ch的优先级小，或栈是空的就入栈。”

2. 思路二可以用一个长度为k的队列来实现，入队后判断下队尾元素的next指针是否为空，若为空，则出队指针即为所求。

3. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
O(n) = O(n-1) + O(n-2) + ….
O(n-1) = O(n-2) + O(n-3)+ …
O(n) – O(n-1) = O(n-1)
O(n) = 2O(n-1)

4. 有限自动机在ACM中是必须掌握的算法，实际上在面试当中几乎不可能让你单独的去实现这个算法，如果有题目要用到有限自动机来降低时间复杂度，那么这种面试题应该属于很难的级别了。

5. 嗯 分析得很到位，确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样：push时，比较要push的elem和辅助栈的栈顶，elem<=min.top()，则min.push(elem).否则只要push（elem）就好。在pop的时候，比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();}，否则{stack.pop();}.