站外题求助
查看原帖
站外题求助
730195
Little_Cabbage楼主2024/6/11 09:25

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, ans[100010], f[100010], t[100010];
vector<pair<int, int> > g[100010];
queue<int> q;
int a, b, c;
int cnt, cnt2;
signed main() {
	cin >> n >> m >> a >> c >> b;
	while (m--) {
		int a, b, c;
		cin >> a >> b >> c;
		g[a].push_back(make_pair(b, c));
		g[b].push_back(make_pair(a, c));
	}
	memset(t, 0x3f, sizeof t);
	t[a] = 0, f[a] = 1, ans[a] = 1;
	q.push(a);
	while (q.size()) {
		int x = q.front();
		q.pop();
		for (int i = 0; i < g[x].size(); i++) {
			int y = g[x][i].first;
			if (t[y] > t[x] + g[x][i].second) {
				f[y] = 1;
				t[y] = t[x] + g[x][i].second;
				q.push(y);
				ans[y] = (ans[y] + ans[x]);
//				cout << x << " -> " << y << endl;
			} else if (t[y] == t[x] + g[x][i].second) {
				ans[y] += ans[x];
//				cout << x << " --> " << y << endl;
			}
		}
	}
//	cout << t[b] << endl;
//	for (int i = 1; i <= n; i++) cout << ans[i] << " ";
//	cout << endl;
	cnt = ans[b];
	memset(t, 0x3f, sizeof t);
	memset(f, 0, sizeof f);
	memset(ans, 0, sizeof ans);
	while (q.size()) q.pop();
	t[b] = 0, f[b] = 1, ans[b] = 1;
	q.push(b);
	while (q.size()) {
		int x = q.front();
		q.pop();
		for (int i = 0; i < g[x].size(); i++) {
			int y = g[x][i].first;
			if (t[y] > t[x] + g[x][i].second) {
				f[y] = 1;
				t[y] = t[x] + g[x][i].second;
				q.push(y);
				ans[y] = (ans[y] + ans[x]);
//				cout << x << " -> " << y << endl;
			} else if (t[y] == t[x] + g[x][i].second) {
				ans[y] += ans[x];
//				cout << x << " --> " << y << endl;
			}
		}
	}
//	cout << t[c] << endl;
//	for (int i = 1; i <= n; i++) cout << ans[i] << " ";
//	cout << endl;
	cnt2 = ans[c];
//	cout << cnt << " " << cnt2 << endl;
	cout << cnt * cnt2 << endl;
	return 0;
}

一直 WA 10pts

2024/6/11 09:25
加载中...