結果

問題 No.2569 はじめてのおつかいHard
ユーザー kzyKTkzyKT
提出日時 2023-12-02 15:55:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 2,231 bytes
コンパイル時間 2,051 ms
コンパイル使用メモリ 167,064 KB
実行使用メモリ 31,388 KB
最終ジャッジ日時 2023-12-02 15:55:54
合計ジャッジ時間 4,050 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
26,448 KB
testcase_01 AC 60 ms
26,448 KB
testcase_02 AC 60 ms
26,448 KB
testcase_03 AC 59 ms
26,448 KB
testcase_04 AC 59 ms
26,448 KB
testcase_05 AC 217 ms
31,380 KB
testcase_06 AC 175 ms
31,388 KB
testcase_07 AC 170 ms
31,256 KB
testcase_08 AC 213 ms
31,380 KB
testcase_09 AC 103 ms
29,696 KB
testcase_10 AC 7 ms
20,112 KB
testcase_11 AC 7 ms
20,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ln cout<<'\n'
#define ll long long
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.12f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

vector<P> v[222222],g[222222];
ll d1[2][222222],d2[2][222222],n;

void bfs(ll s) {
  ll k=s-(n-2);
  {
    fill(d1[k],d1[k]+n,MAXL);
    d1[k][s]=0;
    priority_queue<P,vector<P>,greater<P> > que;
    que.push(P(0,s));
    while(!que.empty()) {
      P p=que.top();que.pop();
      ll x=p.S,c=p.F;
      if(d1[k][x]<c) continue;
      rep(i,v[x].size()) {
        ll y=v[x][i].F,cc=v[x][i].S;
        if(d1[k][y]<=d1[k][x]+cc) continue;
        d1[k][y]=d1[k][x]+cc;
        que.push(P(d1[k][y],y));
      }
    }
  }
  {
    fill(d2[k],d2[k]+n,MAXL);
    d2[k][s]=0;
    priority_queue<P,vector<P>,greater<P> > que;
    que.push(P(0,s));
    while(!que.empty()) {
      P p=que.top();que.pop();
      ll x=p.S,c=p.F;
      if(d2[k][x]<c) continue;
      rep(i,g[x].size()) {
        ll y=g[x][i].F,cc=g[x][i].S;
        if(d2[k][y]<=d2[k][x]+cc) continue;
        d2[k][y]=d2[k][x]+cc;
        que.push(P(d2[k][y],y));
      }
    }
  }
}

void Main() {
  ll m;
  cin >> n >> m;
  rep(i,m) {
    ll x,y,z;
    cin >> x >> y >> z;
    x--,y--;
    v[x].pb(P(y,z));
    g[y].pb(P(x,z));
  }
  bfs(n-1),bfs(n-2);
  rep(i,n-2) {
    ll ans=MAXL;
    ans=min(ans,d2[0][i]+d1[0][n-1]+d1[1][i]);
    ans=min(ans,d2[1][i]+d1[1][n-2]+d1[0][i]);
    if(ans==MAXL) ans=-1;
    pr(ans);
  }
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
0