博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UESTC-888-Absurdistan Roads(kruskal+floyd)
阅读量:5093 次
发布时间:2019-06-13

本文共 3004 字,大约阅读时间需要 10 分钟。

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 (2N2000) -- 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 jto i.

Output

For each test case:

  • Print N lines with three integers 'a b c' denoting that there is a road between cities 1aN and 1bN of length 1c1000000, where ab. 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
40 1 2 11 0 2 12 2 0 11 1 1 040 1 1 11 0 2 21 2 0 21 2 2 030 4 14 0 31 3 0
2 1 14 1 14 2 14 3 12 1 13 1 14 1 12 1 13 1 12 1 43 2 3

Source

Northwestern European Regional Contest 2013
思路:先用kruskal求出n-1条边。那么n-1条边必然是满足的,接下来仅仅须要再找一条边就能够了,直接按权值从小到大枚举全部边直到找到一条边的距离与前面n-1条边构成的图里面该条边的距离不相等就可以,假设没找到就随便输出前n-1条边中的随意一条。

#include 
#include
#define INF 999999999using namespace std;struct E{int u,v,val;bool operator<(const E &p) const{ return val
=n-1) break; } } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(dis[i][k]==INF) break;//没有这个优化直接T了。。。 dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } } } for(i=0;i

转载于:https://www.cnblogs.com/blfshiye/p/5172839.html

你可能感兴趣的文章
快速幂
查看>>
改善C#公共程序类库质量的10种方法
查看>>
AIO 开始不定时的抛异常: java.io.IOException: 指定的网络名不再可用
查看>>
MyBaits动态sql语句
查看>>
[苦逼程序员的成长之路]1、飞扬小鸟
查看>>
零基础自学用Python 3开发网络爬虫(二): 用到的数据结构简介以及爬虫Ver1.0 alpha...
查看>>
修改JEECG项目浏览器标题
查看>>
HDU4405(期望DP)
查看>>
Linux下svn的部署
查看>>
拉格朗日乘子法 那些年学过的高数
查看>>
Linux下MySQL数据库的备份与还原
查看>>
单例模式(Java)
查看>>
centos7安装libgdiplus。netcore生成验证码,处理图片
查看>>
inline-block,inline,block,table-cell,float
查看>>
和我老婆去旅游
查看>>
JQuery之基本操作
查看>>
图片没有.png或者jpg后缀能不能加载?
查看>>
OC 类的load方法
查看>>
怎么将后缀为.opt,.frm,.myd,.myi文件还原或者是导入mySQL中
查看>>
初识dokuwiki
查看>>