-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10171.cpp
94 lines (74 loc) · 1.85 KB
/
uva-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
//uva 10171
//Meeting Prof. Miguel ...
#include <iostream>
#include <climits>
#include <vector>
#include <map>
#include <set>
using namespace std;
int main(void)
{
int n = 0;
while(true){
cin >> n;
if(n == 0)
break;
map <char, map <char, int> > YGraph;
map <char, map <char, int> > MGraph;
for(char i = 'A'; i <= 'Z'; ++i)
for(char j = 'A'; j <= 'Z'; ++j){
YGraph[i][j] = INT_MAX;
MGraph[i][j] = INT_MAX;
}
for(int i = 0; i < n; ++i){
char state, direct, n1, n2;
int cost;
cin >> state >> direct >> n1 >> n2 >> cost;
if(state == 'Y'){
YGraph[n1][n2] = cost;
if(direct == 'B')
YGraph[n2][n1] = cost;
}
else {
MGraph[n1][n2] = cost;
if(direct == 'B')
MGraph[n2][n1] = cost;
}
}
char sourceY, sourceM;
cin >> sourceY >> sourceM;
for(char i = 'A'; i <= 'Z'; ++i){
YGraph[i][i] = 0;
MGraph[i][i] = 0;
}
for(char k = 'A'; k <= 'Z'; ++k)
for(char i = 'A'; i <= 'Z'; ++i)
for(char j = 'A'; j <= 'Z'; ++j){
if(YGraph[i][k] != INT_MAX && YGraph[k][j] != INT_MAX)
YGraph[i][j] = min(YGraph[i][j], YGraph[i][k] + YGraph[k][j]);
if(MGraph[i][k] != INT_MAX && MGraph[k][j] != INT_MAX)
MGraph[i][j] = min(MGraph[i][j], MGraph[i][k] + MGraph[k][j]);
}
int minCost = INT_MAX;
set <char> destination;
for(char i = 'A'; i <= 'Z'; ++i)
if(YGraph[sourceY][i] != INT_MAX && MGraph[sourceM][i] != INT_MAX){
if(YGraph[sourceY][i] + MGraph[sourceM][i] < minCost){
minCost = YGraph[sourceY][i] + MGraph[sourceM][i];
destination.clear();
destination.insert(i);
}
else if(YGraph[sourceY][i] + MGraph[sourceM][i] == minCost)
destination.insert(i);
}
if(destination.empty())
cout << "You will never meet." << endl;
else{
cout << minCost;
for(auto a : destination)
cout << ' ' << a;
cout << endl;
}
}
return 0;
}