UVa 1078 - Steam Roller

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

駕駛蒸氣機在道路上,啟動蒸汽機通過道路需要時間。當要進行轉彎時,必須在轉彎口前進行煞車、出了轉彎口後開始加速,煞車和加速會造成所需時間變成兩倍,在起點出發必須加速、抵達終點時必須減速。

請問最少花費時間需要多少。

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
4 4 1 1 4 4
10 10 10
9 0 0 10
0 0 0
9 0 0 10
9 0 0
0 9 0 10
0 9 9
2 2 1 1 2 2 0 1 1 0
0 0 0 0 0 0

Sample Output

1
2
Case 1: 100
Case 2: Impossible

Solution

定義狀態 dist[i][j][dir][k] 在轉彎口 (i, j),前一次進入轉彎口的方向為 dir,在此之前是否有煞車 k。

隨後進行最短路徑。

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
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
const int MAXN = 128;
int cg[MAXN][MAXN][4];
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int dist[MAXN][MAXN][4][2], inq[MAXN][MAXN][4][2];
int spfa(int r1, int c1, int r2, int c2) {
if (r1 == r2 && c1 == c2)
return 0;
queue<int> X, Y, D, F;
int d1, f1, x, y, w;
memset(dist, 63, sizeof(dist));
memset(inq, 0, sizeof(inq));
for (int i = 0; i < 4; i++) {
if (!cg[r1][c1][i]) continue;
x = r1 + dx[i], y = c1 + dy[i];
dist[x][y][i][1] = 2 * cg[r1][c1][i];
inq[x][y][i][1] = 1;
X.push(x), Y.push(y), D.push(i), F.push(1);
}
while (!X.empty()) {
r1 = X.front(), X.pop();
c1 = Y.front(), Y.pop();
d1 = D.front(), D.pop();
f1 = F.front(), F.pop();
inq[r1][c1][d1][f1] = 0;
// printf("%d %d %d %d - %d\n", r1, c1, d1, f1, dist[r1][c1][d1][f1]);
for (int i = 0; i < 4; i++) {
if (!cg[r1][c1][i]) continue;
x = r1 + dx[i], y = c1 + dy[i];
// printf("-> %d %d\n", x, y);
if (f1 == 1) {
if (i == d1) {
w = cg[r1][c1][i] + dist[r1][c1][d1][f1];
if (dist[x][y][i][0] > w) {
dist[x][y][i][0] = w;
if (!inq[x][y][i][0]) {
inq[x][y][i][0] = 1;
X.push(x), Y.push(y), D.push(i), F.push(0);
}
}
}
w = 2 * cg[r1][c1][i] + dist[r1][c1][d1][f1];
if (dist[x][y][i][1] > w) {
dist[x][y][i][1] = w;
if (!inq[x][y][i][1]) {
inq[x][y][i][1] = 1;
X.push(x), Y.push(y), D.push(i), F.push(1);
}
}
} else {
if (i != d1) continue;
w = cg[r1][c1][i] + dist[r1][c1][d1][f1];
if (dist[x][y][i][0] > w) {
dist[x][y][i][0] = w;
if (!inq[x][y][i][0]) {
inq[x][y][i][0] = 1;
X.push(x), Y.push(y), D.push(i), F.push(0);
}
}
w = 2 * cg[r1][c1][i] + dist[r1][c1][d1][f1];
if (dist[x][y][i][1] > w) {
dist[x][y][i][1] = w;
if (!inq[x][y][i][1]) {
inq[x][y][i][1] = 1;
X.push(x), Y.push(y), D.push(i), F.push(1);
}
}
}
}
}
int ret = 0x3f3f3f3f;
for (int i = 0; i < 4; i++)
ret = min(ret, dist[r2][c2][i][1]);
return ret == 0x3f3f3f3f ? -1 : ret;
}
int main() {
int R, C, r1, c1, r2, c2;
int x, cases = 0;
while (scanf("%d %d %d %d %d %d", &R, &C, &r1, &c1, &r2, &c2) == 6 && R) {
memset(cg, 0, sizeof(cg));
for (int i = 0; i < R; i++) {
for (int j = 0; j < C - 1; j++) {
scanf("%d", &x);
cg[i][j][0] = cg[i][j+1][1] = x;
}
if (i != R - 1) {
for (int j = 0; j < C; j++) {
scanf("%d", &x);
cg[i][j][2] = cg[i+1][j][3] = x;
}
}
}
r1--, c1--, r2--, c2--;
int d = spfa(r1, c1, r2, c2);
printf("Case %d: ", ++cases);
if (d >= 0)
printf("%d\n", d);
else
printf("Impossible\n");
}
return 0;
}
/*
4 4 4 3 2 4
7 6 2
4 5 0 4
6 4 1
4 4 2 2
9 4 4
5 5 3 2
9 6 9
4 4 1 1 4 4
10 10 10
9 0 0 10
0 0 0
9 0 0 10
9 0 0
0 9 0 10
0 9 9
4 4 1 1 1 2
10 10 10
9 0 0 10
0 0 0
9 0 0 10
9 0 0
0 9 0 10
0 9 9
4 4 1 1 4 4
10 10 10
9 0 0 10
0 0 0
9 0 0 10
9 0 0
0 9 0 0
0 9 9
*/