萌新求助
查看原帖
萌新求助
942910
wcr_jason楼主2024/10/2 16:17

为什么第 22 个点一直 WA\red{WA}

代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct qwq{
	int x, cost;
	bool operator < (const qwq &b) const {return (cost > b.cost);}
};
int n, m, x[5005], y[5005], z[5005];
int ans = 1e15, vis[5005], dis[5005];
vector<int> v[5005], c[5005], id[5005];
int dijk(int idx){
	int s = x[idx], t = y[idx];
	priority_queue<qwq> q; q.push({s, 0});
	memset(dis, 0x3f, sizeof(dis));
	memset(vis, 0, sizeof(vis));
	dis[s] = 0;
	while (!q.empty()){
		int x = q.top().x; q.pop();
		if (vis[x]) continue;
		vis[x] = 1;
		for (int i = 0;i < v[x].size();i++){
			if (id[x][i] == idx) continue;
			int nx = v[x][i], cost = c[x][i];
			if (dis[nx] > dis[x] + cost){
				dis[nx] = dis[x] + cost;
				q.push({nx, dis[nx]});
			}
		}
	} return (dis[t] == dis[0] ? -1ll : dis[t]);
} signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m;
	for (int i = 1;i <= m;i++){
		cin >> x[i] >> y[i] >> z[i];
		v[x[i]].push_back(y[i]); c[x[i]].push_back(z[i]); id[x[i]].push_back(i);
		v[y[i]].push_back(x[i]); c[y[i]].push_back(z[i]); id[y[i]].push_back(i);
	} for (int i = 1;i <= m;i++){
		int dist = dijk(i);
		if (dist != -1ll) ans = min(ans, dist + z[i]);
	}
	if(ans == 1e15) cout << "No solution.";
	else cout << ans;
//	cout << ((ans == 1e13) ? "No solution." : to_string(ans));
	return 0;
}
2024/10/2 16:17
加载中...