結果

問題 No.1283 Extra Fee
ユーザー kyoprounokyoprouno
提出日時 2020-11-06 23:55:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,633 bytes
コンパイル時間 2,129 ms
コンパイル使用メモリ 186,120 KB
実行使用メモリ 34,156 KB
最終ジャッジ日時 2023-09-29 19:45:48
合計ジャッジ時間 6,300 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
34,156 KB
testcase_01 AC 12 ms
26,868 KB
testcase_02 AC 12 ms
26,728 KB
testcase_03 AC 10 ms
26,720 KB
testcase_04 AC 9 ms
26,924 KB
testcase_05 AC 10 ms
26,800 KB
testcase_06 AC 11 ms
26,736 KB
testcase_07 AC 9 ms
26,864 KB
testcase_08 AC 12 ms
26,840 KB
testcase_09 AC 11 ms
26,748 KB
testcase_10 AC 10 ms
26,732 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){//popcount%2
	return __builtin_parity(x);
}



template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

const ll INF=1e18;



//const ll INF=1e10+100;
const ll white=0; //未探索
const ll gray=1; //探索中
const ll black=2; //探索済み

vector<pll> m[1000005]; 

ll v,r;

ll dx[4]={-1,1,0,0};
ll dy[4]={0,0,-1,1};
vector<ll> d;
void dijkstra(){
    priority_queue<pll> PQ;
    vector<ll> color(v);
    rep(i,v){
        d[i]=INF;
        color[i]=white;
    }
    d[r]=0;
    PQ.push(make_pair(0,r));
    color[r]=gray;
    while(!PQ.empty()){
        pll f=PQ.top();
        PQ.pop();
        ll u=f.second;
        color[u]=black;
        if(d[u]<f.first*(-1)) continue; //d[u]は正。
        for(ll j=0;j<m[u].size();j++){
            ll v=m[u][j].first;
            if(color[v]==black) continue;
            if(d[v]>d[u]+m[u][j].second){
                d[v]=d[u]+m[u][j].second;
                PQ.push(make_pair(d[v]*(-1),v));
                color[v]=gray;
            }
        }
    }
    
}


int main(){
    ll n,M;
    cin >> n >> M;
    vector<ll> h(M),w(M),c(M);
    d=vector<ll>(n*n,INF);
    rep(i,n){
        rep(j,n){
            ll now=i*n+j;
            rep(k,4){
                ll nx=i+dx[k];
                ll ny=j+dy[k];
                if(0<=nx && nx<=n-1 && 0<=ny && ny<=n-1){
                    ll val=nx*n+ny;
                    m[now].push_back({val,1LL});
                    m[val].push_back({now,1LL});
                }
            }
        }
    }
    vector<pll> edge(M);
    rep(i,M){
        cin >> h[i] >> w[i] >> c[i];
        h[i]--; w[i]--;
        ll now=h[i]*n+w[i];
        rep(k,4){
            ll nx=h[i]+dx[k];
            ll ny=w[i]+dy[k];
            ll val=nx*n+ny;
            if(0<=nx && nx<=n-1 && 0<=ny && ny<=n-1){
                for(auto &x:m[val]){
                    if(x.fi==now){
                        x.se=c[i]+1;
                    }
                }
            }
        }
    }
    
    v=n*n;
    r=0;
    ll ans=INF;
    rep(i,M){
        ll now=h[i]*n+w[i];
        rep(k,4){
            ll nx=h[i]+dx[k];
            ll ny=w[i]+dy[k];
            if(0<=nx && nx<=n-1 && 0<=ny && ny<=n-1){
                ll val=nx*n+ny;
                for(auto &x:m[val]){
                    if(x.fi==now){
                        x.se=1;
                    }
                }
            }
        }
        dijkstra();
        ans=min(ans,d[n*n-1]);
        rep(k,4){
            ll nx=h[i]+dx[k];
            ll ny=w[i]+dy[k];
            if(0<=nx && nx<=n-1 && 0<=ny && ny<=n-1){
                ll val=nx*n+ny;
                for(auto &x:m[val]){
                    if(x.fi==now){
                        x.se=c[i]+1;
                    }
                }
            }
        }
        d=vector<ll>(n*n,INF);
    }
    cout << ans << endl;
    return 0;
}
0