Codeforces 821D Okabe and City 题解

来源:互联网 发布:js时间格式化 编辑:程序博客网 时间:2024/06/05 07:05

题目链接

Codeforces 821D: Okabe and City

题意

在一个n 行m 列的矩阵中,最初有k 盏路灯是亮的,Okabe 想要从坐标为(1,1) 的位置走到坐标为(n,m) 的位置,但是Okabe 只能走路灯亮着的地方,如果Okabe 站在路灯原先就亮着的地方,他可以支付一枚硬币,来让某一行或者某一列的路灯全部都亮起来,但这只是暂时的,一旦他离开自己用硬币支付而亮起来的路灯下,这些路灯如果最初不是亮着的,就会马上熄灭,问从(1,1)(n,m),Okabe 最少需要支付多少硬币,如果他无法到达(n,m),则输出-1。

题解

如果两个路灯的行、列差的绝对值之和等于1,那么Okabe 就不需要支付硬币就可以走到另一个路灯下,否则,如果两个路灯的行、列差的绝对值有一个小于等于2,Okabe 就可以支付一枚硬币走到另一个路灯下,其他情况下为不可到达,这样就可以根据给定的亮着的路灯坐标构一张图,然后找单源最短路径,这里用的是spfa 单源最短路径。
有两种方法构图,一种是一边搜一边构图,另一种是先构图,然后搜最短路。前天刚学链式前向星构图,想用一下,结果在第28 个测试RE 了,因为如果所有的点都在一条直线上,就总共有5×107 条边,边太多存不下……

过题代码

#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <stack>#include <algorithm>#include <climits>#include <cfloat>#include <string>#include <cstring>#include <ctime>#include <cstdlib>using namespace std;#define LL long longconst int inf = 100000000;const int maxn = 10005;struct Node {    int x;    int y;};Node node[maxn];int n, m, k, firstindex;int dis[maxn];bool end;bool inque[maxn];queue<int> que;int spfa() {    // 初始化    int dist, absx, absy;    for(int i = 0; i < k; ++i) {        dis[i] = inf;    }    que.push(firstindex);    inque[firstindex] = true;    dis[firstindex] = 0;    // spfa    int tmp;    while(!que.empty()) {        tmp = que.front();        que.pop();        inque[tmp] = false;        // 访问tmp 点连接的所有边        for(int i = 0; i < k; ++i) {            if(i != tmp) {                dist = inf;                absx = abs(node[i].x - node[tmp].x);                absy = abs(node[i].y - node[tmp].y);                if(absx + absy == 1) {                    dist = 0;                } else if(absx <= 2 || absy <= 2) {                    dist = 1;                }                if(dis[i] > dis[tmp] + dist) {                    dis[i] = dis[tmp] + dist;                    if(!inque[i]) {                        inque[i] = true;                        que.push(i);                    }                }            }        }    }    if(dis[k - 1] == inf) {        return -1;    } else {        return dis[k - 1];    }}int main() {    // 初始化    end = false;    scanf("%d%d%d", &n, &m, &k);    // 输入被点亮的灯    for(int i = 0; i < k; ++i) {        scanf("%d%d", &node[i].x, &node[i].y);        // 判断终点是否被点亮        if(node[i].x == n && node[i].y == m) {            end = true;        }        // 查找起点        if(node[i].x == 1 && node[i].y == 1) {            firstindex = i;        }    }    // 如果终点没有被点亮,就增加终点    if(!end) {        node[k].x = n + 1;        node[k++].y = m + 1;    }    // spfa 搜索最短路    printf("%d\n", spfa());    return 0;}

纪念一下第一次用链式前向星构图RE 代码……

#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <stack>#include <algorithm>#include <climits>#include <cfloat>#include <string>#include <cstring>#include <ctime>#include <cstdlib>using namespace std;#define LL long longconst int inf = 100000000;const int maxn = 10005;struct Edge {    int w;    int to;    int next;};struct Node {    int x;    int y;};Node node[maxn];Edge edge[1000005];int n, m, k, cnt, firstindex;int dis[maxn], head[maxn];bool end;bool inque[maxn];queue<int> que;void add(int u, int v, int w) {    edge[cnt].to = v;    edge[cnt].w = w;    edge[cnt].next = head[u];    head[u] = cnt++;}int spfa() {    // 初始化    for(int i = 0; i < k; ++i) {        dis[i] = inf;    }    que.push(firstindex);    inque[firstindex] = true;    dis[firstindex] = 0;    // spfa    int tmp, next;    while(!que.empty()) {        tmp = que.front();        que.pop();        inque[tmp] = false;        // 访问tmp 点连接的所有边        next = head[tmp];        while(next != -1) {            if(edge[next].w + dis[tmp] < dis[edge[next].to]) {                dis[edge[next].to] = edge[next].w + dis[tmp];                if(!inque[edge[next].to]) {                    inque[edge[next].to] = true;                    que.push(edge[next].to);                }            }            next = edge[next].next;        }    }    if(dis[k - 1] == inf) {        return -1;    } else {        return dis[k - 1];    }}int main() {    scanf("%d%d%d", &n, &m, &k);    // 所有的边的next都指向-1    end = false;    memset(head, -1, sizeof(head));    // 输入被点亮的灯    for(int i = 0; i < k; ++i) {        scanf("%d%d", &node[i].x, &node[i].y);        // 判断终点是否被点亮        if(node[i].x == n && node[i].y == m) {            end = true;        }        // 查找起点        if(node[i].x == 1 && node[i].y == 1) {            firstindex = i;        }    }    // 如果终点没有被点亮,就增加终点    if(!end) {        node[k].x = n + 1;        node[k++].y = m + 1;    }    // 构图    for(int i = 0; i < k; ++i) {        for(int j = 0; j < i; ++j) {            int absx = abs(node[j].x - node[i].x);            int absy = abs(node[j].y - node[i].y);            if(absx + absy == 1) {                add(i, j, 0);                add(j, i, 0);            } else if(absx <= 2 || absy <= 2) {                add(i, j, 1);                add(j, i, 1);            }        }    }    // spfa 搜索最短路    printf("%d\n", spfa());    return 0;}
阅读全文
1 0