zoukankan      html  css  js  c++  java
  • (寒假集训) Piggyback(最短路)

    Piggyback

    时间限制: 1 Sec  内存限制: 64 MB
    提交: 3  解决: 3
    [提交][状态][讨论版]

    题目描述

    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.

    输入

    The first line of input contains the positive integers B, E, P, N, and M.  All of these are at most 40,000.  B, E, and P are described above. N is the number of fields in the farm (numbered 1..N, where N >= 3), and M is the number of connections between fields.  Bessie and Elsie start in fields 1 and 2, respectively.  The barn resides in field N.

    The next M lines in the input each describe a connection between a pair of different fields, specified by the integer indices of the two  fields.  Connections are bi-directional.  It is always possible to travel from field 1 to field N, and field 2 to field N, along a series of such connections. 

    输出

    A single integer specifying the minimum amount of energy Bessie and Elsie collectively need to spend to reach the barn.  In the example shown here, Bessie travels from 1 to 4 and Elsie travels from 2 to 3 to 4.  Then, they travel together from 4 to 7 to 8.

    样例输入

    4 4 5 8 8
    1 4
    2 3
    3 4
    4 7
    2 5
    5 6
    6 8
    7 8
    

    样例输出

    22
    【分析】简单地说就是给你一张地图,n个点m条边,有两个人甲和乙,甲从1节点走到n节点每经过一条边消耗 B 能量,乙从2节点走到n几点每经过一条边消耗 E 能量,
    若甲背着乙走,共消耗 P (P<B+E)能量。求使得甲乙俩人都到达n节点小号的最少的总能量。思路是三遍spfa,求出节点到1,2,n节点的最短距离,然后枚举俩人会和的点,取能量最小值。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 2e9
    #define met(a,b) memset(a,b,sizeof a)
    typedef long long ll;
    using namespace std;
    const int N = 1e5+5;
    const int M = 4e6+5;
    int n,m,k,tot=0,sum,ans,cnt[N],b,e,p;
    int vis[N];
    int dis[N][3],head[N];
    struct man{
        int to,next;
    }edg[M];
    void add(int u,int v){
        edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
    }
    void spfa(int s)
    {
        int ss;
        if(s==0)ss=n;
        else ss=s;
        for(int i=0;i<N;i++)dis[i][s]=inf;
        met(vis,0);
        dis[ss][s]=0;
        vis[ss]=1;
        queue<int>q;
        q.push(ss);
        while(!q.empty()){
            int t=q.front();q.pop();
            vis[t]=0;
            for(int i=head[t];i!=-1;i=edg[i].next){
                int v=edg[i].to;
                if(dis[v][s]>dis[t][s]+1){
                    dis[v][s]=dis[t][s]+1;
                    if(!vis[v])q.push(v),vis[v]=1;
                }
            }
        }
    }
    int main(){
        met(head,-1);
        scanf("%d%d%d%d%d",&b,&e,&p,&n,&m);
        while(m--){
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);add(v,u);
        }
     
        spfa(0);
        spfa(1);
        spfa(2);
        int ans=inf;
        for(int i=1;i<=n;i++){
            //printf("%d %d %d
    ",dis[i][0],dis[i][1],dis[i][2]);
            int s=b*dis[i][1]+e*dis[i][2]+p*dis[i][0];
            ans=min(ans,s);
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    19c 新特性: Hint Usage Reports详解
    byte buddy学习笔记
    彻底告别“人工+Excel”低效模式,传统制造业实现“一站式”数据化管理
    有的人想在上班之余做兼职,并且不想投入太多钱
    2019你为什不选择创业?是因为什么原因?
    深入理解pandas读取excel,txt,csv文件等命令
    劳务派遣公司是怎么盈利的?
    为什么穷人大多不敢创业?
    30多岁了仍一事无成,除了去工厂上班还有哪些出路?
    MySQL Change Data Directory
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6270693.html
Copyright © 2011-2022 走看看