传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2346

比较裸的最短路(' '     ) 水题又多了一道

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn = 400010;
const int maxe = 2001000;
const int maxl = 510;
const int INF = 0x3f3f3f3f;

struct edge {
    int t, d;
    edge* next;
}e[maxe * 2], *head[maxn]; int ne = 0;

void addedge(int f, int t, int d) {
    e[ne].t = t, e[ne].d = d, e[ne].next = head[f], head[f] = e + ne ++;
    e[ne].t = f, e[ne].d = d, e[ne].next = head[t], head[t] = e + ne ++;
}

struct node {
    int pos, dis; 
    node(int a, int b) {
        pos = a, dis = b;
    }
};

bool operator < (const node &a, const node &b) {
    return a.dis > b.dis;
}

priority_queue <node> q; 
int dis[maxn];

void dijkstra(int s, int t) {
    memset(dis, INF, sizeof(dis)); dis[s] = 0;
    for(int i = s; i <= t; ++ i) q.push(node(i, dis[i]));
    while(!q.empty()) {
        node x = q.top(); q.pop();
        if(x.dis != dis[x.pos]) continue;
        for(edge* p = head[x.pos]; p; p = p-> next) 
            if(dis[p-> t] > dis[x.pos] + p-> d)
                dis[p-> t] = dis[x.pos] + p-> d, q.push(node(p-> t, dis[p-> t]));
    }
}

int n, m;

int getpos(int x, int t) {
    return (x - 1) * (m + 1) + t;
}

char s[maxl];

void sov() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++ i) {
        scanf("%s", s + 1);
        for(int j = 1; j <= m; ++ j) {
            if(s[j] == '/') addedge(getpos(i, j), getpos(i + 1, j + 1), 1), addedge(getpos(i + 1, j), getpos(i, j + 1), 0);
            else addedge(getpos(i, j), getpos(i + 1, j + 1), 0), addedge(getpos(i + 1, j), getpos(i, j + 1), 1);
        }
    }
    dijkstra(getpos(1, 1), getpos(n + 1, m + 1)); 
    if(dis[getpos(n + 1, m + 1)] == INF) printf("NO SOLUTION\n");
    else printf("%d\n", dis[getpos(n + 1, m + 1)]);
}

int main() {
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    sov();
    return 0;
}