南京网络预选赛 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;
}

 

上一篇:H.Holy Grail ( floyd )(The Preliminary Contest for ICPC Asia Nanjing 2019)


下一篇:福利 | Flink Forward Asia 2019 由你决定,填问卷,送周边!