题目链接 https://www.luogu.org/problem/show?pid=3110

题目描述

Bessie and her sister Elsie graze in different fields during the day, and in the evening they both want to walk back to the barn to rest. Being clever bovines, they come up with a plan to minimize the total amount of energy they both spend while walking.

Bessie spends B units of energy when walking from a field to an adjacent field, and Elsie spends E units of energy when she walks to an adjacent field. However, if Bessie and Elsie are together in the same field, Bessie can carry Elsie on her shoulders and both can move to an adjacent field while spending only P units of energy (where P might be considerably less than B+E, the amount Bessie and Elsie would have spent individually walking to the adjacent field). If P is very small, the most energy-efficient solution may involve Bessie and Elsie traveling to a common meeting field, then traveling together piggyback for the rest of the journey to the barn. Of course, if P is large, it may still make the most sense for Bessie and Elsie to travel

separately. On a side note, Bessie and Elsie are both unhappy with the term “piggyback”, as they don’t see why the pigs on the farm should deserve all the credit for this remarkable form of

transportation.

Given B, E, and P, as well as the layout of the farm, please compute the minimum amount of energy required for Bessie and Elsie to reach the barn.

Bessie 和 Elsie在不同的区域放牧,他们希望花费最小的能量返回谷仓。从一个区域走到一个相连区域,Bessie要花费B单位的能量,Elsie要花费E单位的能量。

如果某次他们两走到同一个区域,Bessie 可以背着 Elsie走路,花费P单位的能量走到另外一个相连的区域,满足P

#include<iostream>
#include<cstring>
#include<cstdio>
#include <queue>
#include <vector>
using namespace std;
int b,e,p,n,m;
const int N=200005;
int to[N*4],nex[N*4],head[N*4];
int tot;
int dis[N*4],vis[N*4];
inline void add(int x,int y){
    ++tot;
    nex[tot]=head[x];
    to[tot]=y;
    head[x]=tot;
}
int aa[N];
int bb[N];
int cc[N];
inline void spfa(int s,int f[]){
    queue<int> q;
    q.push(s);
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;++i) f[i]=2147483640;

    vis[s]=1;
    f[s]=0;
    while(!q.empty()){
        int dmf=q.front();
        q.pop();
        vis[dmf]=0;
        for(int i=head[dmf];i;i=nex[i])
        {
            int h=to[i];
            if(f[h]>f[dmf]+1)
            {
                f[h]=f[dmf]+1;
                if(!vis[h])
                {
                    vis[h]=true;
                    q.push(h);
                }
            }
        }
    }
}
int main(){
    scanf("%d%d%d%d%d",&b,&e,&p,&n,&m);
    for(int i=1;i<=m;++i)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    spfa(1,aa);
    spfa(2,bb);
    spfa(n,cc);
    int ans=2147483640;
    for(int i=1;i<=n;++i)
    {
        ans=min(ans,b*aa[i]+e*bb[i]+p*cc[i]);
    }
    printf("%d\n",ans);
    return 0;
}

三遍spfa 然后枚举一下相遇地点即可~~~