Way home "SHOI 2012"

The meaning of problems

Given a trellis, above x nodes, allowing only turn at these nodes, you ask shortest path from start to finish.

Thinking

Obviously only use nodes that turn, do not turn no need to go with colleagues or multiple columns.

Therefore, each node is connected to other peer nodes in the same column, with a weight of from 1 * 2 +.

Shortest run again, due at the end do not need to turn, so the answer to -1.

It is said that this question card spfa.


Code

#include <bits/stdc++.h>

using namespace std;

namespace StandardIO {

    template<typename T> inline void read (T &x) {
        x=0;T f=1;char c=getchar();
        for (; c<'0'||c>'9'; c=getchar()) if (c=='-') f=-1;
        for (; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
        x*=f;
    }
    template<typename T> inline void write (T x) {
        if (x<0) putchar('-'),x=-x;
        if (x>=10) write(x/10);
        putchar(x%10+'0');
    }

}

using namespace StandardIO;

namespace Solve {
    
    const int N=20020;
    const int INF=2147483647;
    
    int n,m;
    struct node {
        int x,y;
    } mp[100100],s,t;
    int d[100100],v[100100];
    vector<int> x[N],y[N];
    
    inline void dijkstra () {
        for (register int i=1; i<=m+2; ++i) {
            d[i]=INF,v[i]=0;
        }
        priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
        d[m+1]=0;
        q.push(make_pair(d[m+1],m+1));
        while (!q.empty()) {
            int now=q.top().second;q.pop();
            for (register int i=0; i<x[mp[now].x].size(); ++i) {
                if (x[mp[now].x][i]==now) continue;
                if (d[x[mp[now].x][i]]>d[now]+2*abs(mp[now].y-mp[x[mp[now].x][i]].y)+1) {
                    d[x[mp[now].x][i]]=d[now]+2*abs(mp[now].y-mp[x[mp[now].x][i]].y)+1;
                    q.push(make_pair(d[x[mp[now].x][i]],x[mp[now].x][i]));
                } 
            }
            for (register int i=0; i<y[mp[now].y].size(); ++i) {
                if (y[mp[now].y][i]==now) continue;
                if (d[y[mp[now].y][i]]>d[now]+2*abs(mp[now].x-mp[y[mp[now].y][i]].x)+1) {
                    d[y[mp[now].y][i]]=d[now]+2*abs(mp[now].x-mp[y[mp[now].y][i]].x)+1;
                    q.push(make_pair(d[y[mp[now].y][i]],y[mp[now].y][i]));
                } 
            }
        }
    }

    inline void Main () {
        read(n),read(m);
        for (register int i=1; i<=m+2; ++i) {
            read(mp[i].x),read(mp[i].y); 
            x[mp[i].x].push_back(i),y[mp[i].y].push_back(i);
        }
//      for (register int i=1; i<=n; ++i) {
//          cout<<i<<" : ";
//          for (register int j=0; j<x[i].size(); ++j) {
//              cout<<x[i][j]<<' ';
//          }
//          cout<<endl;
//      }
//      for (register int i=1; i<=n; ++i) {
//          cout<<i<<" : ";
//          for (register int j=0; j<y[i].size(); ++j) {
//              cout<<y[i][j]<<' ';
//          }
//          cout<<endl;
//      }return;
        dijkstra();
        write(d[m+2]-1);
    }

}

int main () {
    Solve::Main();
}

Guess you like

Origin www.cnblogs.com/ilverene/p/11225021.html