列表

详情


NC24370. [USACO 2012 Dec S]Milk Routing

描述

Farmer John's farm has an outdated network of M pipes (1 <= M <= 500) for pumping milk from the barn to his milk storage tank. He wants to remove and update most of these over the next year, but he wants to leave exactly one path worth of pipes intact, so that he can still pump milk from the barn to the storage tank. 
The pipe network is described by N junction points (1 <= N <= 500), each of which can serve as the endpoint of a set of pipes. Junction point 1 is the barn, and junction point N is the storage tank. Each of the M bi-directional pipes runs between a pair of junction points, and has an associated latency (the amount of time it takes milk to reach one end of the pipe from the other) and capacity (the amount of milk per unit time that can be pumped through the pipe in steady state). Multiple pipes can connect between the same pair of junction points. 
For a path of pipes connecting from the barn to the tank, the latency of the path is the sum of the latencies of the pipes along the path, and the capacity of the path is the minimum of the capacities of the pipes along the path (since this is the "bottleneck" constraining the overall rate at which milk can be pumped through the path). If FJ wants to send a total of X units of milk through a path of pipes with latency L and capacity C, the time this takes is therefore L + X/C. 
Given the structure of FJ's pipe network, please help him select a single path from the barn to the storage tank that will allow him to pump X units of milk in a minimum amount of total time.

输入描述

* Line 1: Three space-separated integers: N M X (1 <= X <= 1,000,000).

* Lines 2..1+M: Each line describes a pipe using 4 integers: I J L C.
I and J (1 <= I,J <= N) are the junction points at both ends
of the pipe. L and C (1 <= L,C <= 1,000,000) give the latency
and capacity of the pipe.

输出描述

* Line 1: The minimum amount of time it will take FJ to send milk
along a single path, rounded down to the nearest integer.

示例1

输入:

3 3 15
1 2 10 3
3 2 10 2
1 3 14 1

输出:

27

说明:

INPUT DETAILS:
FJ wants to send 15 units of milk through his pipe network. Pipe #1
connects junction point 1 (the barn) to junction point 2, and has a latency
of 10 and a capacity of 3. Pipes #2 and #3 are similarly defined.

OUTPUT DETAILS:
The path 1->3 takes 14 + 15/1 = 29 units of time. The path 1->2->3 takes
20 + 15/2 = 27.5 units of time, and is therefore optimal.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 5ms, 内存消耗: 588K, 提交时间: 2020-07-07 17:30:38

#include <bits/stdc++.h>
#define ll long long
#define INF 0x7fffffff 
using namespace std;
const int maxn = 505;
int n,m,x;
struct node{
	int l,r,ping,c; 
	bool operator <(const node &p) const{
		return double(ping)+double(x/c) > double(p.ping)+double(x/p.c);
	}
};
priority_queue <node> Q;
vector <node> V[maxn];
int main(){
    ios::sync_with_stdio(false);
    //freopen("1.in.txt","r",stdin);
	//freopen("1.out","w",stdout);	
	cin >> n >> m >> x;
	int l1,r1,ping1,c1;
	for(int i = 0;i < m;i++){
		cin >> l1 >> r1 >> ping1 >> c1;
		V[l1].push_back({l1,r1,ping1,c1});
		V[r1].push_back({r1,l1,ping1,c1});
	}
	for(int i = 0;i < V[1].size();i++)	Q.push(V[1][i]);
	while(Q.size()){
		node temp = Q.top();
		if(temp.r == n){
			ll ans = temp.ping+x/temp.c;
			cout << ans << endl;
			break;
		}
		Q.pop();
		for(int i = 0;i < V[temp.r].size();i++){
			if(temp.l == V[temp.r][i].r)	continue;
			Q.push({temp.r,V[temp.r][i].r,temp.ping+V[temp.r][i].ping,min(temp.c,V[temp.r][i].c)});
		}
	} 
	return 0;
}

C++ 解法, 执行用时: 17ms, 内存消耗: 464K, 提交时间: 2022-07-16 15:03:18

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
struct node{int w,v;};
int n,m,xx,ans=1e9;
int dis[510],h[510],vis[510];
vector<pair<int,node>>vt[510];
void dij(int minn){
	memset(dis,0x3f,sizeof(dis));
	memset(vis,0,sizeof(vis));
	priority_queue<pii,vector<pii>,greater<pii>>q;
	q.push({0,1});
	dis[1]=0;
	while(q.size()){
		auto t=q.top().second;q.pop();
		if(vis[t]) continue;
		vis[t]=1;
		for(auto it:vt[t]){
			int nx=it.first,nw=it.second.w,nv=it.second.v;
			if(nv<minn) continue;
			dis[nx]=min(dis[nx],dis[t]+nw);
			if(!vis[nx]) q.push({dis[nx],nx});
		}
	}
}
int main(){
	cin>>n>>m>>xx;
	for(int i=1;i<=m;i++){
		int a,b,c,d;cin>>a>>b>>c>>d;
		h[i]=d;
		vt[a].push_back({b,node{c,d}});
		vt[b].push_back({a,node{c,d}});
	}
	for(int i=1;i<=m;i++){
		dij(h[i]);
		ans=min(ans,dis[n]+xx/h[i]);
	}
	cout<<ans;
} 

pypy3(pypy3.6.1) 解法, 执行用时: 185ms, 内存消耗: 25952K, 提交时间: 2020-06-26 22:43:14

#!/usr/bin/env python3
#
# Milk Routing
#
import sys, os, math
from collections import deque

def read_ints(): return list(map(int, input().split()))
#------------------------------------------------------------------------------#

n, m, x = read_ints()
g = [[] for _ in range(n)]
for _ in range(m):
	i, j, l, c = read_ints()
	i -= 1; j -= 1
	g[i].append((j, l, c))
	g[j].append((i, l, c))

q = deque()
q.append((0, 0, 1e18))
s = {}
for i in range(n): s[i] = []
s[0].append((0, 1e18))
while q:
	u, ll, cc = q.popleft()
	for v, l, c in g[u]:
		nl, nc = l + ll, min(cc, c)
		found = False
		for tl, tc in s[v]:
			if tl <= nl and tc >= nc:
				found = True
				break
		if not found:
			s[v].append((nl, nc))
			q.append((v, nl, nc))

ans = 1e18
for l, c in s[n - 1]:
	ans = min(ans, l + x // c)

print(ans)

上一题