Baidu Star (problem solution)

Today is Children's Day, and Xiaodu went to the park to play. There are N attractions in the park. It happened that Dudu Bear in the circle of friends was also playing in this park, so they agreed to go to N attractions together. The attraction number where Xiaodu is currently located is T, and the physical energy required to move from one attraction to a nearby attraction is TE. The attraction number where Dudu Bear is located is F, and the energy consumption for moving is FE. Good friends will be much happier when they are together, so if Xiaodu and Dudu bear move together (that is, move in the same position and in the same direction), their total consumption will be reduced by S at each step.
What is the minimum total consumption required for them to go to scenic spot N?

Format

Input format:

The three values ​​​​in the first row, TE, FE, S, respectively represent the consumption value of Xiaodu movement, the consumption value of Duduxiong movement, and the consumption reduction value of moving together. 1≤TE,FE,S≤40000,S≤TE+FE1≤TE,FE,S≤40000,S≤TE+FE; The
four values ​​in the second row, T, F, N, M, respectively represent the starting point of small degree , Dudu Bear’s starting point, target node, and total number of paths. 1≤T,F,N,M≤40000;
next M lines, each line contains two integers X and Y, representing two connected scenic spots. 1≤X,Y≤N.

Output format:

An integer, which is the minimum total consumption. If N cannot be reached, -1 is output.

Sample 1

Input: 
4 4 3 
1 2 8 8 
1 4 
2 3 
3 4 
4 7 
2 5 
5 6 
6 8 
7 8 

Output: 22
#include<bits/stdc++.h> 

using namespace std;
int te,fe,s;
int t,f,n,m;
const int maxn=40005;
vector<int> v[maxn];
int p1[maxn],p2[maxn],d[maxn];
void bfs(int start,int val[]){
    for(int i=1;i<=n;i++) val[i]=n+1;
    queue<int> Q;
    Q.push(start);
    val[start]=0;
    while(!Q.empty()){
        int cur=Q.front();
        Q.pop();
        for(int i=0;i<v[cur].size();i++){
            int d=v[cur][i];
            if(val[d]>val[cur]+1){
                Q.push(d);
                val[d]=val[cur]+1;
            }
        }
    }
}

int main( )
{
    scanf("%d %d %d",&te,&fe,&s);
    scanf("%d %d %d %d",&t,&f,&n,&m);
    for(int i=0;i<m;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        v[a].push_back(b);
        v[b].push_back(a);
    }
    bfs(t,p1); bfs(f,p2); bfs(n,d);
    if(p1[n]==n+1||p2[n]==n+1){
        printf("-1");
    }else{
        int res=p1[n]*te+p2[n]*fe;
        for(int i=1;i<n;i++){
            int cur=p1[i]*te+p2[i]*fe+d[i]*(te+fe-s);
            res=min(res,cur);
        }
        printf("%d",res);
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_71356343/article/details/132892622