Floyd 求最小环问题

板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子板子

#include
using namespace std;
#define ll long long
#define mem(a,b) memset(a,b,sizeof a)
#define cinios (ios::sync_with_stdio(false),cin.tie(0),cout.tie(0))
#define all(a) a.begin(),a.end()

const int N = 1e2 + 10, M = 1e6 + 10, mod = 1e9 + 7;
int n, m, ki;

int w[N][N], f[N][N];
int path[N], len, ans = 1e9;
int mid[N][N];

void get_path(int l, int r) {
	int mi = mid[l][r];
	if (!mi)return;
	get_path(l, mi);
	path[++len] = mi;
	get_path(mi, r);
}

void floyd() {
	for (int k = 1; k <= n; k++) {

		for(int i = 1; i < k; i++)
			for (int j = i + 1; j < k; j++) {
				if (ans > (ll)f[i][j] + w[i][k] + w[k][j]) {
					ans = f[i][j] + w[i][k] + w[k][j];
					len = 0;
					path[++len] = k;
					path[++len] = i;
					get_path(i, j);
					path[++len] = j;
				}
			}

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++) {
				if (f[i][k] + f[k][j] < f[i][j]) {
					f[i][j] = f[i][k] + f[k][j];
					mid[i][j] = k;
				}
			}
	}
}

int main() {
	cinios;

	cin >> n >> m;
	mem(w, 0x3f);
	for (int i = 1; i <= n; i++)w[i][i] = 0;

	for (int i = 1; i <= m; i++) {
		int a, b, x;
		cin >> a >> b >> x;
		w[a][b] = w[b][a] = min(w[a][b], x);
	}

	memcpy(f, w, sizeof w);
	floyd();

	if(ans > 1e9 /2)cout << "No solution.";
	else {
		for (int i = 1; i <= len; i++)
			cout << path[i] << " ";
	}

	return 0;
}

你可能感兴趣的:(图论,知识点笔记,c++,算法,图论)