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: ...
随机推荐
- 【Codevs3567】宫廷守卫
Position: http://codevs.cn/problem/3567/ Description 从前有一个王国,这个王国的城堡是一个矩形,被分为M×N个方格.一些方格是墙,而另一些是空地.这 ...
- 动态DNS——本质上是IP变化,将任意变换的IP地址绑定给一个固定的二级域名。不管这个线路的IP地址怎样变化,因特网用户还是可以使用这个固定的域名 这样看的话,p2p可以用哇
动态域名是因应网络远程访问的需要而产生的一项应用技术.因为没有固定IP,只能运用二级域名来应对经常变化的IP,动态域名的由来因此而产生. 它当前主要应用在:路由器.网络摄像机.带网络监控的硬盘录像机. ...
- 网页分享URL
<a href="tencent://Message/?Uin=QQ号码&Site=qq联系">点击联系</a> 1.新浪微博:<a href ...
- E20170930-hm
parse vt. 从语法上描述或分析(词句等);
- [Swift通天遁地]四、网络和线程-(2)通过BlockOperation实现线程的队列
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- SpringBoot2.0 浅谈注解@ControllerAdvice的作用
我们都知道做项目一般都会有全局异常统一处理的类,那么这个类在Spring中可以用@ControllerAdvice来实现,费话不多说,先看代码: import org.springframework. ...
- Github提交本地版本是遇到的问题
问题如下:*** Please tell me who you are. Run git config --global user.email "you@example.com" ...
- Active Learning主动学习
Active Learning主动学习 我们使用一些传统的监督学习方法做分类的时候,往往是训练样本规模越大,分类的效果就越好.但是在现实生活的很多场景中,标记样本的获取是比较困难的,这需要领域内的专家 ...
- PAT甲级1016Phone Bills
#include<iostream> #include<cstdio> #include<cstdlib> #include<vector> #incl ...
- JS高级——封装注册事件
兼容性问题 1.ele.on事件类型 = function(){}一个元素ele注册一种事件多次,会被替换成最后一个,所以有局限性 2.addEventListener(事件类型,事件处理函数,use ...