CDOJ 888 Absurdistan Roads
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 |
2 1 1 |
Source
#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的更多相关文章
- UESTC-888-Absurdistan Roads(kruskal+floyd)
The people of Absurdistan discovered how to build roads only last year. After the discovery, every c ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- POJ1947 Rebuilding Roads[树形背包]
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11495 Accepted: 5276 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- cdoj 1489 老司机采花
地址:http://acm.uestc.edu.cn/#/problem/show/1489 题目: 老司机采花 Time Limit: 3000/1000MS (Java/Others) M ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- POJ 1947 Rebuilding Roads
树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...
随机推荐
- 【POJ 1845】 Sumdiv
[题目链接] 点击打开链接 [算法] 不妨先将A分解质因数 A = p1^q1p2^p2p3^p3..pn^qn 那么,A^B = p1^q1Bp2^q2B...pn^qnB 根据约数和定理,A^B的 ...
- 【POJ 1328】 Radar Installation
[题目链接] http://poj.org/problem?id=1328 [算法] 每个雷达都位于笛卡尔坐标系的x轴上,因此,对于每个岛屿,我们都可以用勾股定理算出它的有效管辖区域 那么,问题就被转 ...
- P3694 邦邦的大合唱站队/签到题(状压dp)
P3694 邦邦的大合唱站队/签到题 题目背景 BanG Dream!里的所有偶像乐队要一起大合唱,不过在排队上出了一些问题. 题目描述 N个偶像排成一列,他们来自M个不同的乐队.每个团队至少有一个偶 ...
- keystone身份认证服务
Keystone介绍 keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户权限的定义等等.云 ...
- 判断IOS静态库(.a文件)是否支持模拟器和真机运行
判断IOS静态库(.a文件)是否支持模拟器和真机运行 在mac终端下,进入到.a文件目录下,然后输入: lipo -info libMyAlertView.a Architectures in the ...
- Java学习-异常2
1.异常处理的第一种方式是:上抛[throws] 2.异常处理的第二种方式是:try....catch..如果不想让调用程序知道该异常发生了,被调用的程序应该使用try...catch..进行异常捕捉 ...
- HDU 1847 博弈
sg[0]=0; sg[i]=mex{sg[i-2^(j)]} (i>=2^j) mex()为不在此集合的最小非负整数 #include <stdio.h> #include &l ...
- Servlet到Servlet的请求转发与重定向的区别
Servlet跳转到另一个Servlet中: request.getRequestDispatcher().forward();代表默认的是post方法,即在被跳转的Servlet的doPost()方 ...
- jvm gc日志解读
参考 https://blog.csdn.net/yxc135/article/details/12137663 认识gc日志每个位置的含义 java 8 full gc [Full GC (Meta ...
- C#入门经典 Chapter4 流程控制
4.1布尔逻辑 布尔比较运算符 == != < > <= >= 处理布尔值的布尔值运算符 ! & | ^(异或) 条件布尔运算符 &&am ...