結果

問題 No.1393 Median of Walk
ユーザー chocoruskchocorusk
提出日時 2021-02-13 03:47:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 802 ms / 2,000 ms
コード長 4,520 bytes
コンパイル時間 1,774 ms
コンパイル使用メモリ 143,204 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 10:56:59
合計ジャッジ時間 10,910 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 785 ms
4,380 KB
testcase_09 AC 802 ms
4,376 KB
testcase_10 AC 787 ms
4,376 KB
testcase_11 AC 154 ms
4,380 KB
testcase_12 AC 179 ms
4,380 KB
testcase_13 AC 165 ms
4,380 KB
testcase_14 AC 200 ms
4,376 KB
testcase_15 AC 208 ms
4,376 KB
testcase_16 AC 64 ms
4,376 KB
testcase_17 AC 73 ms
4,380 KB
testcase_18 AC 35 ms
4,380 KB
testcase_19 AC 95 ms
4,380 KB
testcase_20 AC 89 ms
4,380 KB
testcase_21 AC 178 ms
4,380 KB
testcase_22 AC 180 ms
4,380 KB
testcase_23 AC 162 ms
4,380 KB
testcase_24 AC 188 ms
4,376 KB
testcase_25 AC 192 ms
4,376 KB
testcase_26 AC 790 ms
4,376 KB
testcase_27 AC 799 ms
4,376 KB
testcase_28 AC 21 ms
4,380 KB
testcase_29 AC 114 ms
4,376 KB
testcase_30 AC 34 ms
4,380 KB
testcase_31 AC 33 ms
4,376 KB
testcase_32 AC 80 ms
4,380 KB
testcase_33 AC 99 ms
4,376 KB
testcase_34 AC 162 ms
4,376 KB
testcase_35 AC 47 ms
4,380 KB
testcase_36 AC 100 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 18 ms
4,376 KB
testcase_39 AC 9 ms
4,380 KB
testcase_40 AC 36 ms
4,376 KB
testcase_41 AC 72 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <stack>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
    int n, m; cin>>n>>m;
    vector<P> g[1010], gr[1010];
    int u[2525], v[2525], w[2525];
    vector<P> wp(m);
    for(int i=0; i<m; i++){
        cin>>u[i]>>v[i]>>w[i];
        u[i]--; v[i]--;
        g[u[i]].push_back({v[i], i});
        gr[v[i]].push_back({u[i], i});
        wp[i]=P(w[i], i);
    }
    sort(wp.begin(), wp.end());
    //wp.erase(unique(wp.begin(), wp.end()), wp.end());
    int ans[1010];
    fill(ans, ans+n, -2);
    int d[1010];
    const int INF=1e9;
    fill(d, d+n, INF);
    d[0]=0;
    for(int i=0; i<n; i++){
        for(int x=0; x<n; x++){
            for(auto p:g[x]){
                int y=p.first;
                if(d[y]>d[x]+1){
                    d[y]=d[x]+1;
                }
            }
        }
    }
    for(int i=0; i<n; i++){
        if(d[i]==INF) ans[i]=-1;
        //cout<<d[i]<<" ";
    }
    //cout<<endl;
    for(int i=0; i<m; i++){
        queue<int> que1;
        priority_queue<P, vector<P>, greater<P>> que;
        int i1=wp[i].second;
        
        int d2[1010];
        fill(d2, d2+n, INF);
        d2[v[i1]]=0;
        que.push({0, v[i1]});
        if(d[u[i1]]>=-INF/2 && ans[u[i1]]!=-1){
            que.push({v[i1], 0});
            while(!que.empty()){
                P p=que.top(); que.pop();
                int x=p.second;
                //cout<<x<<" "<<p.first<<" "<<i<<endl;
                if(d2[x]<p.first) continue;
                for(auto q:g[x]){
                    if(q.second==i1) continue;
                    int y=q.first;
                    if(d[y]<-INF/2) continue;
                    int e=d[x]-d[y];
                    if(P(w[q.second], q.second)<=wp[i]) e--;
                    else e++;
                    //cout<<x<<" "<<y<<" "<<d[x]<<" "<<d[y]<<" "<<e<<endl;
                    if(d2[y]>d2[x]+e){
                        d2[y]=d2[x]+e;
                        que.push({d2[y], y});
                    }
                }
            }
        }
        //cout<<i<<" "<<u[i1]<<" "<<v[i1]<<" "<<d2[u[i1]]<<" "<<d[u[i1]]<<" "<<d[v[i1]]<<endl;
        if(d2[u[i1]]<INF/2 && d2[u[i1]]+d[u[i1]]-d[v[i1]]-1<0 && ans[u[i1]]!=-1){
            queue<int> que2;
            bool used2[1010]={};
            used2[u[i1]]=1;
            que2.push(u[i1]);
            while(!que2.empty()){
                int x=que2.front(); que2.pop();
                for(auto q:g[x]){
                    int y=q.first;
                    if(!used2[y]){
                        used2[y]=1;
                        que2.push(y);
                    }
                }
            }
            for(int x=1; x<n; x++){
                if(used2[x]){
                    if(ans[x]==-2) ans[x]=wp[i].first;
                    d[x]=-INF;
                }
            }
        }else{
            que.push({0, 0});
            int d1[1010];
            fill(d1, d1+n, INF);
            d1[0]=0;
            while(!que.empty()){
                P p=que.top(); que.pop();
                int x=p.second;
                if(d1[x]<p.first) continue;
                //cout<<i<<" "<<x<<" "<<d1[x]<<endl;
                for(auto q:g[x]){
                    int y=q.first;
                    if(d[y]<-INF/2) continue;
                    int e=d[x]-d[y];
                    if(P(w[q.second], q.second)<=wp[i]) e--;
                    else e++;
                    //cout<<i<<" "<<x<<" "<<y<<" "<<d[x]<<" "<<d[y]<<" "<<e<<endl;
                    if(d1[y]>d1[x]+e){
                        d1[y]=d1[x]+e;
                        que.push({d1[y], y});
                    }
                }
            }
            for(int x=0; x<n; x++){
                if(ans[x]==-1) continue;
                if(d[x]>=-INF/2) d[x]+=d1[x];
                else d[x]=-INF;
                if(ans[x]==-2 && d[x]<=0){
                    ans[x]=wp[i].first;
                }
                //cout<<d[x]<<" ";
            }
            //cout<<endl;
        }
    }
    for(int x=1; x<n; x++){
        cout<<ans[x]<<endl;
    }
    return 0;
}
0