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 ;
}

最新文章

  1. highchart 中数据千分位显示为空格而不是逗号的解决方案
  2. Autofac 及 Autofac.WebApi 与MVC版本兼容问题
  3. C#读取Excel的三种方式以及比较
  4. CRM HomePage.aspx
  5. T60上安装Gentoo笔记
  6. C#下搭建文件格式转换服务器
  7. C自学笔记-递归与迭代的使用方法笔记与两者的使用场合
  8. Hard problem
  9. 更新插件时提示“正在更新缓存”“正在等待jockey-backend退出”
  10. Ueditor 1.4.3 jsp utf-8版图片上传问题
  11. 浙大pat1013题解
  12. HDU 3874 Necklace
  13. 虚拟机下安装ubuntu系统
  14. zuul1.3源码扒一扒(1)
  15. 三层+EasyUI+Ajax 提交Form表单
  16. symmfony
  17. 【Python全栈-HTML】HTML如何做出分割线效果
  18. 学习笔记37—WIN7系统本地连接没有有效的IP地址 电脑本地连接无有效ip配置怎么办
  19. Java NIO系列教程(五) 通道之间的数据传输
  20. iOS进阶学习笔记

热门文章

  1. nodejs Yarn替代npm的包管理——快速、安全、可靠性高的依赖管理
  2. [BZOJ 3126] Photo
  3. 浅谈IO优化
  4. PCB genesis加尾孔实现方法
  5. RHEL6.5 设置yum,IP地址,解压缩
  6. $P2126 Mzc家中的男家丁$
  7. Flask小总结+实例化Flask参数以及对app的配置
  8. 【USACO2009 Open】滑雪课程ski
  9. 【IOS网络编程】socket编程 - Asyncsocket
  10. 【转】Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)