(寒假集训) Piggyback(最短路)
Piggyback
时间限制: 1 Sec 内存限制: 64 MB
提交: 3 解决: 3
[提交][状态][讨论版]
题目描述
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.
输入
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.
输出
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(最短路)的更多相关文章
- CSU-ACM寒假集训选拔-入门题
CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...
- 「BZOJ3694」「FJ2014集训」最短路
「BZOJ3694」「FJ2014集训」最短路 首先树剖没得说了,这里说一下并查集的做法, 对于一条非树边,它会影响的点就只有u(i),v(i)到lca,对于lca-v的路径上所有点x,都可通过1-t ...
- (寒假集训)Roadblock(最短路)
Roadblock 时间限制: 1 Sec 内存限制: 64 MB提交: 9 解决: 5[提交][状态][讨论版] 题目描述 Every morning, FJ wakes up and walk ...
- HZNU-ACM寒假集训Day4小结 最短路
最短路 1.Floy 复杂度O(N3) 适用于任何图(不存在负环) 模板 --kuangbin #include<iostream> #include<cstdio> #in ...
- 中南大学2019年ACM寒假集训前期训练题集(基础题)
先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...
- 【寒假集训系列DAY.1】
Problem A. String Master(master.c/cpp/pas) 题目描述 所谓最长公共子串,比如串 A:“abcde”,串 B:“jcdkl”,则它们的最长公共子串为串 “cd” ...
- 2022寒假集训day2
day1:学习seach和回溯,初步了解. day2:深度优化搜索 T1 洛谷P157:https://www.luogu.com.cn/problem/P1157 题目描述 排列与组合是常用的数学方 ...
- GlitchBot -HZNU寒假集训
One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a lis ...
- Wooden Sticks -HZNU寒假集训
Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...
随机推荐
- 【Linear Regression】林轩田机器学习基石
这一节开始讲基础的Linear Regression算法. (1)Linear Regression的假设空间变成了实数域 (2)Linear Regression的目标是找到使得残差更小的分割线(超 ...
- ACM二分搜索算法
二分搜索算法就是把要搜索的数据在搜索文本中根据情况进行折半,比如要在2 6 4 9 3 8 7 3 5中找到找到4的位置,那么可以考虑先把数据进行排序,然后把拍好后的数据的中间的那个数据和要查找的数据 ...
- 课时1:我和python的第一次亲密接触
目录: 一.Python3的下载与安装 二.从IDIE启动Python 三.尝试点新的东西 四.为什么会这样? 五.课时01课后习题及答案 ============================== ...
- NodeJS05
商品分类模块 分类model const mongoose = require('mongoose') const schema = new mongoose.Schema({ name: { typ ...
- HDU 1556 Color the ball (树状数组区间更新)
水题,练习一下树状数组实现区间更新. 对于每个区间,区间左端点+1,右端点的后一位-1,查询每个位置的覆盖次数 #include <cstdio> #include <cstring ...
- 搭建springmvc项目404,没扫描到包
搭建简单项目完成之后,曾经出现过一个问题 跳转报了404,控制台忘了没留啊... 反正意思就是说我配置有问题,导致没有扫描到注释的类 <context:component-scan base-p ...
- 学习go语言第一天
今天先下载了go语言,FQ去下载的,一开始想用eclipse,然后下载了go插件,结果出现错误,我英语水平有限,就换了liteIDE,感觉还不错,go语言环境变量因为我是msi安装的,好像可以不用自己 ...
- vue.$refs 的用法
官网给出的解释是: 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上. 1.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素; 2.如果用在子组件上,引用 ...
- 《R语言实战》读书笔记--第三章 图形初阶(一)
3.1使用图形 可以使用pdf等函数将图形直接保存在文件中.在运用attach和detach函数的使用中经常出现错误,比如命名重复的问题,所以,应该尽量避免使用这两个函数. plot是一般的画图函数, ...
- linux 某个路径创建快捷方式
ln -s /绝对路径 桌面名称