Floyd模板

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int N = 20;
int G[N][N], nxt[N][N], n;

void Floyd()
{
	for(int k = 0; k < n; k++) {//考虑增加结点k
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) if(k != i&&k != j) {
				if(G[i][j] > G[i][k] + G[k][j]) {//因k的增加所造成的可能的松弛
					G[i][j] = G[i][k] + G[k][j];
					nxt[i][j] = nxt[i][k];
				}
			}
		}
	}
}

int main()
{
#ifdef J_Sure
	freopen("000.in", "r", stdin);
//  freopen(".out", "w", stdout);
#endif
	scanf("%d", &n);
	int u, v, w;
	memset(G, 0x3f, sizeof(G));
	while(~scanf("%d%d%d", &u, &v, &w)) {
		if(u == -1) break;
		G[u][v] = w;
		nxt[u][v] = v;
	}
	Floyd();
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++) if(i != j) {
			printf("%d=>%d\t%d\t%d", i, j, G[i][j], i);
			int x = i;
			while(x != j) {
				printf("->%d", nxt[x][j]);
				x = nxt[x][j];
			}
			puts("");
		}
	}
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值