[USACO12DEC] Milk Milk Routing Routing

Los goo

Meaning of the questions: Farmer John's farm set of old pipe network, pipeline network from the M (1 <= M <= 500) constitutes, for the transport of milk from the barn to milk storage tank he wanted to move in next year. in addition to most pipes and updated, but he wanted to keep intact a complete path, so that he can still transport the milk from the tank to the barn. of N network nodes (1 <= N <= 500) composed of each point can be set as a pipe end. barn node 1, the node N is the tank. Article M bidirectional conduits each connected pair of nodes, and has a retardation value (milk reaches the other end of the tube) and the capacity value (amount of milk per unit time can be stabilized by the pipeline). Multiple pipes can be connected to the same node for a path connecting the barn to the tank, the delay equal to the delay of the path of the pipeline and all along the way, the path is equal to the capacity of the smallest capacity along the pipeline (as this is restricting the transport of milk. " bottleneck "). If John by a delay L, the transport capacity of the milk pipeline units X C, the time required for the L + X / C. John given network configuration, please help him choose a path, such that minimum total time his delivery X units of milk from the barn to the storage tank.

Analysis: The plane just started to see the title thought it was a network stream, then a turn is a stochastic shortest path problem a bit like?. The road to Orgrimmar , but this problem also easier, do not need half the answer, but directly to enumerate the minimum capacity. after the enumeration of the minimum capacity to run the shortest path.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
    int x=0,o=1;char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')o=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*o;
}
const int N=505;
int n,m,x,ans=1e9,visit[N],dis[N],rl[N];
int tot,head[N],nxt[N<<1],to[N<<1],w1[N<<1],w2[N<<1];
inline void add(int a,int b,int c,int d){
    nxt[++tot]=head[a];head[a]=tot;
    to[tot]=b;w1[tot]=c;w2[tot]=d;
}
queue<int>q;
inline void spfa(int maxn){
    for(int i=1;i<=n;++i)dis[i]=1e9,visit[i]=0;
    q.push(1);visit[1]=1;dis[1]=0;
    while(q.size()){
        int u=q.front();q.pop();visit[u]=0;
        for(int i=head[u];i;i=nxt[i]){
            if(w2[i]<maxn)continue;//不符合最小容量要求
            int v=to[i];
            if(dis[v]>dis[u]+w1[i]){
                dis[v]=dis[u]+w1[i];
                if(!visit[v]){
                    visit[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main(){
    n=read(),m=read(),x=read();
    for(int i=1;i<=m;++i){
        int a=read(),b=read(),c=read(),d=read();
        add(a,b,c,d);add(b,a,c,d);rl[i]=d;
    }
    sort(rl+1,rl+m+1);//从小到大排序,方便枚举
    for(int i=1;i<=m;++i){
        spfa(rl[i]);//跑最短路
        ans=min(ans,dis[n]+(int)x/rl[i]);
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/PPXppx/p/11562925.html