Absurdistan Roads

Time Limit: 5678/3456MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others)

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 1000000. 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 and output

Sample Input Sample Output
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
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

Source

Northwestern European Regional Contest 2013
 
解题:先最小生成树一遍,然后求Floyd。看看是否都满足输入的方阵,如不满足则加边。代码思路很简单。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int u,v,w;
arc(int x = ,int y = ,int z = ){
u = x;
v = y;
w = z;
}
};
int mp[maxn][maxn],tot,n,cnt;
arc e[];
int uf[maxn];
int Find(int x){
if(x != uf[x])
uf[x] = Find(uf[x]);
return uf[x];
}
bool cmp(const arc &x,const arc &y){
return x.w < y.w;
}
int kruskal(){
int i,j,k,u,v,w;
for(i = ; i <= n; i++) uf[i] = i;
sort(e,e+tot,cmp);
for(cnt = i = ; i < tot; i++){
int x = Find(e[i].u);
int y = Find(e[i].v);
if(x != y){
uf[x] = y;
u = e[i].u;
v = e[i].v;
w = e[i].w;
mp[e[i].u][e[i].v] = mp[e[i].v][e[i].u] = e[i].w;
cnt++;
printf("%d %d %d\n",e[i].u,e[i].v,e[i].w);
if(cnt >= n-) break;
}
}
for(k = ; k <= n; k++){
for(i = ; i <= n; i++){
for(j = ; j <= n; j++){
if(mp[i][k] == INF) break;
if(mp[i][j] > mp[i][k]+mp[k][j]) mp[i][j] = mp[i][k]+mp[k][j];
}
}
}
for(i = ; i < tot; i++){
if(e[i].w != mp[e[i].u][e[i].v]){
printf("%d %d %d\n",e[i].u,e[i].v,e[i].w);
break;
}
}
if(i == tot) printf("%d %d %d\n",u,v,w);
}
int main() {
int i,j,w;
bool flag = false;
while(~scanf("%d",&n)){
tot = ;
if(flag) puts("");
flag = true;
for(i = ; i <= n; i++){
for(j = ; j <= n; j++){
scanf("%d",&w);
mp[i][j] = INF;
if(j > i) e[tot++] = arc(i,j,w);
}
}
kruskal();
}
return ;
}

CDOJ 888 Absurdistan Roads的更多相关文章

  1. UESTC-888-Absurdistan Roads(kruskal+floyd)

    The people of Absurdistan discovered how to build roads only last year. After the discovery, every c ...

  2. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  3. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. POJ1947 Rebuilding Roads[树形背包]

    Rebuilding Roads Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11495   Accepted: 5276 ...

  5. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  6. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  7. cdoj 1489 老司机采花

    地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others)     M ...

  8. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  9. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

随机推荐

  1. telnet端口问题

    今天测试发现telnet 一个端口不通,开始还以为是服务开放这个端口有问题,后来才发现这个端口是udp的.而telnet下层走的tcp协议,自然无法测试那些tcp的端口. 而之前下意识里还总以为都可以 ...

  2. CF85 E Guard Towers——二分图

    题目:http://codeforces.com/contest/85/problem/E 给定一些点的坐标,求把它们分成两组,组内最大距离的最小值: 二分答案,判断就是看距离大于 mid 的点能否组 ...

  3. PCB 内层负片散热PAD Symbols尺寸更改方法

    如下图这是我们熟悉的内层负片散热PAD Symbols,我们CAM制作时,为了满足PCB工厂生产制作能力,,会优化散热PAD尺寸,让热PAD的尺寸符合制作规范要求,通常我们只关注散热PAD的3个指标即 ...

  4. 多物体运动框架案例一:多个Div的宽度运动变化

    多物体运动框架,鼠标移入Div,此Div逐渐变宽,鼠标移出后,此Div逐渐缩短恢复原长度. <!doctype html> <html> <head> <ti ...

  5. cookie和seesion区别

    cookie 和session 的区别详解 这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie ...

  6. 解决Oracle 本地可以连接,远程不能连接问题

    一.问题描述: 1.telnet 2.输入命令 lsnrctl status 远程访问不到的原因为:监听指向的是localhost,而不是你本机的远程访问ip地址,因此需要将监听的指向修改为你本机的远 ...

  7. 如何下载 Nginx (windows 版本)并且简单的使用

    官网地址:http://nginx.org/ 进到官网 我这里下载的是 稳定版的 windows版本. 开始我们的简单测试 步骤一:找到nginx的压缩包,(随意找个地方)解压 步骤二:进入conf文 ...

  8. 定制UVM Messages(参考)

    UVM的Messages机制有些时候很繁琐,很多时候希望能够在UVM messages的基础上做一些个人化的订制,这里给出来一个找到的例子作为参考. my_macros.sv:    `define ...

  9. JS——旋转木马

    1.opacity和zIndex的综合运用 2.样式的数组的替换:向右边滑动---删除样式数组第一位并在数组最后添加:向左边滑动---删除样式数组最后一位并在数组前添加 3.开闭原则,只有当回调函数执 ...

  10. Python语言之模块

    模块基本上就是一个包含了所有你定义的函数和变量的文件.它用处在于使你你能在别的程序中重用它提供的功能和服务. 1.模块的使用方法 模块的文件名必须以.py作为扩展名. 当我们需要使用某个模块时,我们需 ...