关于题目解法的一点疑问
查看原帖
关于题目解法的一点疑问
494862
MIKE_LO_MPPC楼主2023/8/13 12:14

此题如果路径容量和答案是不同的极值(如容量取 min⁡\min 而答案取 max⁡\max 或反过来 ),用以下代码是不是不能做或要加些语句

#include <bits/stdc++.h>
using namespace std;
const int N = 1e3+10;

int to[N], nxt[N], wl[N], wc[N], head[N], cnt;

void add(int x, int y, int L, int C) {
	to[++cnt] = y;
	nxt[cnt] = head[x];
	wl[cnt] = L;
	wc[cnt] = C;
	head[x] = cnt;
}

int n, m, X, C[N], dis[N], dic[N];

struct Node {
	int x, d;
	bool operator < (const Node &b) const {
		return d < b.d;
	}
};

void dijkstra(int lmt) {
	memset(dis, 0x3f, sizeof(dis));
	priority_queue<Node> q;
	dis[1] = 0;
	q.push((Node){1, 0});
	while (!q.empty()) {
		int x = q.top().x;
		q.pop();
		for (int i = head[x]; i; i = nxt[i]) {
			int y = to[i], l = wl[i], c = wc[i];
			if (c < lmt) continue;
			if (dis[y] > dis[x] + l) {
				dis[y] = dis[x] + l;
				q.push((Node){y, dis[y]});
			}
		}
	}
}

int main() {
	scanf("%d%d%d", &n, &m, &X);
	for (int i = 1; i <= m; i++) {
		int x, y, L;
		scanf("%d%d%d%d", &x, &y, &L, C + i);
		add(x, y, L, C[i]);
		add(y, x, L, C[i]);
	}
	double ans = 1e9;
	for (int i = 1; i <= m; i++) {
		dijkstra(C[i]);
		ans = min(ans, dis[n] + X * 1.0 / C[i]);
	}
	printf("%d", (int)ans);
	return 0;
}
2023/8/13 12:14
加载中...