結果

問題 No.30 たこやき工場
ユーザー GOTKAKO
提出日時 2025-05-20 19:25:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,688 bytes
コンパイル時間 3,009 ms
コンパイル使用メモリ 208,980 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-05-20 19:25:54
合計ジャッジ時間 3,541 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<int> topolo(vector<vector<pair<int,long long>>> &Graph){
    int siz = Graph.size();
    vector<int> ret,inV(siz);
    for(auto u : Graph) for(auto [to,w] : u) inV.at(to)++;
    queue<int> Q;
    for(int i=siz-1; i<siz; i++) if(inV.at(i) == 0) Q.push(i);
    while(Q.size()){
        int pos = Q.front(); Q.pop();
        ret.push_back(pos);
        for(auto [to,w] : Graph.at(pos)){
            inV.at(to)--;
            if(inV.at(to) == 0) Q.push(to);
        }
    }
    return ret;
}

vector<long long> BFS(vector<vector<pair<int,long long>>> &Graph,int start){
    int N = Graph.size();
    vector<long long> ret(N,-1);
    queue<int> Q;
    ret.at(start) = 0,Q.push(start);
    while(Q.size()){
        int pos = Q.front(); Q.pop();
        for(auto [to,w] : Graph.at(pos)){
            if(ret.at(to) != -1) continue;
            ret.at(to) = ret.at(pos)+1;
            Q.push(to);
        }
    }
    return ret;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int N,M; cin >> N >> M;
    vector<vector<pair<int,long long>>> Graph(N),G(N);
    for(int i=0; i<M; i++){
        int a,b,c; cin >> a >> b >> c; a--; c--;
        G.at(c).push_back({a,b});
    }
    vector<long long> need = BFS(G,N-1);
    for(int i=0; i<N; i++) if(need.at(i) != -1) for(auto [to,w] : G.at(i)) Graph.at(i).push_back({to,w});

    auto topo = topolo(Graph);
    need.assign(N,0);
     need.at(N-1) = 1;
    for(auto pos : topo) for(auto [to,w] : Graph.at(pos)) need.at(to) += need.at(pos)*w;
    for(int i=0; i<N-1; i++){
        if(Graph.at(i).size()) cout << "0\n";
        else cout << need.at(i) << "\n";
    }
}
0