洛谷 P3063 [USACO12DEC]牛奶的路由Milk Routing

洛谷传送门

JDOJ 2334: USACO 2012 Dec Silver 3.Milk Routing

JDOJ传送门

Description

Problem 3: Milk Routing [Brian Dean, 2012]

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.

Input

* 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.

Output

* Line 1: The minimum amount of time it will take FJ to send milk

along a single path, rounded down to the nearest integer.

Sample Input

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

Sample Output

27

HINT

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.

题目翻译:

农民约翰的农场有一套老旧的管网,管网由M条管道(1<=M<=500)构成,用于将牛奶从谷仓运到储奶罐。 他想在明年移除和更新大部分管道,但他想原封不动地保留一条完整的路径,这样他仍然可以把牛奶从谷仓输送到储罐。

管网由N个节点(1<=N<=500)组成,每个点都可以作为一组管道的端点。结点1是谷仓,结点N是储罐。M条双向管道中的每一条都连接一对节点,并且都有一个延迟值(牛奶达到管的另一端的用时)和容量值(单位时间内可以稳定通过管道的牛奶量)。多条管道可以连接同一对节点。

对于一条连接谷仓与储罐的路径,路径的延迟等于沿途所有管道的延迟之和,路径的容量等于沿途管道最小的容量(因为这是制约牛奶运送的“瓶颈”)。如果约翰通过一条延迟为L、容量为C的管道运送X个单位的牛奶,需要的时间为L+X/C。

给出约翰的管网结构,请帮助他选择一条路径,使得他从谷仓到储罐运送X个单位牛奶的总时间最少。

题解:

一道图论中的最短路变形题目。

我的题意又理解错了,不要以为是单纯的松弛时间就可以了。这道题的坑点在于,你的一条路径的时间计算中的C其实是整条路径的C的最小值。

所以我们就不能直接拍板子了。

那我们怎么解决跑最短路的时候还能维护时间呢?

直观一点想,我们在每次松弛的时候可以维护一个可行的最短时间。

所以我们就想到了,能不能在SPFA的模板中加一个参数,使得它能够不断地被更新,达到松弛时间的目的呢?

我们可以实现,具体方式是新开一个数组,然后保存每条边的需要时间,最后枚举和跑SPFA的时候直接按这个时间枚举即可。

代码:

#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#pragma GCC optimize(2)
using namespace std;
int n,m,t,ans=1e9;
int tot,to[1001],vall[1001],valc[1001],nxt[1001],head[501],c[501];
int f[501];
bool v[501];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void add(int x,int y,int zl,int zc)
{
to[++tot]=y;
vall[tot]=zl;
valc[tot]=zc;
nxt[tot]=head[x];
head[x]=tot;
}
void spfa(int w)
{
memset(f,0x3f,sizeof(f));
memset(v,0,sizeof(v));
queue<int> q;
q.push(1);
v[1]=1;
f[1]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
v[x]=0;
for(int i=head[x];i;i=nxt[i])
{
int y=to[i];
if(f[y]>f[x]+vall[i] && valc[i]>=w)
{
f[y]=f[x]+vall[i];
if(v[y]==0)
{
q.push(y);
v[y]=1;
}
}
}
}
}
int main()
{
n=read();
m=read();
t=read();
for(int i=1;i<=m;i++)
{
int x,y,zl,zc;
x=read();
y=read();
zl=read();
zc=read();
add(x,y,zl,zc);
add(y,x,zl,zc);
c[i]=zc;
}
for(int i=1;i<=m;i++)
{
spfa(c[i]);
ans=min(ans,f[n]+t/c[i]);
}
printf("%d",ans);
return 0;
}

USACO Milk Routing的更多相关文章

  1. USACO Milk Routing /// 优先队列广搜

    题目大意: 在n个点 m条边的无向图中 需要运送X单位牛奶 每条边有隐患L和容量C 则这条边上花费时间为 L+X/C 求从点1到点n的最小花费 优先队列维护 L+X/C 最小 广搜到点n #inclu ...

  2. 洛谷 P3063 [USACO12DEC]牛奶的路由Milk Routing

    P3063 [USACO12DEC]牛奶的路由Milk Routing 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 Farmer John's farm ...

  3. Milk Pumping G&Milk Routing S 题解

    Milk Pumping G&Milk Routing S 双倍经验时间 洛谷P5837 [USACO19DEC]Milk Pumping G 洛谷P3063 [USACO12DEC]Milk ...

  4. USACO milk

    /* ID:kevin_s1 PROG:milk LANG:C++ */ #include <iostream> #include <string> #include < ...

  5. 【luogu P3063 [USACO12DEC]牛奶的路由Milk Routing】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3063#sub 我很好奇这道题为什么没被收入SPFA好题 #include <cstdio> #i ...

  6. 洛谷P3063 [USACO12DEC]牛奶的路由Milk Routing

    链接 其实在博客园里写题解都挺应付的都是在洛谷写了之后 挑一部分粘过来 在洛谷写的也都是废话,是为了凑篇幅 主要就是代码 大体思路就一提 这题贪心不行废话 跑m遍SPFA更新最小值 注意数组记得清空 ...

  7. 洛谷 P3063 【[USACO12DEC]Milk Routing S】

    这道题可以暴力哒~ 我们枚举每一个出现过的容量,然后跑一次最短路,求延迟,在跑最短路的时候,如果遇到的某一个点,比我们当前枚举的那个点小,那么就直接不走这一个点,然后枚举完后,就能得到最大值了. 代码 ...

  8. Mixing Milk 混合牛奶 USACO 贪心

    1009: 1.3.1 Mixing Milk 混合牛奶 时间限制: 1 Sec  内存限制: 128 MB提交: 9  解决: 9[提交] [状态] [讨论版] [命题人:外部导入] 题目描述 1. ...

  9. 【BZOJ】【1717】【USACO 2006 Dec】Milk Patterns产奶的模式

    后缀数组 o(︶︿︶)o 唉傻逼了一下,忘了把后缀数组的字典范围改回20001,直接21交了上去,白白RE了两发……sigh 既然要找出现了K次的子串嘛,那当然要用后缀数组了>_>(因为我 ...

随机推荐

  1. jquery + node 通过 CORS 实现跨域访问,支持cookie和自定义header

    跨域有多种方式,现在的情况看来还是CORS更适合一些,有很多优点,比如浏览器正式支持.支持post.可以控制跨域访问的网站等. 我们来看看node如何实现cors方式的跨域.在网上找到了一些代码,考过 ...

  2. PHP 命名空间笔记

    PHP 命名空间笔记 1.php文件代码如下<pre><?php//我用这样的命名空间表示处于blog下的article模块namespace Blog\Article; class ...

  3. Python学习教程(一)自学资源分享

    Python 可以用来做什么? 在我看来,基本上可以不负责任地认为,Python 可以做任何事情.无论是从入门级选手到专业级选手都在做的爬虫,还是Web 程序开发.桌面程序开发还是科学计算.图像处理, ...

  4. Deep Learning专栏--强化学习之从 Policy Gradient 到 A3C(3)

    在之前的强化学习文章里,我们讲到了经典的MDP模型来描述强化学习,其解法包括value iteration和policy iteration,这类经典解法基于已知的转移概率矩阵P,而在实际应用中,我们 ...

  5. 单点登录(sso)入门

    单点登录的英文名叫做Single Sign On,简称SSO. 在以前,一般我们就单系统,所有的功能都在同一个系统上. 后来,我们为了合理利用资源和降低耦合性,于是把单系统拆分成多个子系统. 比如阿里 ...

  6. git的本质是资源库和版本(资源)目录的维护过程

    仓库的本质: 资源+索引. 对git而言,添加到暂存区的过程是,将单个资源的修改副本保存到资源库,同时维护暂存区目录的过程. git的本质是资源库和版本目录的维护过程. 一.要素 1.资源 2.副本 ...

  7. 浅析libuv源码-node事件轮询解析(2)

    上一篇讲了轮询的边角料,这篇进入正题.(竟然真有人看我博客,上两个图给你们整理下思路) 这是轮询总流程图. 下图为本节内容简图. Poll for I/O The loop blocks for I/ ...

  8. kafka 解密:破除单机topic数多性能下降魔咒

    https://bbs.huaweicloud.com/blogs/112956 版权归PUMA项目组所有,转载请声明,多谢. kakfa大规模集群能力在前面已给大家分享过,kafka作为消息总线,在 ...

  9. c# 类实例序列化反序列化json文件 (原发布 csdn 2017-10-01 20:02:12)

    前言 前段时间使用了net.json保存对象数据.添加完成后,测试发现300多实例数据保存加载json文件,速度比原方式(BinaryFormatter)慢.但是功能加上后也懒再删掉代码了,索性就采用 ...

  10. 强化Linux 服务器的7个步骤

    这篇入门文章将向你介绍基本的 Linux 服务器安全知识.虽然主要针对 Debian/Ubuntu,但是你可以将此处介绍的所有内容应用于其他 Linux 发行版.我也鼓励你研究这份材料,并在适用的情况 ...