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. iOS笔记060 - 自定义控件

    自定义tabBar 系统自带的tabBar不能满足需求 自己定义UITabBar 自定义一个类继承自UITabBar 实现initWithFrame和layoutSubviews方法即可. //#im ...

  2. iOS笔记053- Quartz2D-练习

    1.水印处理 给图片添加文字.图片水印 // 水印处理 - (void)shuiyin { // 水印处理 UIImage *image  = [UIImage imageNamed:@"4 ...

  3. python学习笔记-基础

    1.大小写敏感 2. print (n,f,s1,s2,s3,s4,sep='\n')  -- 换行输出  seq='\n' print ('n=%d'%n,'f=%f'%f,'s1=%s'%s1,' ...

  4. packstack测试环境安装heat

    虚机all in one环境测试安装heat [root@armstrong ~]# tmux at -t mysql MariaDB [(none)]> CREATE DATABASE hea ...

  5. [python] 网络数据采集 操作清单 BeautifulSoup、Selenium、Tesseract、CSV等

    Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesseract.CSV等 Python网络数据采集操作清单 BeautifulSoup.Selenium.Tesse ...

  6. shell之ip命令

    转:出处我也不知道了,学习时候记下的笔记 1.作用 ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig.route等,使用权限为超级用户. ...

  7. django之HTTPResponse和JsonResponse详解

    HttpResponse对象 Django服务器接收到客户端发送过来的请求后,会将提交上来的这些数据封装成一个HttpRequest对象传给视图函数.那么视图函数在处理完相关的逻辑后,也需要返回一个响 ...

  8. hdu 2492 树状数组 Ping pong

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) Ping pong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 3 ...

  9. 华为手机怎么安装Google

    华为手机怎么安装google 新买了个华为荣耀九,结果安装Google Play提示gms core 步骤一 gms 安装器.应用市场已经下架了  地址:链接: 点击打开链接 密码: m63j 步骤二 ...

  10. file mmap

    do_set_pmd 统计参数只会在这里设置: add_mm_counter(vma->vm_mm, MM_FILEPAGES, HPAGE_PMD_NR); 但是这貌似都是处理大页的情况哪,小 ...