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. mysql一机多实例安装记录

    因为想研究mycat,所以需要安装多个mysql实例进行研究.限于没有多于计算机,只能在本机安装了.通过mysql文档,自己琢磨着安装成功! 目录结构如下: 其中one和two文件夹用来模拟数据库分库 ...

  2. flask-ssti,xss的防止

    源码 import uuid from flask import Flask, request, make_response, session,render_template, url_for, re ...

  3. 添加selenium对应的jar包至pom.xml

    1.进入https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java,点开相应的版本 2.复制图中选中的代码,粘贴至 ...

  4. HDU 4750 Count The Pairs (离线并查集)

    按边从小到大排序. 对于每条边(from, to, dist),如果from和to在同一个集合中,那么这条边无意义,因为之前肯定有比它更小的边连接了from和to. 如果from和to不属于同一个集合 ...

  5. [译]如何去除pandas dataframe里面的Unnamed的列?

    原文来源: https://stackoverflow.com/questions/43983622/remove-unnamed-columns-in-pandas-dataframe 问:我有一个 ...

  6. resharper激活

    1.解压后点击64位系统的IntelliJIDEALicenseServer_windows_amd64.exe      32位点击IntelliJIDEALicenseServer_windows ...

  7. poj1273 网络流入门题 dinic算法解决,可作模板使用

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 62078   Accepted: 2384 ...

  8. jQuery选择器示例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. openssl unicode编译以及VC++2015环境下的问题

    这几天需要使用openssl,前期本机上保存的目录不知道哪天整理的时候删除了,索性下载最新的自己编译一下: 在最新版的openssl(openssl-1.0.2e),编译过程中出现了很多问题,这里主要 ...

  10. Oracle 根据逗号分隔字符串 同时记录一波坑

    报表需要过滤掉不需要的数据,由于报表是根据零件编号来统计,需要过滤掉不合格品,只能根据关联的物料编码(零件编号)来过滤,只能通过not in来过滤,但是天真的我却用下面代码来当子查询: b.part_ ...