10171

#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
#include <map>

#define INF 1000000000

using namespace std;

int grY[30][30];
int grO[30][30];

int main()
{
	int N,KK,st,dst;
	char people,roadtyp,ct1,ct2;
	char start,dest;
	int energy;
//	ofstream out;
//	out.open("out.txt");
	while(cin>>N)
	{
		if(N==0)
		break;
		for(int i=0;i<30;i++)
		{
			for(int j=0;j<30;j++)
			{
				if(i==j)
				{
					grY[i][j]=0;
					grO[i][j]=0;
				}
				else
				{
					grY[i][j]=INF;
					grO[i][j]=INF;
				}
			}
		}
		while(N--)
		{
			cin>>people>>roadtyp>>ct1>>ct2>>energy;

			if(people=='Y')
			{
				grY[(ct1-'A')+1][(ct2-'A')+1]=energy;
				if(roadtyp=='B')
					grY[(ct2-'A')+1][(ct1-'A')+1]=energy;
			}
			else
			{
				grO[(ct1-'A')+1][(ct2-'A')+1]=energy;
				if(roadtyp=='B')
					grO[(ct2-'A')+1][(ct1-'A')+1]=energy;
			}
		}

		for (int i = 0; i < 27; i++)
		{
			grY[i][i] = 0;
			grO[i][i] = 0;
		}
		cin>>start>>dest;
		st=(start-'A')+1;
		dst=(dest-'A')+1;

		for(int k=1;k<=26;k++)
		{
			for(int i=1;i<=26;i++)
			{
				for(int j=1;j<=26;j++)
				{
					if(grY[i][j]>grY[i][k]+grY[k][j])
						grY[i][j] = grY[i][k]+grY[k][j];
					if(grO[i][j]>grO[i][k]+grO[k][j])
						grO[i][j] = grO[i][k]+grO[k][j];
				}
			}
		}

		int MN = INF;
		for(int i=1;i<27;i++)
		{
			if(grY[st][i]==INF || grO[dst][i]==INF)
				continue;
			if(MN>grY[st][i]+grO[dst][i])
			{
				MN = grY[st][i]+grO[dst][i];
			}
		}
		if(MN==INF)
		{
			cout<<"You will never meet.\n";
			//out<<"You will never meet.\n";
		}
		else
		{
			cout<<MN;
			//out<<MN;

			for(int i=1;i<27;i++)
			{
				if(MN == (grY[st][i]+grO[dst][i]))
				{
					cout<<" "<<char((i-1)+'A');
					//out<<" "<<char((i-1)+'A')<<" ";
				}
			}
			cout<<"\n";
			//out<<"\n";
		}

	}
//	out.close();

	return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *