TLE82分求调
查看原帖
TLE82分求调
980839
sanbanguanjunze楼主2024/2/27 00:32
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 1e15;
const int N = 1500005;
int head[N], nxt[N], to[N], E;
LL w[N], d[N];

struct node {
	int u;
	LL dis;
	bool operator <(const node& cmp)const {
		return dis > cmp.dis;
	}
};
int n, m, k;
int s, t;
void add(int a, int b, int c) {
	to[++E] = b;
	w[E] = c;
	nxt[E] = head[a];
	head[a] = E;
}
void dijkstra() {
	memset(d, 0x3f3f, sizeof d);
	priority_queue<node> q;
	q.push({s, 0});
	d[s] = 0;
	while(!q.empty()) {
		node t = q.top(); q.pop();
		int u = t.u;
		if(t.dis > d[u]) continue;
		for(int i = head[u]; ~i; i = nxt[i]) {
			int v = to[i];
			if(d[u] + w[i] < d[v]) {
				d[v] = d[u] + w[i];
				q.push({v, d[v]});
			}
		}
	}
	LL ret = INF;
	for(int i = 0; i <= k; i++) {
		ret = min(ret, d[t + i * n]);
	}
	printf("%lld\n", ret);
}

int main() {
	memset(head, -1, sizeof head);
	scanf("%d%d%d", &n, &m, &k);
	scanf("%d%d", &s, &t);
	for(int i = 0, a, b, c; i < m; i++) {
		scanf("%d%d%d", &a, &b, &c);
		add(a, b, c), add(b, a, c);
		for(int j = 1; j <= k; j++) {
			add(a + j * n, b + j * n, c);
			add(b + j * n, a + j * n, c);
			add(a + (j - 1) * n, b + j * n, 0);
			add(b + (j - 1) * n, a + j * n, 0);
		}
	}
	dijkstra();
	return 0;
}

2024/2/27 00:32
加载中...