Floyd模板

#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1005;
const int INF = (1 << 30 ) - 1;

int g[N][N];
int n,m,k;
int dis[N][N];

int main(){
	cin >> n >> m;
	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= n; j++){
			g[i][j] = INF;
		}
	}
	for(int i = 0; i < m; i++){
		int u,v,c; cin >> u >> v >> c;
		g[u][v] = c;
	}
	for(int k = 1; k <= n; k++){
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
			}
		}
	}
	cout << g[1][2] << " "<< g[4][1] << " " << g[1][3] << " " << g[2][3];
	return 0;
}

/*
5 10
2 3 5
1 5 5
3 5 6
1 2 8
1 3 8
5 3 4
4 1 8
4 5 3
3 5 6
5 4 2
*/

你可能感兴趣的:(Floyd模板)