Skip to content

Kevin's Home

baylor 6622 Absurdistan Roads( NWERC Contest)

图论2 min read

原题pdf:click here

Description

The people of Absurdistan discovered how to build roads only last year. After the discovery, every city decided to build their own road connecting their city with another city. Each newly built road can be used in both directions.

Absurdistan is full of surprising coincidences. It took all N cities precisely one year to build their roads. And even more surprisingly, in the end it was possible to travel from every city to every other city using the newly built roads.

You bought a tourist guide which does not have a map of the country with the new roads. It only contains a huge table with the shortest distances between all pairs of cities using the newly built roads.

You would like to know between which pairs of cities there are roads and how long they are, because you want to reconstruct the map of the N newly built roads from the table of shortest distances.

You get a table of shortest distances between all pairs of cities in Absurdistan using the N roads built last year. From this table, you must reconstruct the road network of Absurdistan. There might be multiple road networks with N roads with that same table of shortest distances, but you are happy with any one of those networks.

Input

For each test case:

• A line containing an integer N (2 ≤ N ≤ 2000) — the number of cities and roads.

• N lines with N numbers each. The j-th number of the i-th line is the shortest distance from city i to city j.

• All distances between two distinct cities will be positive and at most 1 000 000. The distance from i to i will always be 0 and the distance from i to j will be the same as the distance from j to i.

Output

For each test case:

• Print N lines with three integers ‘a b c’ denoting that there is a road between cities 1 ≤ a ≤ N and 1 ≤ b ≤ N of length 1 ≤ c ≤ 1000000, where a ̸= b. If there are multiple solutions, you can print any one and you can print the roads in any order. At least one solution is guaranteed to exist.

• Print a blank line between every two test cases.

Sample Input

4
0 1 2 1
1 0 2 1
2 2 0 1
1 1 1 0
4
0 1 1 1
1 0 2 2
1 2 0 2
1 2 2 0
3
0 4 1
4 0 3
1 3 0

Sample Output

2 1 1
4 1 1
4 2 1
4 3 1
2 1 1
3 1 1
4 1 1
2 1 1
3 1 1
2 1 4
3 2 3


- - -

NWERC 题目还挺简单的。
题意就是一个N个顶点,N条边的无向图。告诉你两两点之间的最短路。让你重构这个图。(随意输出任意一张满足以上条件的图)
这题的突破口就是N条边。要想想如果只有N-1条边,那么这个图就是一个树了。所以,我们可以先构造最小生成树,再来考虑最后一条边。
可以先将构造好的树求一遍floyd,然后再跟题目的最短路相比较,如果两点u,v求出的最短路w`与题目给的最短路w不一致(可以断言:w`>w),那么就将w作为最后一条边的边长加在u,v之间即可。这样的边可能会找到很多,事实上只需要加上任意一条就使所有的w`都等于w了。 需要注意的是,可能整个树就已经满足题目了。这样的话,最后一条边只需要重复任意一条树边就好了。