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. Python 3基础教程7-if语句

    前面文章介绍的循环语句,这里开始介绍控制语句.直接看下面的demo.py例子 # 这里介绍 if语句 x = 5y = 8z = 4s = 5 if x < y: print('x is les ...

  2. ThreadLocal 学习

    JDK 1.2版本就已经提供了java.lang.ThreadLocal.其为多线程程序的并发问题提供了一种新的思路.使用该工具类可以简洁地编写出优美的多线程程序. 当使用ThreadLocal维护变 ...

  3. CentOS下Apache虚拟主机配置

    通过phpinfo可以看到Apache安装的目录 修改配置文件,首先将配置文件备份 编辑httpd.conf,并找到虚拟路径配置的部分 vi httpd.conf 在vi下先按esc在键入 :/vho ...

  4. SQL 基础笔记(一)

    本笔记整理自<SQL 基础教程>.<MySQL 必知必会>和网上资料.个人笔记不保证正确. 一.基础 SQL,即结构化查询语言,是为访问与操作关系数据库中的数据而设计的语言. ...

  5. zoj 2110 Tempter of the Bone (dfs)

    Tempter of the Bone Time Limit: 2 Seconds      Memory Limit: 65536 KB The doggie found a bone in an ...

  6. [洛谷P4074][WC2013]糖果公园

    题目大意:给一棵$n$个节点的树,每个点有一个值$C_i$,每次询问一条路径$x->y$,求$\sum\limits_{c}val_c\times \sum\limits_{i=1}^{cnt_ ...

  7. [poj] 3347 Kadj Square || 计算几何的“线段覆盖”

    原题 多组数据,给出n个正方形的边长,使他们以45度角倾斜的情况下最靠左(在第一象限内),如图.求从上看能看到哪几个完整的正方形. 借鉴于https://www.cnblogs.com/Ritchie ...

  8. [codeforces] 498D Traffic Jams in th Land

    原题 简单的线段树问题. 对于题目中,a[i]的范围是2~6,我们仔细思考可以得出第0秒和第60秒是一样的(因为2~6的最小公倍数是60,),然后我们可以建一个线段树,里面记录0~59秒时刻开始通过这 ...

  9. php56升级后php7 mcrypt_encrypt 报错

    mcrypt_encrypt(MCRYPT_BLOWFISH, $passphrase, $data, MCRYPT_MODE_CBC, $iv); openssl_encrypt($data, &q ...

  10. 如何禁用Eclipse的Validating

    使用Eclipse开发项目,在加载项目.刷新项目.修改了某个代码的时候,经常出现Eclipse正在Validating的提示.项目比较大文件(js)较多的情况下,甚至出现Validating几分钟的盛 ...