-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10171.cpp
136 lines (128 loc) · 2.55 KB
/
10171.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
10171 - Meeting Prof. Miguel...
*/
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
typedef long long ll;
map<char,ll>mapp;
///y for younger and o for older
ll y[30][30],o[30][30];
void y_warshall()
{
for(int k=1; k<=26; k++)
{
for(int i=1; i<=26; i++)
{
for(int j=1; j<=26; j++)
{
y[i][j]=min(y[i][j],y[i][k]+y[k][j]);
}
}
}
}
void o_warshall()
{
for(int k=1; k<=26; k++)
{
for(int i=1; i<=26; i++)
{
for(int j=1; j<=26; j++)
{
o[i][j]=min(o[i][j],o[i][k]+o[k][j]);
}
}
}
}
int main()
{
for(int i='A'; i<='Z'; i++)
{
mapp[i]=i-'A'+1;
}
///freopen("input.txt", "r", stdin);
///freopen("output.txt", "w", stdout);
ll n,c;
char a,b,u,v;
while(cin>>n && n!=0)
{
for(int i=1; i<=26; i++)
{
for(int j=1; j<=26; j++)
{
y[i][j]=INF;
o[i][j]=INF;
}
}
for(int i=1; i<=n; i++)
{
cin>>a>>b>>u>>v>>c;
ll posx = mapp[u];
ll posy = mapp[v];
if(a=='Y')
{
y[posx][posy]=c;
if(b=='B')
y[posy][posx]=c;
}
if(a=='M')
{
o[posx][posy]=c;
if(b=='B')
o[posy][posx]=c;
}
}
cin>>u>>v;
int younger_start=mapp[u];
int older_start=mapp[v];
y[younger_start][younger_start]=0;
o[older_start][older_start]=0;
y_warshall();
o_warshall();
///now all y[][] will store apsp for younger
///and all o[][] will store apsp for older
ll ans = INF;
for(int i=1; i<=26; i++)
{
ans = min(ans,y[younger_start][i]+o[older_start][i]);
}
if(ans==INF)
{
cout<<"You will never meet."<<endl;
continue;
}
vector<char>temp;
for(int i=1; i<=26; i++)
{
if(ans==y[younger_start][i]+o[older_start][i])
{
temp.push_back(i-1+'A');
}
}
sort(temp.begin(),temp.end());
cout<<ans;
for(char it : temp)
{
cout<<" "<<it;
}
cout<<endl;
}
return 0;
}
/*
Sample Input
4
Y U A B 4
Y U C A 1
M U D B 6
M B C D 2
A D
2
Y U A B 10
M U C D 20
A D
0
Sample Output
10 B
You will never meet.
*/