2020杭电多校第四场 Deliver the Cake(二维最短路)

Problem Description
It is Zhang3’s birthday! Zhang3 has bought a birthday cake and now it’s time to take it home.

There are n villages, labeled 1,2,…,n. There are m bidirectional roads, the ith of which connects village ai, bi and it is di meter(s) long.

The bakery locates at village s and Zhang3’s home locates at village t. So Zhang3 wants to carry the cake from s to t. She can carry the cake either with her left hand or with her right hand. She can switch to the other hand during the trip, which takes extra x second(s) each time (when she’s performing this action, she must stay in her place). Switching is allowed at any place, including the middle of the roads. She can do this as many times as she like, or don’t do it at all.

Some villages are LEFT. When Zhang3 is at a LEFT village, she must carry the cake with her left hand at the moment. In the same way, some other villages are RIGHT, she must carry with her right hand when she’s at these villages. The rest villages are called MIDDLE. There’s no special rules at MIDDLE villages.

Zhang3 can start and finish with any hand carrying the cake. However, if s or t is not MIDDLE, their special rules must be followed.

Please help Zhang3 find a way to take the cake home, with the minimum amount of spent time.

Input
The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow.

For each test case, the first line contains five integers n,m,s,t,x(1≤n≤105,1≤m≤2×105,1≤x≤109), representing the number of villages, the number of roads, the bakery’s location, home’s location, and the time spent for each switching.

The next line contains a string of length n, describing the type of each village. The ith character is either L representing village i is LEFT, or M representing MIDDLE, or R representing RIGHT.

Finally, m lines follow, the ith of which contains three integers ai,bi,di(1≤di≤109), denoting a road connecting village ai and bi of length di.

It is guaranteed that t can be reached from s.

The sum of n in all test cases doesn’t exceed 2×105. The sum of m doesn’t exceed 4×105.

Output
For each test case, print a line with an integer, representing the minimum amount of spent time (in seconds).

Sample Input
1
3 3 1 3 100
LRM
1 2 10
2 3 10
1 3 100

Sample Output
100

Source
2020 Multi-University Training Contest 4

题意:
每个边有边权,且有些点要求右手,有些要求左手,有些没要求。换手有时间。
求1到n的最短路

思路:
二维最短路就好了。
定义 d [ i ] [ 0 / 1 ] d[i][0/1] 代表到达第i个点,且为左手(右手)的最短时间。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include <map>

using namespace std;
typedef long long ll;

typedef long long ll;
const int maxn = 1e5 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;

char str[maxn];
ll d[maxn][2];
int vis[maxn][2];
int n,m,s,t;
ll cost;

vector<pair<int,ll> >G[maxn];

struct Node {
    ll d;
    int x,id;
    bool operator < (const Node&res) const {
        return d > res.d;
    }
};

ll dijkstra() {
    priority_queue<Node>q;
    for(int i = 1;i <= n;i++) {
        d[i][0] = d[i][1] = INF; //0为左手,1为右手
        vis[i][0] = vis[i][1] = 0;
    }
    
    if(str[s] == 'L') {
        d[s][0] = 0;
        q.push({d[s][0],s,0});
    } else if(str[s] == 'R') {
        d[s][1] = 0;
        q.push({d[s][1],s,1});
    } else {
        d[s][1] = d[s][0] = 0;
        q.push({d[s][0],s,0});
        q.push({d[s][1],s,1});
    }
    
    while(!q.empty()) {
        int x = q.top().x,id = q.top().id;q.pop();
        if(vis[x][id]) continue;
        vis[x][id] = 1;
        for(int i = 0;i < G[x].size();i++) {
            int v = G[x][i].first;
            ll w = G[x][i].second;
            if(str[v] == 'L') {
                if(id == 0) {
                    if(d[v][id] > d[x][id] + w) {
                        d[v][id] = d[x][id] + w;
                        q.push({d[v][id],v,id});
                    }
                } else {
                    if(d[v][id ^ 1] > d[x][id] + w + cost) {
                        d[v][id ^ 1] = d[x][id] + w + cost;
                        q.push({d[v][id ^ 1],v,id ^ 1});
                    }
                }
            } else if(str[v] == 'R') {
                if(id == 0) {
                    if(d[v][id ^ 1] > d[x][id] + w + cost) {
                        d[v][id ^ 1] = d[x][id] + w + cost;
                        q.push({d[v][id ^ 1],v,id ^ 1});
                    }
                } else {
                    if(d[v][id] > d[x][id] + w) {
                        d[v][id] = d[x][id] + w;
                        q.push({d[v][id],v,id});
                    }
                }
            } else if(str[v] == 'M') {
                if(d[v][id] > d[x][id] + w) {
                    d[v][id] = d[x][id] + w;
                    q.push({d[v][id],v,id});
                }
                if(d[v][id ^ 1] > d[x][id] + w + cost) {
                    d[v][id ^ 1] = d[x][id] + w + cost;
                    q.push({d[v][id ^ 1],v,id ^ 1});
                }
            }
        }
    }
    
    return min(d[t][0],d[t][1]);
}

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        scanf("%d%d%d%d%lld",&n,&m,&s,&t,&cost);
        scanf("%s",str + 1);
        for(int i = 1;i <= n;i++) {
            G[i].clear();
        }
        for(int i = 1;i <= m;i++) {
            int x,y;
            ll z;
            scanf("%d%d%lld",&x,&y,&z);
            G[x].push_back({y,z});
            G[y].push_back({x,z});
        }
        printf("%lld\n",dijkstra());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/107828252
今日推荐