結果

問題 No.614 壊れたキャンパス
ユーザー algon_320algon_320
提出日時 2017-12-14 06:31:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,289 ms / 2,000 ms
コード長 3,405 bytes
コンパイル時間 2,052 ms
コンパイル使用メモリ 196,460 KB
実行使用メモリ 110,652 KB
最終ジャッジ日時 2023-08-21 03:51:04
合計ジャッジ時間 12,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 739 ms
108,784 KB
testcase_09 AC 1,289 ms
108,520 KB
testcase_10 AC 408 ms
97,544 KB
testcase_11 AC 999 ms
109,900 KB
testcase_12 AC 1,035 ms
110,060 KB
testcase_13 AC 1,013 ms
110,636 KB
testcase_14 AC 789 ms
110,652 KB
testcase_15 AC 406 ms
89,320 KB
testcase_16 AC 638 ms
109,640 KB
testcase_17 AC 232 ms
108,024 KB
testcase_18 AC 605 ms
87,440 KB
testcase_19 AC 562 ms
90,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w)
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
#define INF (1001001001)
#define INFLL (1001001001001001001ll)
template<class T> ostream& operator << (ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v)) os<<*i<<(i==end(v)-1?"":" "); return os; }
template<class T> istream& operator >> (istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
    is>>p.first>>p.second; return is; }
template <class T> bool chmax(T &a,const T &b) {if(a<b) {a=b;return 1;} return 0;}
template <class T> bool chmin(T &a,const T &b) {if(b<a) {a=b;return 1;} return 0;}
template <class T> using heap = priority_queue<T,vector<T>,greater<T>>;
struct before_main_function {
    before_main_function() {
        #ifdef int
            #undef INF
            #define INF INFLL
            #define stoi stoll
        #endif
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
        #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

signed main() {
    int N,M,K,S,T;
    cin>>N>>M>>K>>S>>T;
    vector<vector<int>> bridge(M,vector<int>(3));
    cin>>bridge;
    vector<map<int,vector<pair<pii,int>>>> g(N);
    g[0][S]={};
    g[N-1][T]={};
    REP(i,M) {
        int a=bridge[i][0]-1;
        int b=bridge[i][1];
        int c=bridge[i][2];
        g[a][b].push_back({{a+1,c},0});
        if(!CONTAIN(g[a+1],c)) {
            g[a+1][c]={};
        }
    }
    REP(i,N) {
        ITR(itr,begin(g[i]),end(g[i])) {
            if(itr!=begin(g[i])) {
                auto prev=--itr;
                itr++;
                itr->second.push_back({{i,prev->first},abs((itr->first)-(prev->first))});
            }
            auto next=++itr;
            itr--;
            if(next!=end(g[i])) {
                itr->second.push_back({{i,next->first},abs((itr->first)-(next->first))});
            }
        }
    }

    // REP(i,N) {
    //     cout<<i<<endl;
    //     FORE(mp,g[i]) {
    //         cout<<mp.first<<": "<<mp.second<<endl;
    //     }
    //     cout<<endl;
    // }

    heap<pair<int,pii>> hp;
    hp.push({0,{0,S}});
    vector<map<int,int>> dist(N);
    while(!hp.empty()) {
        int d=hp.top().first;
        int v=hp.top().second.first;
        int f=hp.top().second.second;
        hp.pop();
        FORE(e,g[v][f]) {
            int vv=e.first.first;
            int ff=e.first.second;
            int cost=e.second;
            if(!CONTAIN(dist[vv],ff) || dist[vv][ff]>d+cost) {
                dist[vv][ff]=d+cost;
                hp.push({d+cost,{vv,ff}});
            }
        }
    }
    if(CONTAIN(dist[N-1],T)) {
        cout<<dist[N-1][T]<<endl;
    } else {
        cout<<-1<<endl;
    }
    return 0;
}
0