题目大意:给出一张无向图,有两个人,分别在1和2,他们要到n,一个人走的消耗是c1,c2,两个人一起走是c3,问最少消耗。
思路:题中说是可以一起走,而不是必须一起走,所以之需要看这两个人到所有点的距离,还有每个点到终点的距离,之后枚举从那个点开始一起走,求一下最小值就可以了。
CODE:
#define _CRT_SECURE_NO_WARNINGS #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 100010 #define INF 0x3f3f3f3f using namespace std; int c1,c2,futari; int points,edges; int head[MAX],total; int _next[MAX],aim[MAX]; inline void Add(int x,int y) { _next[++total] = head[x]; aim[total] = y; head[x] = total; } int dis1[MAX],dis2[MAX],dis_final[MAX]; void BFS(int start,int arr[]) { static queue<int> q; static bool v[MAX]; memset(v,false,sizeof(v)); q.push(start); v[start] = true; while(!q.empty()) { int x = q.front(); q.pop(); for(int i = head[x]; i; i = _next[i]) { if(v[aim[i]]) continue; v[aim[i]] = true; arr[aim[i]] = arr[x] + 1; q.push(aim[i]); } } } int main() { cin >> c1 >> c2 >> futari >> points >> edges; for(int x,y,i = 1; i <= edges; ++i) { scanf("%d%d",&x,&y); Add(x,y),Add(y,x); } BFS(1,dis1); BFS(2,dis2); BFS(points,dis_final); int ans = INF; for(int i = 1; i <= points; ++i) ans = min(ans,c1 * dis1[i] + c2 * dis2[i] + futari * dis_final[i]); cout << ans << endl; return 0; }
BZOJ 3891 Usaco2014 Dec Piggy Back BFS
原文地址:http://blog.csdn.net/jiangyuze831/article/details/43971115