(寒假集训) Piggyback(最短路)
Posted 贱人方
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(寒假集训) Piggyback(最短路)相关的知识,希望对你有一定的参考价值。
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.
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.
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+5; const int M = 4e6+5; int n,m,k,tot=0,sum,ans,cnt[N],b,e,p; int vis[N]; int dis[N][3],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==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; }
以上是关于(寒假集训) Piggyback(最短路)的主要内容,如果未能解决你的问题,请参考以下文章