-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10171.cpp
104 lines (89 loc) · 2.33 KB
/
P10171.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
typedef PI WeightAndVertex;
#define INF numeric_limits<int>::max()
/*
Dijkstra for shortest path to all targets.
N vertices
*/
void dijkstra(int N, vector<WeightAndVertex> *adjacencyLists, int source, int *minPath) {
bool *visited = new bool[N];
FORI(N) {
visited[i] = false;
minPath[i] = INF;
}
minPath[source] = 0;
set<WeightAndVertex> Q; // To visit
Q.insert(WeightAndVertex(0, source));
while(!Q.empty()) {
const WeightAndVertex p = *Q.begin();
Q.erase(Q.begin());
const int from = p.second;
if(visited[from])
continue;
const int weight = p.first;
// perform relaxation:
visited[from] = true;
FORIT(vector<WeightAndVertex>, adjacencyLists[from]) {
int neighbour = it->second;
if(visited[neighbour])
continue;
int neighbourWeight = weight + it->first;
if(minPath[neighbour] <= neighbourWeight)
continue;
minPath[neighbour] = neighbourWeight;
Q.insert(WeightAndVertex(neighbourWeight, neighbour));
}
}
delete[] visited;
}
int main() {
vector<WeightAndVertex> neighboursY[26], neighboursM[26];
string a, b, c, d;
int sM, sY, w, minPathY[26], minPathM[26];
while(true) {
GI(N);
if(N == 0)
return 0;
FORI(26) {
neighboursY[i].clear();
neighboursM[i].clear();
}
FORI(N) {
cin >> a >> b >> c >> d >> w;
bool uni = b[0] == 'U';
int s = c[0]-'A';
int t = d[0]-'A';
if(a[0] == 'Y') {
neighboursY[s].push_back(WeightAndVertex(w, t));
if(!uni)
neighboursY[t].push_back(WeightAndVertex(w, s));
}
else {
neighboursM[s].push_back(WeightAndVertex(w, t));
if(!uni)
neighboursM[t].push_back(WeightAndVertex(w, s));
}
}
cin >> a >> b;
sY = a[0]-'A';
sM = b[0]-'A';
dijkstra(26, neighboursM, sM, minPathM);
dijkstra(26, neighboursY, sY, minPathY);
// Output:
int bestValue = INF;
FORI(26) {
if(minPathM[i] < INF && minPathY[i] < INF && minPathM[i]+minPathY[i] < bestValue)
bestValue = minPathM[i]+minPathY[i];
}
if(bestValue == INF) {
cout << "You will never meet." << endl;
}
else {
cout << bestValue;
FORI(26) {
if(minPathM[i] < INF && minPathY[i] < INF && minPathM[i]+minPathY[i] == bestValue)
cout << " " << (char)(i+'A');
}
cout << endl;
}
} // while true
} // int main