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+;
const int M = 4e6+;
int n,m,k,tot=,sum,ans,cnt[N],b,e,p;
int vis[N];
int dis[N][],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==)ss=n;
else ss=s;
for(int i=;i<N;i++)dis[i][s]=inf;
met(vis,);
dis[ss][s]=;
vis[ss]=;
queue<int>q;
q.push(ss);
while(!q.empty()){
int t=q.front();q.pop();
vis[t]=;
for(int i=head[t];i!=-;i=edg[i].next){
int v=edg[i].to;
if(dis[v][s]>dis[t][s]+){
dis[v][s]=dis[t][s]+;
if(!vis[v])q.push(v),vis[v]=;
}
}
}
}
int main(){
met(head,-);
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();
spfa();
spfa();
int ans=inf;
for(int i=;i<=n;i++){
//printf("%d %d %d\n",dis[i][0],dis[i][1],dis[i][2]);
int s=b*dis[i][]+e*dis[i][]+p*dis[i][];
ans=min(ans,s);
}
printf("%d\n",ans);
return ;
}

(寒假集训) Piggyback(最短路)的更多相关文章

  1. CSU-ACM寒假集训选拔-入门题

    CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...

  2. 「BZOJ3694」「FJ2014集训」最短路

    「BZOJ3694」「FJ2014集训」最短路 首先树剖没得说了,这里说一下并查集的做法, 对于一条非树边,它会影响的点就只有u(i),v(i)到lca,对于lca-v的路径上所有点x,都可通过1-t ...

  3. (寒假集训)Roadblock(最短路)

    Roadblock 时间限制: 1 Sec  内存限制: 64 MB提交: 9  解决: 5[提交][状态][讨论版] 题目描述 Every morning, FJ wakes up and walk ...

  4. HZNU-ACM寒假集训Day4小结 最短路

    最短路 1.Floy 复杂度O(N3)  适用于任何图(不存在负环) 模板 --kuangbin #include<iostream> #include<cstdio> #in ...

  5. 中南大学2019年ACM寒假集训前期训练题集(基础题)

    先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...

  6. 【寒假集训系列DAY.1】

    Problem A. String Master(master.c/cpp/pas) 题目描述 所谓最长公共子串,比如串 A:“abcde”,串 B:“jcdkl”,则它们的最长公共子串为串 “cd” ...

  7. 2022寒假集训day2

    day1:学习seach和回溯,初步了解. day2:深度优化搜索 T1 洛谷P157:https://www.luogu.com.cn/problem/P1157 题目描述 排列与组合是常用的数学方 ...

  8. GlitchBot -HZNU寒假集训

    One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a lis ...

  9. Wooden Sticks -HZNU寒假集训

    Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...

随机推荐

  1. IIS Express mime type 列表。

    C:\Users\Administrator\Documents\IISExpress\config\applicationhost.config -------------------------- ...

  2. tomcat运行solr

    https://blog.csdn.net/u010346953/article/details/67640036

  3. Python全栈工程师(迭代器、字节串)

    ParisGabriel                每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰     Python人工智能从入门到精通 迭代器 Iterator: 用<&g ...

  4. Opencv3.4.5安装包

    这个资源是Opencv3.4.5安装包,包括Windows软件包,Android软件包,IOS软件包,还有opencv的源代码:需要的下载吧. 点击下载

  5. perror表

    #define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #defi ...

  6. SQL小助手——SQL Prompt

    背景: 当数据库设计的比较复杂.庞大时,我们如果对脚本不是很熟悉,就会很难完成看似简单的增.删.改.查的操作.我们需要一款软件来给出相应的提示或帮助,来提高代码的可读性,更快更好的完成任务. 简介: ...

  7. 2016年NK冬季训练赛 民间题解

    A题 水题,考察对行的读入和处理,注意使用long long #include <iostream> #include <cstring> #include <cstdi ...

  8. [CF463D]Gargari and Permutations

    题目大意:给你$k(2\leqslant k\leqslant5)$个$1\sim n(n\leqslant10^3)$的排列,求它们的最长子序列 题解:将$k$个排列中每个元素的位置记录下来.如果是 ...

  9. 进程管理利器Supervisor--入门简介

    目录 概述 Supervisor是什么 Supervisor意图 Supervisor特性 Supervisor组件 平台需求 概述 项目运行需要后台运行,一般都是使用 nohup,但是nohup不能 ...

  10. 洛谷 P2114 [NOI2014]起床困难综合症 解题报告

    P2114 [NOI2014]起床困难综合症 题目描述 21世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm一直坚持与起床困难综合症作 ...