UESTC-888-Absurdistan Roads(kruskal+floyd)
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 jto 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
思路:先用kruskal求出n-1条边。那么n-1条边必然是满足的,接下来仅仅须要再找一条边就能够了,直接按权值从小到大枚举全部边直到找到一条边的距离与前面n-1条边构成的图里面该条边的距离不相等就可以,假设没找到就随便输出前n-1条边中的随意一条。
#include <stdio.h>
#include <algorithm>
#define INF 999999999
using namespace std; struct E{
int u,v,val;
bool operator<(const E &p) const
{
return val<p.val;
}
}e[4000005]; int node[2005],dis[2005][2005]; int findroot(int x)
{
if(node[x]!=x) return node[x]=findroot(node[x]); return node[x];
} int main()
{
int n,i,j,k,t,u,v,val,cnt,roota,rootb;
bool first=1; while(~scanf("%d",&n))
{
if(first) first=0;
else puts(""); cnt=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++)
{
scanf("%d",&val); if(i<=j) continue; e[cnt].u=i;
e[cnt].v=j;
e[cnt++].val=val;
} sort(e,e+cnt); for(i=1;i<=n;i++) node[i]=i;
for(i=1;i<=n;i++) for(j=1;j<=n;j++) dis[i][j]=INF; t=0; for(i=0;i<cnt;i++)
{
roota=findroot(e[i].u);
rootb=findroot(e[i].v); if(roota!=rootb)
{
node[roota]=rootb; dis[e[i].u][e[i].v]=dis[e[i].v][e[i].u]=e[i].val; u=e[i].u,v=e[i].v,val=e[i].val; printf("%d %d %d\n",e[i].u,e[i].v,e[i].val); t++; if(t>=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<cnt;i++)
{
if(e[i].val!=dis[e[i].u][e[i].v])
{
printf("%d %d %d\n",e[i].u,e[i].v,e[i].val); break;
}
} if(i==cnt) printf("%d %d %d\n",u,v,val);
}
}
UESTC-888-Absurdistan Roads(kruskal+floyd)的更多相关文章
- CDOJ 888 Absurdistan Roads
Absurdistan Roads Time Limit: 5678/3456MS (Java/Others) Memory Limit: 65432/65432KB (Java/Others ...
- UESTC 30 最短路,floyd,水
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Statu ...
- HDU 1102 Constructing Roads(kruskal)
Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...
- POJ1251 Jungle Roads(Kruskal)(并查集)
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23882 Accepted: 11193 De ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- HDU1301 Jungle Roads(Kruskal)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- POJ 2421 Constructing Roads(Kruskal算法)
题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostre ...
- POJ1251 Jungle Roads Kruskal+scanf输入小技巧
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- hdu 1102 Constructing Roads(kruskal || prim)
求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...
随机推荐
- PPT去掉图片白色背景
双击图片,点击菜单栏“删除背景”,用矩形框选中想要的区域,然后将鼠标焦点移到图片外,单击鼠标即可.
- 使用<base target="_self" /> IE6 cann't open the Internet site 已终止操作
今日发现一个问题,我的网页需要用到<base target="_self" />,经测试IE8,9 谷歌 火狐全都正常,但是IE6 showModalDialog ...
- (转) unity 在移动平台中,文件操作路径详解
http://www.unitymanual.com/thread-23491-1-1.html 今天,这篇文章其实是个老生常谈的问题咯,在网上类似的文章也比比皆是,在此我只是做个详细总结方便大家能够 ...
- 内外连接、组函数、DDL、DML和TCL
前言 cross join ,是笛卡尔积:nature join 是自然连接. 正文 内外连接 inner join inner join 的inner能够省略. 内连接 在一个表中可以找到在还有一个 ...
- Android 打造自己的个性化应用(二):应用程序内置资源实现换肤功能
通过应用程序内置资源实现换肤,典型的应用为QQ空间中换肤的实现. 应用场景为: 应用一般不大,且页面较少,风格相对简单,一般只用实现部分资源或者只用实现背景的更换. 此种换肤方式实现的思路: 1. 把 ...
- plsql连接oracal数据库
一般只是作为客户端访问Oracle,并不需要安装庞大的Oracle 免安装客户端使用plsql连接oracal数据库 在本机没有安装oracle和oracle客户端的情况下,可以安装oracle提供的 ...
- C# 文件操作(一)
1.说明: 写入文件内容,如果文件中有内容,则进行追加,目录是程序集下的目录 public static void WriteLog(string value) { try { //目录是程序集下的D ...
- 使用Fiddler捕获Java程序中的HTTP请求
默认Java程序是不支持Fiddler获取请求的,因此需要通过设置代理来实现. Fiddler的端口是8888,若Fiddler没有运行,则会抛出异常. HttpClient4.5示例: ...
- VS 2003 无法打开Web项目,位于服务器“http:10.45.4.70:8080”上的项目不存在
解决方法: 用记事本打开*.sln文件更改第2行 改成正确的虚拟目录 出现这种情况往往是从一台机器搬到另一台机器造成的虚拟路径名字不同
- Eclipse error:Access restriction
报错:Access restriction: The method decodeBuffer(String) from the type CharacterDecoder is not accessi ...