洛谷 P3110 [USACO14DEC]驮运Piggy Back spfa

15 篇文章 0 订阅

题目链接 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 然后枚举一下相遇地点即可~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值