1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include<bits/stdc++.h>
using namespace std;
template<typename T>void read(T&x){x=0;int fl=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') fl=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}x*=fl;} template<typename T,typename...Args>inline void read(T&t,Args&...args){read(t);read(args...);}
typedef long long LL; typedef vector<int> vi; typedef pair<int, int> pii; #define mp(x, y) make_pair(x, y) #define pb(x) emplace_back(x)
const int N = 200500; const int M = 800005; const LL INF = 1e16;
struct Edge{ int nxt, to; LL dis; }edge[M<<1]; int head[N], edgeNum; void addEdge(int from, int to, LL dis){ edge[++edgeNum] = (Edge){head[from], to, dis}; head[from] = edgeNum; } inline void ae(int from, int to, LL dis){ addEdge(from, to, dis), addEdge(to, from, dis); }
namespace SP{ int n; LL dis[N]; void dijkstra(int s){ for(int i = 1; i <= n; i++) dis[i] = INF; priority_queue< pair<LL, int> > q; dis[s] = 0; q.push(make_pair(-dis[s], s)); while(!q.empty()){ auto cur = q.top(); q.pop(); if(-cur.first > dis[cur.second]) continue; for(int i = head[cur.second]; i; i = edge[i].nxt){ if(dis[edge[i].to] > dis[cur.second] + edge[i].dis){ dis[edge[i].to] = dis[cur.second] + edge[i].dis; q.push(make_pair(-dis[edge[i].to], edge[i].to)); } } } } }
int T, n, m, s, t, x; char c[N];
int main(){ for(read(T); T; T--){ read(n, m, s, t, x); edgeNum = 0; for(int i = 1; i <= 2 * n + 2; i++) head[i] = 0;
scanf("%s", c+1); for(int i = 1; i <= m; i++){ int u, v; LL d; read(u, v, d); if(c[u] == 'L'){ if(c[v] == 'L') ae(u, v, d); else if(c[v] == 'R') ae(u, v+n, d+x); else ae(u, v, d), ae(u, v+n, d+x); } else if(c[u] == 'R'){ if(c[v] == 'R') ae(u+n, v+n, d); else if(c[v] == 'L') ae(u+n, v, d+x); else ae(u+n, v, d+x), ae(u+n, v+n, d); } else if(c[u] == 'M'){ if(c[v] == 'L') ae(u, v, d), ae(u+n, v, d+x); else if(c[v] == 'R') ae(u, v+n, d+x), ae(u+n, v+n, d); else ae(u, v, d), ae(u, v+n, d+x), ae(u+n, v, d+x), ae(u+n, v+n, d); } } int S = 2 * n + 1, T = 2 * n + 2; addEdge(S, s, c[s] == 'R' ? INF : 0), addEdge(S, s+n, c[s] == 'L' ? INF : 0); addEdge(t, T, c[t] == 'R' ? INF : 0), addEdge(t+n, T, c[t] == 'L' ? INF : 0); SP::n = 2 * n + 2; SP::dijkstra(S); printf("%lld\n", SP::dis[T]); } return 0; }
|