分层图dij求调
查看原帖
分层图dij求调
405146
Ruan_ji楼主2023/12/16 16:54

样例输出:0

大抵是图建错了,球球了帮忙看看

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>

#define MAXN 100005
#define inf 1000000005

using namespace std;

struct str{
	long long a, b;
	bool operator<(const str e)const {
		return b > e.b;
	}
};

vector<str> g[MAXN];
priority_queue<str, vector<str>, less<str> > q;
long long dis[MAXN];
long long n, m, k, s, t;

void dij() {
	q.push({s, 0});
	for (int i = 1; i <= n; ++i) {
		dis[i] = inf;
	}
	dis[s] = 0;
	while (!q.empty()) {
		str cnt = q.top();
		long long u = cnt.a;
		long long v = cnt.b;
		q.pop();
		
		if (v > dis[u]) {
			continue;
		}
		for (int i = 0; i < g[u].size(); ++i) {
			long long c1 = g[u][i].a;
			long long c2 = g[u][i].b;
			if (c2 + dis[u] < dis[c1]) {
				dis[c1] = c2 + dis[u];
				q.push({c1, dis[c1]});
			}
		}
	}
}

int main() {
	cin >> n >> m >> k;
	cin >> s >> t;
	
	for (int i = 1; i <= m; ++i) {
		long long u, v, w;
		cin >> u >> v >> w;
		for (int j = 0; j <= k; ++j) {
			g[u + j * n].push_back({v + j * n, w});
			g[v + j * n].push_back({u + j * n, w});
			if (j != k) {
				g[u + j * n].push_back({v + (j + 1) * n, 0});
				g[v + j * n].push_back({u + (j + 1) * n, 0}); 
			}
		}
	}
	dij();
	
	long long ans = inf;
	for (long long i = 0; i <= k; ++i) {
		ans = min(ans, dis[t + i * n]);
	}
	
	cout << ans << endl;
	return 0;
}
2023/12/16 16:54
加载中...