Luo Gu P3831 [SHOI2012] way home

Luo Gu P3831 [SHOI2012] way home

topic:

  • There are pictures. Turn link

answer:

  • Layered graph.
  • Very good, further understanding of the layered graph.
  • FIG stratification is to state all the figures are represented, and between different states interfere with each other . This means that if there is a problem can be solved by a method or a second method to solve, then layered graph can not make the method is to use a second method, both methods disorder situation does not appear to take.
  • This problem meaning of the questions: a trellis, if they had gone when an edge with 2, turning only at a certain point, if they had turned over 1, when asked by the minimum two points.
  • Because only point to come forward direction mark, that is, from the beginning to the end point must be labeled to reach (start and end markers are also considered). Then it is just relationship between points marked the scope of our consideration.
  • For the point marked, the rotational direction is.
  • Decision this question is turned, since there is only horizontal and vertical directions, we established one of these two directions, respectively. I.e. only one picture even transverse edges, even only one longitudinal edge.
  • Then the same point as the first layer and the second layer a weight of even a 1 side, showing away from the horizontal into the vertical down 1 comes at a cost.
  • Finally, you can run the shortest path.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <algorithm>
#define N 1000005
#define M 1000005
using namespace std;

struct Node
{
    int val, pos;
    friend bool operator < (Node x, Node y) {
        return x.val > y.val;
    }
};
struct E {int next, to, dis;} e[M];
struct A {int x, y, id;} a[N];
int n, m, num;
int h[N], dis[N];
bool vis[N];

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

bool cmp1(A u, A v)
{
    if(u.x == v.x) return u.y < v.y;
    return u.x < v.x;
}
bool cmp2(A u, A v)
{
    if(u.y == v.y) return u.x < v.x;
    return u.y < v.y;
}

void add(int u, int v, int w)
{
    e[++num].next = h[u];
    e[num].to = v;
    e[num].dis = w;
    h[u] = num;
}

int main()
{
    cin >> n >> m;
    n = m + 2;
    for(int i = 1; i <= n; i++)
    {
        a[i].x = read();
        a[i].y = read();
        a[i].id = i;
    }
    sort(a + 1, a + 1 + n, cmp1);
    for(int i = 2; i <= n; i++)
        if(a[i - 1].x == a[i].x)
        {
            add(a[i - 1].id, a[i].id, (a[i].y - a[i - 1].y) * 2);
            add(a[i].id, a[i - 1].id, (a[i].y - a[i - 1].y) * 2);
        }
    sort(a + 1, a + 1 + n, cmp2);
    for(int i = 2; i <= n; i++)
        if(a[i - 1].y == a[i].y)
        {
            add(a[i - 1].id + n, a[i].id + n, (a[i].x - a[i - 1].x) * 2);
            add(a[i].id + n, a[i - 1].id + n, (a[i].x - a[i - 1].x) * 2);
        }
    for(int i = 1; i <= n; i++)
        add(i, i + n, 1), add(i + n, i, 1);
    add(n - 1, n - 1 + n, 0);
    add(n - 1 + n, n - 1, 0);
    add(n, n + n, 0);
    add(n + n, n, 0);
    memset(dis, 0x3f, sizeof(dis));
    priority_queue<Node> que;
    dis[n - 1] = 0, que.push((Node){0, n - 1});
    while(que.size())
    {
        int now = que.top().pos;
        que.pop();
        if(vis[now]) continue;
        vis[now] = 1;
        for(int i = h[now]; i != 0; i = e[i].next)
            if(dis[now] + e[i].dis < dis[e[i].to])
            {
                dis[e[i].to] = dis[now] + e[i].dis;
                que.push((Node){dis[e[i].to], e[i].to});
            }
    }
    if(dis[n] == 0x3f3f3f3f && dis[n + n] == 0x3f3f3f3f)
        {cout << -1; return 0;}
    cout << min(dis[n], dis[n + n]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11516707.html