Floyd

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf=99999999;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int e[20][20],i,j,k,n,m;
	while(cin>>n>>m){
		for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		if(i==j) e[i][j]=0;
		else e[i][j]=inf;
		int t1,t2,d;
		for(i=1;i<=m;i++)
		{
			cin>>t1>>t2>>d;
			e[t1][t2]=d;
		}
		//Floyd_Warshall算法
		for(k=1;k<=n;k++){
			for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			if(e[i][j]>e[i][k]+e[k][j])
			e[i][j]=e[i][k]+e[k][j];
		} 
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			cout<<e[i][j]<<"  ";
			cout<<endl;
		}
	}
	return 0;
}
posted @ 2018-12-06 18:15  ChunhaoMo  阅读(110)  评论(0编辑  收藏  举报