WA on #6 求调
查看原帖
WA on #6 求调
745434
Ifyoung楼主2023/10/26 09:03

rt

#include "bits/stdc++.h"
#define itn int
#define int long long

using namespace std;

int read() {
	int x = 0, w = 1;
	char ch = 0;
	while (ch < '0' || ch > '9') {
		if (ch == '-') w = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + (ch ^ '0');
		ch = getchar();
	}
	return x * w;
}

void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) write(x / 10);
	putchar(x % 10 ^ '0');
}

void writep(int x) {
	write(x), putchar(' ');
}

void writeh(int x) {
	write(x), puts("");
}

typedef pair <int, int> PII;

const int K = 210, N = 2e3 + 5, M = 1e4 + 5;

int k, n, m, st, ed; // 200, 2e3, 1e4
int he[N], tot;
struct edge {
	int to, nxt, t, h;
} e[M << 1];
int dis[N][K];
bool vis[N];
int ans = 0x3f3f3f3f;

void add(int u, int v, int t, int h) {
	e[ ++ tot] = (edge) {v, he[u], t, h};
	he[u] = tot;
}

void dij(int u) {
	memset(dis, 0x3f, sizeof dis);
	dis[u][0] = 0;
	priority_queue <PII, vector <PII>, greater <PII> > q; // 第一个是距离,第二个是节点
	q.push({0, u});
	while (q.size()) {
		auto p = q.top();
		q.pop();
		int v = p.second, di = p.first;
		if (vis[v]) continue;
		vis[v] = 1;
		for (int i = he[v]; i; i = e[i].nxt) {
			int j = e[i].to, h = e[i].h, t = e[i].t; // h 为损坏值,t 为长度
			for (int o = 0; o < k - h; o ++ ) {
				if (dis[j][o + h] > dis[v][o] + t) {
					dis[j][o + h] = dis[v][o] + t;
					q.push({dis[j][o + h], j});
				}
			}
		}
	}
}

signed main() {
	k = read(), n = read(), m = read();
	for (int i = 1; i <= m; i ++ ) {
		int u = read(), v = read(), t = read(), h = read();
		add(u, v, t, h), add(v, u, t, h);
	}
	st = read(), ed = read();
	dij(st);
	for (int i = 0; i < k; i ++ ) {
		ans = min(ans, dis[ed][i]);
	}
	if (ans == 0x3f3f3f3f) puts("-1");
	else writeh(ans);
	return 0;
}
2023/10/26 09:03
加载中...