模板【Floyd】

版权声明:蒟蒻写博客不宜,请随文附上原文链接 https://blog.csdn.net/qq_34493840/article/details/84670921

Floyd详解

记录
代码

#include <bits/stdc++.h>//Floyd

using namespace std;

#define MAXX 10100

int n, m, s;
int f, g, w;
int a[MAXX][MAXX];
const int inf = 123456789;

int main() {
	scanf("%d %d %d", &n, &m, &s);
	for(int i = 1; i <= n; ++i) {
		for(int j = 1; j <= n; ++j) {
			if(i == j) a[i][j] = 0;
			else if(i != j) a[i][j] = inf;
		}
	}
	for(int i = 1; i <= m; ++i) {
		scanf("%d %d %d", &f, &g, &w);
		if(a[f][g] > w) a[f][g] = w;
	}
	for(int k = 1; k <= n; ++k) {
		for(int i = 1; i <= n; ++i) {
			if(i == k || a[i][k] == inf) continue;
			else {
				for(int j = 1; j <= n; ++j) {
					if(a[i][k] + a[k][j] < a[i][j]) a[i][j] = a[i][k] + a[k][j];
				}
			}
		}
	}
	for(int i = 1; i <= n; ++i) {
		printf("%d ", a[s][i]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34493840/article/details/84670921