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
题意:给出一个NxM的有权无向图,起点s,目标是t。点分成左点右点和中间点,到左点的时候必须用左手,到右点的时候必须用右手,中间点没有特殊要求。每次切换左右手都要花费额外的时间,询问起点到终点的最短路。
思路:把每个点拆成两个点,左点拆成两个左点,右点拆成两个右点,中间点拆成一左一右两个点,然后跑dijsktra。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 5;
const ll inf = 1e18;
int n, m, s, t, cost;
string Str;
ll d[N];
int vis[N];
char wjm[N];
int h[N * 3];
int temp = 0;
struct Node
{
int u;
int v;
int w;
int next;
} edge[N * 8];
void add(int u, int v, int w)
{
edge[temp].u = u;
edge[temp].v = v;
edge[temp].w = w;
edge[temp].next = h[u];
h[u] = temp++;
edge[temp].u = v;
edge[temp].v = u;
edge[temp].w = w;
edge[temp].next = h[v];
h[v] = temp++;
}
struct Qnode
{
int v;
ll w;
bool operator < (const Qnode &r) const
{
return w > r.w;
}
};
void Dij(int s)
{
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= 2 * n; i++)
{
d[i] = inf;
}
priority_queue<Qnode> q;
d[s] = 0;
q.push({ s,0 });
Qnode tmp;
while (!q.empty())
{
tmp = q.top();
q.pop();
int u = tmp.v;
if (vis[u])
{
continue;
}
vis[u] = 1;
for (int i = h[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
int tt;
if (wjm[u] == wjm[v])
{
tt = 0;
}
else
{
tt = 1;
}
if (!vis[v] && d[v] > d[u] + edge[i].w + tt * cost)
{
d[v] = d[u] + edge[i].w + tt * cost;
q.push({ v,d[v] });
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
cin >> n >> m >> s >> t >> cost;
for (int i = 1; i <= n * 2; i++)
{
h[i] = -1;
}
cin >> Str;
for (int i = 1; i <= n; i++)
{
if (Str[i - 1] == 'L')
{
wjm[i] = 'L';
wjm[i + n] = 'L';
}
else if (Str[i - 1] == 'R')
{
wjm[i] = 'R';
wjm[i + n] = 'R';
}
else
{
wjm[i] = 'L';
wjm[i + n] = 'R';
}
}
temp = 0;
for (int i = 0; i < m; i++)
{
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(u + n, v, w);
add(u + n, v + n, w);
add(u, v + n, w);
}
ll ans = 1e18;
Dij(s);
ans = min(ans, d[t]);
ans = min(ans, d[t + n]);
Dij(s + n);
ans = min(ans, d[t]);
ans = min(ans, d[t + n]);
cout << ans << endl;
}
}