首页 技术 正文
技术 2022年11月15日
0 收藏 459 点赞 4,465 浏览 847 个字

http://oj.changjun.com.cn/problem/detail/pid/1365

Description

给出N个点,M条无向边的简单图,问所有点对之间的最短路。

Input

第1行两个正整数N,M(N<=100,M<=5000)

下面M行,每行3个正整数x, y, w,为一条连接顶点x与y的边权值为w。(x<=n,y<=n,w<=1000)

Output

包括N行,每行N个数,第i行第j个数为点i到点j的最短路,第i行第i个数应为0,数字之间空格隔开。

Sample Input

5 10

3 2 1

2 4 7

5 3 4

4 1 2

5 1 8

3 4 10

5 4 9

2 5 2

1 2 1

3 1 10

Sample Output

0 1 2 2 3

1 0 1 3 2

2 1 0 4 3

2 3 4 0 5

3 2 3 5 0

题解

让我们来温习一下弗洛伊德Floyd吧!标准裸题。不解释了。直接上代码

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
void Floyd()
{
int n,m;
int g[101][101];
cin>>n>>m;
int a,b,c;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
if(i==j)g[i][j]=0;
else
g[i][j]=99999999;
for(int i=1;i<=m;++i)
{
cin>>a>>b>>c;
g[a][b]=g[b][a]=c;
}
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
if(g[i][j]>g[i][k]+g[k][j])g[i][j]=g[i][k]+g[k][j];
}
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
cout<<g[i][j]<<' ';cout<<endl;
}
}
int main()
{
Floyd();
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850