牛客 NC24307 [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.

思路:在建图的同时将每条边的容量单独存储一遍,而后遍历所有的容量跑spfa,只有当遍历到的边大于等于当前的容量时才进行更新,每次都取答案与spfa处理后的dist[n]作对比

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

const int N=5010;

int n,m,x;
int h[N],ne[N],w[N],e[N],s[N],idx;
int dist[N],d[N];
bool st[N];
queue<int>q;

void add(int a,int b,int t,int c)
{
	s[idx]=t,e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}

void spfa(int x)
{
	memset(dist,0x3f,sizeof dist);
	dist[1]=0;
	q.push(1);
	
	while(q.size())
	{
		int t=q.front();
		q.pop();
		st[t]=0;
		
		for(int i=h[t];~i;i=ne[i])
		{
			int j=e[i];
			if(dist[j]>dist[t]+s[i]&&w[i]>=x)
			{
				dist[j]=dist[t]+s[i];
				if(!st[j])
				{
					st[j]=1;
					q.push(j);
				}
			}
		}
	}
}

int main()
{
	memset(h,-1,sizeof h);
	scanf("%d%d%d",&n,&m,&x);
	for(int i=0;i<m;i++)
	{
		int a,b,c,e;
		scanf("%d%d%d%d",&a,&b,&c,&e);
		add(a,b,c,e),add(b,a,c,e);
		d[i]=e;
	}
	
	int res=0x3f3f3f3f;
	for(int i=0;i<m;i++)
	{
		spfa(d[i]);
		res=min(res,dist[n]+x/d[i]);
	}
	cout<<res<<endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Double.Qing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值