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
0 1 2 1
1 0 2 1
2 2 0 1
1 1 1 0
4
0 1 1 1
1 0 2 2
1 2 0 2
1 2 2 0
3
0 4 1
4 0 3
1 3 0
2 1 1
4 1 1
4 2 1
4 3 1 2 1 1
3 1 1
4 1 1
2 1 1 3 1 1
2 1 4
3 2 3

Source

Northwestern European Regional Contest 2013



思路:先用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)的更多相关文章

  1. CDOJ 888 Absurdistan Roads

    Absurdistan Roads Time Limit: 5678/3456MS (Java/Others)     Memory Limit: 65432/65432KB (Java/Others ...

  2. UESTC 30 最短路,floyd,水

    最短路 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Statu ...

  3. HDU 1102 Constructing Roads(kruskal)

    Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...

  4. POJ1251 Jungle Roads(Kruskal)(并查集)

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23882   Accepted: 11193 De ...

  5. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  6. HDU1301 Jungle Roads(Kruskal)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. POJ 2421 Constructing Roads(Kruskal算法)

    题意:给出n个村庄之间的距离,再给出已经连通起来了的村庄.求把所有的村庄都连通要修路的长度的最小值. 思路:Kruskal算法 课本代码: //Kruskal算法 #include<iostre ...

  8. POJ1251 Jungle Roads Kruskal+scanf输入小技巧

    Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...

  9. hdu 1102 Constructing Roads(kruskal || prim)

    求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...

随机推荐

  1. android-sdk-windows版本号下载

    Android SDK 4.0.3 开发环境配置及执行 近期又装了一次最新版本号的ADK环境 眼下最新版是Android SDK 4.0.3 本文的插图和文本尽管是Android2.2的 步骤都是一样 ...

  2. 利用MetaWeblog API 自制博客发布小工具

    博客园提供了诸多数据接口, 利用这些接口可以很容易的实现博客的发布,修改,删除等 1.需要引用一个DLL:为CookComputing.XmlRpcV2 2.新建一个类,在其中是一些要实现的东西,如: ...

  3. WIN7系统JavaEE(java+tomcat7+Eclipse)环境配

    在进行 Java Web环境开发之前,首先要做的第一件事就是搭建开发环境,开发环境搭建成功,接下来便是对整个开发环境进行测试,可以通过编写一个简单的JSP 程序发布到Tomcat应用服务器上运行. 1 ...

  4. 如何在程序退出的时候清除activity栈

    在公司里接手了一个后期的项目,由于项目前期对activity栈管理的不够谨慎,所以导致了在某些情况下程序退出的时候没有将activity栈清除掉.在网上找到的无非就是那几种例子,都不是最好的解决办法. ...

  5. 写一个Windows上的守护进程(8)获取进程路径

    写一个Windows上的守护进程(8)获取进程路径 要想守护某个进程,就先得知道这个进程在不在.我们假设要守护的进程只会存在一个实例(这也是绝大部分情形). 我是遍历系统上的所有进程,然后判断他们的路 ...

  6. KMP与扩展KMP

    原文转自:http://www.cppblog.com/MatoNo1/archive/2011/04/17/144390.aspx KMP:给出两个字符串A(称为模板串)和B(称为子串),长度分别为 ...

  7. gcc 编译的4个过程简单识记

    直入正题,测试编译代码如下: lude <stdio.h> int main() { ,y,z; x*=(y=z=); printf("%d\n",x); z=; x= ...

  8. MySql存储引擎介绍

    MySQL5.5以后默认使用InnoDB存储引擎,其中InnoDB和BDB提供事务安全表,其它存储引擎都是非事务安全表.若要修改默认引擎,可以修改配置文件中的default-storage-engin ...

  9. FTP之主动模式vs被动模式

    背景说明 最近有个项目涉及到FTP的上传下载问题.在本地开发好的程序测试的时候能正常获取FTP内容,但一放到生产上却显示connection timeout,无法连接.经过一些研究,发现是防火墙造成的 ...

  10. UML中九种图的理解

    1.用例图. 用例图是用来描述用户需求的,从用户的角度来描述系统的功能,并指出各个执行者.强调谁在使用,系统的执行者是谁. 2.类图. 用来定义系统中的类,包括描述类的结构和类之间的关系.类图的主要作 ...