E 求条,AC 5
查看原帖
E 求条,AC 5
902351
Little_x_starTYJ楼主2025/7/26 21:42
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
int dist[510][510];
set<int> s;
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i != j) {
				dist[i][j] = inf;
			}
		}
	}
	for (int i = 1; i <= m; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		if (c < dist[a][b]) {
			dist[a][b] = c;
			dist[b][a] = c;
		}
	}
	int k, timee;
	cin >> k >> timee;
	for (int i = 1; i <= k; i++) {
		int d;
		cin >> d;
		s.insert(d);
	}
	vector<int> v(s.begin(), s.end());
	for (int i = 0; i < v.size(); i++) {
		for (int j = i + 1; j < v.size(); j++) {
			int u = v[i];
			int V = v[j];
			if (timee < dist[u][V]) {
				dist[u][V] = timee;
				dist[V][u] = timee;
			}
		}
	}
	
	for (int kk = 1; kk <= n; kk++) {
		for (int i = 1; i <= n; i++) {
			if (dist[i][kk] == inf) continue;
			for (int j = 1; j <= n; j++) {
				if (dist[kk][j] != inf && dist[i][j] > dist[i][kk] + dist[kk][j]) {
					dist[i][j] = dist[i][kk] + dist[kk][j];
				}
			}
		}
	}
	
	int q;
	cin >> q;
	while (q--) {
		int op;
		cin >> op;
		if (op == 1) {
			int x, y, t;
			cin >> x >> y >> t;
			if (t < dist[x][y]) {
				dist[x][y] = t;
				dist[y][x] = t;
			}
			for (int kk : {x, y}) {
				for (int i = 1; i <= n; i++) {
					if (dist[i][kk] == inf) continue;
					for (int j = 1; j <= n; j++) {
						if (dist[kk][j] != inf && dist[i][j] > dist[i][kk] + dist[kk][j]) {
							dist[i][j] = dist[i][kk] + dist[kk][j];
						}
					}
				}
			}
			for (int kk : {x, y}) {
				for (int i = 1; i <= n; i++) {
					if (dist[kk][i] == inf) {
						continue;
					}
					for (int j = 1; j <= n; j++) {
						if (dist[i][j] != inf && dist[kk][j] > dist[kk][i] + dist[i][j]) {
							dist[kk][j] = dist[kk][i] + dist[i][j];
						}
					}
				}
			}
			for (int kk : {x, y}) {
				for (int i = 1; i <= n; i++) {
					for (int j = 1; j <= n; j++) {
						if (dist[i][j] != inf && dist[kk][j] != inf && dist[i][kk] > dist[i][j] + dist[kk][j]) {
							dist[i][kk] = dist[i][j] + dist[kk][j];
						}
					}
				}
			}
		} else if (op == 2) {
			int x;
			cin >> x;
			if (s.find(x) != s.end()) {
				continue;
			}
			s.insert(x);
			for (int y : s) {
				if (x == y) continue;
				if (timee < dist[x][y]) {
					dist[x][y] = timee;
					dist[y][x] = timee;
				}
			}
			for (int i = 1; i <= n; i++) {
				if (dist[i][x] == inf) continue;
				for (int j = 1; j <= n; j++) {
					if (dist[x][j] != inf && dist[i][j] > dist[i][x] + dist[x][j]) {
						dist[i][j] = dist[i][x] + dist[x][j];
					}
				}
			}
			for (int i = 1; i <= n; i++) {
				if (dist[x][i] == inf) continue;
				for (int j = 1; j <= n; j++) {
					if (dist[i][j] != inf && dist[x][j] > dist[x][i] + dist[i][j]) {
						dist[x][j] = dist[x][i] + dist[i][j];
					}
				}
			}
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					if (dist[i][j] != inf && dist[j][x] != inf && dist[i][x] > dist[i][j] + dist[j][x]) {
						dist[i][x] = dist[i][j] + dist[j][x];
					}
				}
			}
		} else if (op == 3) {
			int ans = 0;
			for (int i = 1; i <= n; i++) {
				for (int j = 1; j <= n; j++) {
					if (dist[i][j] < inf) {
						ans += dist[i][j];
					}
				}
			}
			cout << ans << '\n';
		}
	}
	return 0;
}
2025/7/26 21:42
加载中...