首页 > 其他分享 >NC24370 [USACO 2012 Dec S]Milk Routing

NC24370 [USACO 2012 Dec S]Milk Routing

时间:2023-01-03 20:12:06浏览次数:61  
标签:pipe int pipes USACO NC24370 Routing path milk dis

题目链接

题目

题目描述

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.

题解

知识点:枚举,最短路。

每个管道有两个参数,延迟 \(L\) 和能力 \(C\) ,一条路径的时间花费是 \(\sum L_i + \frac{X}{\min(C_i)}\) ,显然路径上所有管道的 \(C\) 要尽可能大而 \(L\) 要尽可能小。然而,单看 \(L\) 尽可能小,最短路就能解决,但是另一个参数 \(C\) 就不确定了,而且没有办法兼顾两个参数。因此,我们要想办法确定一个参数。

我们已经知道 \(L\) 很好跑最短路,而 \(C\) 没有什么好的办法,那么我们手动控制 \(C\) 。枚举 \(C\) 的大小,每次固定一个 \(C\) 控制跑最短路时选取的管道,小于 \(C\) 的不选,最终取时间最小值即可。

当然也可以二分 \(C\) 不过这里枚举就能过了。

时间复杂度 \(O(\max(C_i)(n+m)\log m)\)

空间复杂度 \(O(n+m)\)

代码

#include <bits/stdc++.h>
#define ll long long

using namespace std;

template<class T>
struct Graph {
    struct edge {
        int v, nxt;
        T w;
    };
    int idx;
    vector<int> h;
    vector<edge> e;

    Graph(int n, int m) :idx(0), h(n + 1), e(m + 1) {}
    void init(int n) {
        idx = 0;
        h.assign(n + 1, 0);
    }

    void add(int u, int v, T w) {
        e[++idx] = edge{ v,h[u],w };
        h[u] = idx;
    }
};

const int N = 507, M = 507 << 1;
Graph<pair<int, int>> g(N, M);
int n, m, x;

int dis[N];
bool vis[N];
struct node {
    int v, w;
    friend bool operator<(const node &a, const node &b) {
        return a.w > b.w;
    }
};
priority_queue<node> pq;
void dijkstra(int st, int val) {
    for (int i = 1;i <= n;i++) dis[i] = 0x3f3f3f3f, vis[i] = 0;
    dis[st] = 0;
    pq.push({ st,0 });
    while (!pq.empty()) {
        int u = pq.top().v;
        pq.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = g.h[u];i;i = g.e[i].nxt) {
            int v = g.e[i].v;
            auto [l, c] = g.e[i].w;
            if (c < val) continue;
            if (dis[v] > dis[u] + l) {
                dis[v] = dis[u] + l;
                pq.push({ v,dis[v] });
            }
        }
    }
}

int main() {
    std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m >> x;
    int mx = 0;
    for (int i = 1;i <= m;i++) {
        int u, v, l, c;
        cin >> u >> v >> l >> c;
        g.add(u, v, { l,c });
        g.add(v, u, { l,c });
        mx = max(mx, c);
    }
    int ans = 0x3f3f3f3f;
    for (int i = mx;i >= 1;i--) {
        dijkstra(1, i);
        ans = min(ans, (int)round(dis[n] + x / i));
    }
    cout << ans << '\n';
    return 0;
}

标签:pipe,int,pipes,USACO,NC24370,Routing,path,milk,dis
From: https://www.cnblogs.com/BlankYang/p/17023243.html

相关文章

  • NC24755 [USACO 2010 Dec S]Apple Delivery
    题目链接题目题目描述Bessiehastwocrispredapplestodelivertotwoofherfriendsintheherd.Ofcourse,shetravelstheC(1<=C<=200,000)cowpaths......
  • NC24858 [USACO 2009 Nov S]Job Hunt
    题目链接题目题目描述Bessieisrunningoutofmoneyandissearchingforjobs.FarmerJohnknowsthisandwantsthecowstotravelaroundsohehasimposeda......
  • Navigation & Routing
    LeartohandlenavigationbetweendifferentpagesinFlutter.Thewayofhandlingnavigationiscalledrouting.Inthissection,wewilldiscussnavigationand......
  • P1217 [USACO1.5]回文质数 Prime Palindromes
    题目题目描述因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以151是回文质数。写一个程序来找出范围[a,b](5<=a<b<=100,000,000)(一亿)间的......
  • USACO 2019 January Contest, Silver
    2019JanuarySilverT1.GrassPlanting这道题是一道结论题:我们发现点的度数越多,答案越多,所以我们可以发现答案为最大的点的度数+1。#include<bits/stdc++.h>usingnam......
  • AcWing1170. 排队布局[USACO05]
    解题思路\(\qquad\)这题也是一个比较裸的差分约束:多了的那个输出\(-2\)的其实就是在差分约束系统中\(1\)号点和\(n\)号点没有约束关系,也就是\(1\)和\(n\)号不连通。由于这......
  • P2894 [USACO08FEB]Hotel G
    \(P2894\)[\(USACO08FEB\)]\(Hotel\)\(G\)一、题目描述参考样例,第一行输入\(n,m\),\(n\)代表有\(n\)个房间,编号为\(1-\simn\),开始都为空房,\(m\)表示以下有\(m\)行操作......
  • [USACO22DEC] Cow College B 题解
    洛谷P8897AcWing4821题目描述有\(n\)头奶牛,每头奶牛愿意交的最大学费位\(c_i\),问如何设置学费,可以使赚到的钱最多。\(1\len\le10^5,1\lec_i\le10^6\)做法分析......
  • [USACO22FEB] Sleeping in Class B
    好题分享——[USACO22FEB]SleepinginClassB洛谷题目链接题面翻译\(T\)组数据,每组给定一个长度为\(N\)的数组\(a_1,a_2,\dotsb,a_n\)。每次操作可选择两个......
  • P3128 [USACO15DEC]Max Flow P 树上点差分
    //题意:给出一棵树,现在有一操作:给出两点,将两点之间的路径都加1,最后统计整棵树中值最大的点是谁//思路:树上路径问题,树剖+线段树可以解决,但是因为只是简单的维护区间加减,用......