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

Posted zquzjx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了USACO Milk Routing /// 优先队列广搜相关的知识,希望对你有一定的参考价值。

题目大意:

在n个点 m条边的无向图中 需要运送X单位牛奶

每条边有隐患L和容量C 则这条边上花费时间为 L+X/C

求从点1到点n的最小花费

 

优先队列维护 L+X/C 最小 广搜到点n

技术图片
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
#define mem(i,j) memset(i,j,sizeof(i))
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,r,l) for(int i=r;i>=l;i--)
const int N=2000+5;
const int mod=1e9+7;

int n,M;
LL X;
struct NODE {
    int u,v; LL L,C;
    bool operator <(const NODE& p) const {
        return (double)L+(double)X/C > (double)p.L+(double)X/p.C;
    }
};
vector<NODE>G[505];

int main()
{
    while(~scanf("%d%d%lld",&n,&M,&X)) {
        inc(i,1,n) G[i].clear();
        inc(i,1,M) {
            int u,v; LL l,c;
            scanf("%d%d%lld%lld",&u,&v,&l,&c);
            G[u].push_back({u,v,l,c});
            G[v].push_back({v,u,l,c});
        }
        LL ans;
        priority_queue <NODE> q;
        while(!q.empty()) q.pop();
        q.push({1,1,0,INF});
        while(!q.empty()) {
            NODE e=q.top(); q.pop();
            if(e.v==n) {
                ans=e.L+X/e.C; break;
            }
            int len=G[e.v].size()-1;
            inc(i,0,len) {
                NODE t=G[e.v][i];
                if(t.v==e.u) continue;
                q.push({e.v,t.v,e.L+t.L,min(e.C,t.C)});
            }
        }
        printf("%lld
",ans);
    }

    return 0;
}
View Code

 

以上是关于USACO Milk Routing /// 优先队列广搜的主要内容,如果未能解决你的问题,请参考以下文章

[USACO12DEC]牛奶的路由Milk Routing

USACO 5.3 Milk Measuring

[USACO06DEC] Milk Patterns

[USACO06DEC] Milk Patterns

题解 P5116 [USACO18DEC]Mixing Milk

USACO Mother&#39;s Milk(bfs)