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: ...
随机推荐
- Faas 典型场景——应用负载有显著的波峰波谷,典型用例-基于事件的数据处理
Serverless适用的两大场景 场景一:应用负载有显著的波峰波谷 Serverless化与否的评判标准并不是公司规模的大小,而是其业务背后的具体技术问题,比如业务波峰波谷明显,如何实现削峰填谷.一 ...
- P2657 [SCOI2009]windy数 数位dp
数位dp之前完全没接触过,所以NOIP之前搞一下.数位dp就是一种dp,emm……用来求解区间[L,R]内满足某个性质的数的个数,且这个性质与数的大小无关. 在这道题中,dp[i][j]代表考虑了i位 ...
- 杂项-JSP-Runoob:JSP 标准标签库(JSTL)
ylbtech-杂项-JSP-Runoob:JSP 标准标签库(JSTL) 1.返回顶部 1. JSP 标准标签库(JSTL) JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通 ...
- C++ 指针 部分
基本知识:在内存中的每个字节都有一个编号,这就是“地址”,相当于旅馆中的房间号.记住,内存单元的地址和内存单元的内容是两个不同的概念. 程序在编译之后,就已经将变量名转换成变量地址,对变量值的存取都是 ...
- Akka源码分析-Remote-收消息
上一遍博客中,我们分析了网络链接建立的过程,一旦建立就可以正常的收发消息了.发送消息的细节不再分析,因为对于本地的actor来说这个过程相对简单,它只是创立链接然后给指定的netty网路服务发送消息就 ...
- sql case when 用法
sql语言中有没有类似C语言中的switch case的语句?? 没有,用case when 来代替就行了. 例如,下面的语句显示中文年月 select ...
- CF 351A - Jeff and Rounding DP
http://codeforces.com/problemset/problem/351/C 题意:有2*n个浮点数a1,a2,a3...a2*n,把他们分成n队,对于每对<A,B>,对A ...
- 343 Integer Break 整数拆分
给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积.例如,给定 n = 2,返回1(2 = 1 + 1):给定 n = 10,返回36(10 = 3 ...
- 在 kafka 的 broke 和 client 之间加入 SSL 双向认证
在 kafka 的 broke 和 client 之间加入 SSL 双向认证https://blog.csdn.net/hohoo1990/article/details/79110031 kafka ...
- Microsoft SQL Server 2008/2012 Internals 一处疑问
Kalen Delaney 等著的深入解析 Microsoft SQL Server 系列,享有盛誉,深入研读,是管窥深奥复杂之 SQL Server 的阶梯与门径.手头有 Microsoft SQL ...