其他分享
首页 > 其他分享> > 南京网络预选赛 The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail 多源最短路

南京网络预选赛 The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail 多源最短路

作者:互联网

 链接

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 305;
ll g[MAX_N][MAX_N];  // 算法中的 G 矩阵
int n,m; 
void init() {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (i == j) {
					g[i][j] = 0;
				} else {
					g[i][j] = 1e9+10;
				}
		}
	}
}
void insert(int u, int v, ll w) {
	g[u][v] = w;
}
void floyd() {
		for (int k = 0; k < n; ++k) {
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j < n; ++j) {
					if (g[i][k] + g[k][j] < g[i][j]) {
						g[i][j] = g[i][k] + g[k][j];
					}
				}
			}
		}
	}
int main() {
	int _,u,v;
	ll w;
	for(scanf("%d",&_);_;_--){	
		scanf("%d %d",&n,&m);
		init();
		for(int i=1;i<=m;i++){
			scanf("%d %d %lld",&u,&v,&w);
			//insert(v,u,-w);
			insert(u,v,w); 
		}
		floyd();
		for(int l=1;l<=6;l++){
			scanf("%d %d",&u,&v);
			printf("%lld\n",-g[v][u]);
			insert(u,v,-g[v][u]);
			floyd();
		}
	}
	return 0;
}

 

标签:Holy,Contest,int,MAX,ll,Asia,++,long,void
来源: https://blog.csdn.net/qq_41603898/article/details/100186965