-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVA_10171_MeetingProfMiguel.cpp
133 lines (132 loc) · 2.56 KB
/
UVA_10171_MeetingProfMiguel.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
#include <stdio.h>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
char MMM[600], nodesN;
int find(char X)
{
for(int i=0; i<nodesN; i++)
{
if(MMM[i] == X)
{
return i;
}
}
return -1;
}
int main()
{
int roadsN, i, j, k, adjMatY[500][500], adjMatM[500][500];
int weight, firstN, secondN, INF=1000000, min2;
char age, direction, first, second;
vector<char> best;
scanf("%d",&roadsN);
while(roadsN != 0)
{
for(i=0; i<500; i++)
{
for(j=0; j<500; j++)
{
adjMatY[i][j] = INF;
adjMatM[i][j] = INF;
}
adjMatY[i][i] = 0;
adjMatM[i][i] = 0;
}
nodesN = 0;
for(i=0; i<roadsN; i++)
{
char temp = getchar();
scanf("%c %c %c %c %d",&age,&direction,&first,&second,&weight);
//printf("%c %c %c %c %d\n",age,direction,first,second,weight);
firstN = find(first);
if(firstN == -1)
{
MMM[nodesN] = first;
firstN = nodesN;
nodesN ++;
}
secondN = find(second);
if(secondN == -1)
{
MMM[nodesN] = second;
secondN = nodesN;
nodesN ++;
}
if(age == 'Y')
{
adjMatY[firstN][secondN] = min(adjMatY[firstN][secondN], weight);
if(direction == 'B')
{
adjMatY[secondN][firstN] = min(adjMatY[secondN][firstN], weight);
}
}
else if(age == 'M')
{
adjMatM[firstN][secondN] = min(adjMatM[firstN][secondN], weight);
if(direction == 'B')
{
adjMatM[secondN][firstN] = min(adjMatM[secondN][firstN], weight);
}
}
}
for(k=0; k<nodesN; k++)
{
for(i=0; i<nodesN; i++)
{
for(j=0; j<nodesN; j++)
{
adjMatY[i][j] = min(adjMatY[i][j], adjMatY[i][k]+adjMatY[k][j]);
adjMatM[i][j] = min(adjMatM[i][j], adjMatM[i][k]+adjMatM[k][j]);
}
}
}
getchar();
scanf("%c %c",&first,&second);
firstN = find(first);
secondN = find(second);
if(firstN == -1 && secondN == -1 && first == second)
{
printf("0 %c\n", first);
}
else if(firstN == -1 || secondN == -1)
{
printf("You will never meet.\n");
}
else
{
min2 = INF;
for(i=0; i<nodesN; i++)
{
if(adjMatY[firstN][i] + adjMatM[secondN][i] < min2)
{
min2 = adjMatY[firstN][i] + adjMatM[secondN][i];
best.clear();
best.push_back(MMM[i]);
}
else if(adjMatY[firstN][i] + adjMatM[secondN][i] == min2)
{
best.push_back(MMM[i]);
}
}
sort(best.begin(), best.end());
if(min2 == INF)
{
printf("You will never meet.\n");
}
else
{
printf("%d", min2);
for(i=0; i<best.size(); i++)
{
printf(" %c", best[i]);
}
printf("\n");
}
}
best.clear();
scanf("%d",&roadsN);
}
return 0;
}