(寒假集训) Piggyback(最短路)
2017-01-10 20:28
357 查看
Piggyback
时间限制: 1 Sec 内存限制: 64 MB提交: 3 解决: 3
[提交][状态][讨论版]
题目描述
Bessieand 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.
输入
Thefirst 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.
输出
Asingle 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+5; const int M = 4e6+5; int n,m,k,tot=0,sum,ans,cnt ,b,e,p; int vis ; int dis [3],head ; 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==0)ss=n; else ss=s; for(int i=0;i<N;i++)dis[i][s]=inf; met(vis,0); dis[ss][s]=0; vis[ss]=1; queue<int>q; q.push(ss); while(!q.empty()){ int t=q.front();q.pop(); vis[t]=0; for(int i=head[t];i!=-1;i=edg[i].next){ int v=edg[i].to; if(dis[v][s]>dis[t][s]+1){ dis[v][s]=dis[t][s]+1; if(!vis[v])q.push(v),vis[v]=1; } } } } int main(){ met(head,-1); 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(0); spfa(1); spfa(2); int ans=inf; for(int i=1;i<=n;i++){ //printf("%d %d %d\n",dis[i][0],dis[i][1],dis[i][2]); int s=b*dis[i][1]+e*dis[i][2]+p*dis[i][0]; ans=min(ans,s); } printf("%d\n",ans); return 0; }
相关文章推荐
- 2013寒假ACM集训_最短路
- SDUT_2015寒假集训_最短路_A-最短路(Floyd)
- (寒假集训)Roadblock(最短路)
- HLJU14级寒假集训之最短路专场
- SDUT_2015寒假集训_动规递推_C-彼岸
- NIT ACM 2014 寒假集训#1 初阶组
- 寒假集训小记
- 寒假集训.Multiplying by Rotation
- 2018寒假集训-紫皮书-全排列
- 寒假集训三, Lake Counting ,DFS POJ-2386
- 【2018寒假集训 Day1】【位运算】桐桐的运输方案
- 2017年寒假集训分组测试赛2 Ranklist
- 2018寒假福州集训记Day1
- 2017暑假集训 div1 最短路(3)
- SDUT_2015寒假集训_结构体练习_E-小泉的难题
- SDUT_2015寒假集训_背包_F-小P寻宝记——粗心的基友
- 寒假集训.Rope
- 2015寒假集训--dp--数字三角形问题
- ACM暑假集训第二章——最短路
- 2018寒假集训-紫皮书-全排列