D

https://vjudge.net/contest/386568#problem/D

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

There are nn villages, labeled 1,2,,n1,2,…,n. There are mm bidirectional roads, the ithith of which connects village aiai, bibi and it is didi meter(s) long.

The bakery locates at village ss and Zhang3's home locates at village tt. So Zhang3 wants to carry the cake from ss to tt. 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 xx 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 ss or tt 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.

InputThe first line of the input gives the number of test cases, T(1T100)T(1≤T≤100). TT test cases follow.

For each test case, the first line contains five integers n,m,s,t,x(1n105,1m2×105,1x109)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 nn, describing the type of each village. The ithith character is either LL representing village ii is LEFT, or MM representing MIDDLE, or RR representing RIGHT.

Finally, mm lines follow, the ithith of which contains three integers ai,bi,di(1di109)ai,bi,di(1≤di≤109), denoting a road connecting village aiai and bibi of length didi.

It is guaranteed that tt can be reached from ss.

The sum of nn in all test cases doesn't exceed 2×1052×105. The sum of mm doesn't exceed 4×1054×105.
OutputFor 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

Sponsor

参考代码:https://www.cnblogs.com/chuliyou/p/13416544.html

思路:

  区别左手右手

  分成两个方向用dijsktra?(未解决

附草稿代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
//#define sort(a,n,int) sort(a,a+n,less<int>())

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=4e5+10;
const int mod=1e9+7;

int n, m, s, t, xx;
struct Node
{
	int head;
	int ver;
	int edge;
	int Next;
}line[N*2];
bool v[N*2];
long long d[N*2];
int hand[N*2];

int tot = 0;
priority_queue <pair<long long , int> >q;
void add(int x , int y , int z)
{
	tot += 1;
    line[tot].ver = y;
    line[tot].edge = z;
    line[tot].Next = line[x].head;
    line[x].head = tot;
}
long long dijkstra(int s, int t)
{
	long long ans = 1e18;
    for(int i = 1; i<=2*n;i++)
	{
	 	d[i] = 1e18;
    }
	memset(v , 0 , sizeof(v));
    d[s] = 0;
    q.push(make_pair(0 , s));
    int i = 0 , j = 0;
    while(q.size())
    {
        int x = q.top().second;
        q.pop();
        if(v[x])
		{
		 	continue;
        }
		v[x] = 1;
        for(i = line[x].head;i != 0; i=line[i].Next)
        {
            int y = line[i].ver , z = line[i].edge;
            if(hand[x] != hand[y])
			{
			 	z += xx;//是否要换手 
            }if(d[y] > d[x]+z)
            {
                d[y] = d[x] + 1ll*z;
                q.push(make_pair(-d[y] , y));
            }
        }
    }
	return min(d[t] , d[t + n]);
}
signed main()
{
    int T = 0;
    cin >> T;
    while(T--)
    {
    	tot = 0;
    	for(int i = 1;i<=2*n;i++)
		{
		 	line[i].head = 0;
		}
    	cin >> n >> m >> s >> t >> xx;
    	string S;
    	cin >> s;
    	for(int i = 0;i<S.size();i++)
    	{
    		if(S[i] == 'L')
    		{
    			hand[i+1] = 0;
	 			hand[i+1+n] = 0;
			}
			if(S[i] == 'M')
			{
				hand[i+1] = 0;
				hand[i+1+n] = 1;
			}
			if(S[i] == 'R')
    		{
    			hand[i+1] = 1;
				hand[i+1+n] = 1;
			}
		}
		for(int i = 1; i <= m; i++)
		{
			int u = 0 , v = 0 , w = 0;
			cin >> u >> v >> w;
			add(u , v , w);
			add(v , u , w);
			add(u+n , v , w);
			add(v , u+n , w);
			add(u , v+n , w);
			add(v+n , u , w);
			add(u+n , v+n , w);
			add(v+n , u+n , w);
		}
		cout << min(dijkstra(s,t) , dijkstra(s+n, t)) <<endl;
	}
    return 0;
}

  

原文地址:https://www.cnblogs.com/SutsuharaYuki/p/13429565.html