P5837 [USACO19DEC]Milk Pumping G

知识点:枚举,最短路

这个题的正解应该是枚举+最短路,枚举每个边的流量,然后求起点到终点的最短路,当然小于枚举的流量的边其实就是不连通的,这样这个题就做出来了,但是一开始我写的是深搜,加了剪枝,用邻接矩阵存储,勉强过了,但是这个枚举+最短路确跑的快很多,明明复杂度也有1e7了,可能是因为实际中有很多断边的原因把,深搜的代码就不放了

#include <bits/stdc++.h>

using namespace std;

const int N = 2005;

struct node {
	int x, d;
	node() {}
	node(int a, int b): x(a), d(b) {}
	bool operator < (const node &a) const {
		return d > a.d;
	}
};

int tot, ver[N], c[N], f[N], nxt[N], head[N];
int n, m, a[N];

void add(int x, int y, int t1, int t2) {
	ver[++tot] = y;
	c[tot] = t1; f[tot] = t2;
	nxt[tot] = head[x]; head[x] = tot;
}

int bfs(int x) {
	priority_queue<node> q;
	q.push(node(1, 0));
	int vis[N] = {};
	while (!q.empty()) {
		node now = q.top(); q.pop();
		if (now.x == n) return now.d;
		if (vis[now.x]) continue;
		vis[now.x] = 1;
		for (int i = head[now.x]; i; i = nxt[i]) {
			if (f[i] >= x) q.push(node(ver[i], now.d + c[i]));
		}
	}
	return -1;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int x, y, z;
		scanf("%d%d%d%d", &x, &y, &z, &a[i]);
		add(x, y, z, a[i]);
		add(y, x, z, a[i]);
	}
	double ans = 0;
	for (int i = 1; i <= m; i++) {
		int tmp = bfs(a[i]);
		if (tmp == -1) continue;
		ans = max(ans, 1.0 * a[i] / tmp);
	}
	cout << (int) (ans * 1e6);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值