-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10702.cpp
134 lines (118 loc) · 3.23 KB
/
P10702.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
/*
OK. Let's try to improve this: Improce the computation of tmpProfit.
Attempt 1:
Stop early by sorting and using "highestProfitGoingToCity" and "sortedCities".
=> Time from 0.030 -> 0.010 (Only 6 others here. None at 0.000).
Attempt 2:
Only update profit(key) in "sortedCities" => less "sorting" in each step.
=> No improvement :(
Attempt 3:
Use sorted in-edges (Code block 2 below)
*/
/*
int main() {
int C, S, E, T, city;
int profitMatrix[100][100], profit[100], tmpProfit[100], highestProfitGoingToCity[100];
PI sortedCities[100]; // value -> city
while(true) {
scanf("%d %d %d %d", &C, &S, &E, &T);
if(0 == (C | S | E | T))
return 0;
--S;
FORI(C) {
sortedCities[i] = PI(-1000000000, i);
highestProfitGoingToCity[i] = 0;
}
sortedCities[S] = PI(0, S);
// C cities. S start. E ends. T visiting.
FORI(C) {
profit[i] = -1000000000; // Indicating no profit.
FORJ(C) {
scanf("%d", &profitMatrix[j][i]);
if(profitMatrix[j][i] > highestProfitGoingToCity[j])
highestProfitGoingToCity[j] = profitMatrix[j][i];
}
}
profit[S] = 0;
// Compute profit:
FORK(T) { // Walk T times:
sort(sortedCities, sortedCities+C);
FORJ(C) { // Update each city
tmpProfit[j] = 0;
for(int xi = C-1; xi >= 0 && tmpProfit[j] < sortedCities[xi].first + highestProfitGoingToCity[j]; --xi) {
int i = sortedCities[xi].second;
int tryProfit = profit[i] + profitMatrix[j][i];
if(tryProfit > tmpProfit[j])
tmpProfit[j] = tryProfit;
}
}
swap(profit, tmpProfit);
FORJ(C) {
sortedCities[j].first = profit[sortedCities[j].second];
}
} // FORK(T) Walk T times
int bestProfit = 0;
FORI(E) {
scanf("%d", &city);
--city;
if(profit[city] > bestProfit)
bestProfit = profit[city];
}
printf("%d\n", bestProfit);
}
}
*/
int main() {
int C, S, E, T, city;
int profitMatrix[100][100], profit[100], tmpProfit[100];
PI sortedInEdges[100][100]; // value -> from city
while(true) {
scanf("%d %d %d %d", &C, &S, &E, &T);
if(0 == (C | S | E | T))
return 0;
--S;
// C cities. S start. E ends. T visiting.
FORI(C) {
FORJ(C) {
scanf("%d", &profitMatrix[j][i]);
if(i == S) {
profit[j] = profitMatrix[j][i]; // Note: Becomes 0 for S.
}
sortedInEdges[j][i] = PI(-profitMatrix[j][i], i); // '-' to reverse sort.
}
}
--T;
// Perform sortings:
FORJ(C) {
sort(sortedInEdges[j], sortedInEdges[j]+C);
}
// Compute profit:
int maxProfit = 0;
FORK(T) { // Walk T times:
FORJ(C) {
if(profit[j] > maxProfit)
maxProfit = profit[j];
} // FORJ
FORJ(C) { // Check each city
tmpProfit[j] = 0;
for(int xi = 0; xi < C && tmpProfit[j] < maxProfit - sortedInEdges[j][xi].first; ++xi) {
int i = sortedInEdges[j][xi].second;
int tryProfit = profit[i] - sortedInEdges[j][xi].first;
if(tryProfit > tmpProfit[j])
tmpProfit[j] = tryProfit;
} // for xi
} // FORJ
FORJ(C) { // Update each city
profit[j] = tmpProfit[j];
}
} // FORK(T) Walk T times
int bestProfit = 0;
FORI(E) {
scanf("%d", &city);
--city;
if(profit[city] > bestProfit)
bestProfit = profit[city];
}
printf("%d\n", bestProfit);
}
}