O – 简单的图论问题?
Time Limit:5000MS Memory Limit:65535KB 64bit IO Format:
Submit
Status
Description
给一个 n 行 m 列的迷宫,每个格子要么是障碍物要么是空地。每个空地里都有一个权值。你的 任务是从找一条(r1,c1)到(r2,c2)的路径,使得经过的空地的权值之和最小。每一步可以往上下 左右四个方向之一移动一格,但不能斜着移动,也不能移动到迷宫外面或者进入障碍物格子。
如下图,灰色格子代表障碍物。路径 A->B->D->F->E 的权值为 10+3+6+14+8=41,它是从 A 到 E 的最优路径。注意,如果同一个格子被经过两次,则权值也要加两次。
为了让题目更有趣(顺便增加一下难度),你还需要回答另外一个问题:如果你每次必须转弯 (左转、右转或者后退,只要不是沿着上次的方向继续走即可),最小权值是多少?比如,在 上图中,如果你刚刚从 A 走到 B,那么下一步你可以走到 D 或者 A,但不能走到 G。在上图 中,A 到 E 的最优路径是 A->B->D->H->D->F->E,权和为 10+3+6+2+6+14+8=49。注意,D 经 过了两次。
Input
输入包含不超过 10 组数据。每组数据第一行包含 6 个整数 n, m, r1, c1, r2, c2 (2<=n,m<=500, 1<=r1,r2<=n, 1<=c1,c2<=m). 接下来的 n 行每行包含 m 个格子的描述。每个格子要么是一个 1~100 的整数,要么是星号”*”(表示障碍物)。起点和终点保证不是障碍物。
Output
对于每组数据,输出两个整数。第一个整数是“正常问题”的答案,第二个整数是“有趣问 题”的答案。如果每个问题的答案是“无解”,对应的答案应输出-1。
Sample Input
4 4 1 2 3 2 7 10 3 9 * 45 6 2 * 8 14 * 21 1 * * 2 4 1 1 1 4 1 2 3 4 9 * * 9 2 4 1 1 1 4 1 * 3 4 9 9 * 9
Sample Output
Case 1: 41 49 Case 2: 10 -1 Case 3: -1 -1
15年湖南省赛的题目,做了下后发现就是裸Dijkstra
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
const int inf = 0x3fffffff;
int mp[505][505], d[505][505][4], dp[505][505], dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
struct node {
int cost, x, y, dir;
node(int a, int b, int c, int d) {
cost = a;
x = b;
y = c;
dir = d;
}
bool operator<(const node& _A)const {
return cost > _A.cost;
}
};
int pow(int b) {
int ret = 1;
for(int i = 0; i < b; i++) ret *= 10;
return ret;
}
int main() {
int n, m, x1, x2, y1, y2, T = 1;
// freopen("in.txt","r",stdin);
while(~scanf("%d%d%d%d%d%d", &n, &m, &x1, &y1, &x2, &y2)) {
x1--; y1--; x2--; y2--;
char t[5];
int len;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
d[i][j][0] = d[i][j][1] = d[i][j][2] = d[i][j][3] = dp[i][j] = inf;
scanf("%s", t);
if(t[0] == '*') mp[i][j] = inf;
else {
len = strlen(t);
mp[i][j] = 0;
for(int k = 0; k < len; k++) mp[i][j] += (t[k] - '0') * pow(len - k - 1);
}
}
}
printf("Case %d: ", T++);
dp[x1][y1] = mp[x1][y1];
priority_queue<node>q;
q.push(node(dp[x1][y1], x1, y1, 0));
while(!q.empty()) {
node p = q.top(); q.pop();
if(dp[p.x][p.y] < p.cost) continue;
for(int i = 0; i < 4; i++) {
int xx = p.x + dx[i], yy = p.y + dy[i];
if(xx < 0 || xx >= n || yy < 0 || yy >= m || mp[xx][yy] == inf) continue;
node np(dp[p.x][p.y] + mp[xx][yy], xx, yy, 0);
if(dp[np.x][np.y] > np.cost) {
dp[np.x][np.y] = np.cost;
q.push(np);
}
}
}
if(dp[x2][y2] == inf) printf("-1 ");
else printf("%d ", dp[x2][y2]);
while(!q.empty()) q.pop();
for(int i = 0; i < 4; i++) {
int xx = x1 + dx[i], yy = y1 + dy[i];
if(xx < 0 || xx >= n || yy < 0 || yy >= m || mp[xx][yy] == inf) continue;
d[xx][yy][i] = mp[xx][yy] + mp[x1][y1];
q.push(node(d[xx][yy][i], xx, yy, i));
}
while(!q.empty()) {
node p = q.top(); q.pop();
if(d[p.x][p.y][p.dir] < p.cost) continue;
for(int i = 0; i < 4; i++) {
int xx = p.x + dx[i], yy = p.y + dy[i];
if(i == p.dir || xx < 0 || xx >= n || yy < 0 || yy >= m || mp[xx][yy] == inf) continue;
node np(d[p.x][p.y][p.dir] + mp[xx][yy], xx, yy, i);
if(d[np.x][np.y][np.dir] > np.cost) {
d[np.x][np.y][np.dir] = np.cost;
q.push(np);
}
}
}
int ans = inf;
for(int i = 0; i < 4; i++) ans = min(ans, d[x2][y2][i]);
if(ans == inf) printf("-1\n");
else printf("%d\n", ans);
}
}