結果

問題 No.468 役に立つ競技プログラミング実践編
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-06-03 01:52:06
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,783 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 169,912 KB
実行使用メモリ 23,148 KB
最終ジャッジ日時 2023-08-16 11:15:26
合計ジャッジ時間 10,030 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 103 ms
21,276 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.06.03 01:52:02
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

struct DAG {
private:
    struct Edge {
        int to,cost;
    };
    std::vector<std::vector<Edge>> graph;
    bool is_dag = false;
    std::vector<int> sorted; // トポロジカルソート
    int V; // 頂点数
public:
    DAG() {}
    DAG(int v) {
        assert(v > 0);
        V = v;
        graph.resize(v);
    }
    // from から to への有向辺を張る
    void add_edge(int from, int to,int cost) {
        graph[from].push_back({to,cost});
    }
    // トポロジカルソート O(V + E)
    // DAG じゃないなら size 0 の vectorを返す
    std::vector<int> topological_sort() {
        std::stack<int> sta;
        //std::vector<int> dist(V, 0);//その頂点までの最長路
        std::vector<int> in(V, 0);// 入次数
        int used_cnt = 0;//使用した頂点の数
        for (int i = 0; i < V; i++) {
            for (Edge e : graph[i]) {
                in[e.to]++;
            }
        }
        for (int i = 0; i < V; i++) if (in[i] == 0) {
            sta.push(i);
            used_cnt++;
        }
        while (!sta.empty()) {
            int p = sta.top(); sta.pop();
            sorted.push_back(p);
            for (Edge e : graph[p]) {
                int v = e.to;
                in[v]--;
                //dist[v] = std::max(dist[v], dist[p] + 1);
                if (in[v] == 0) {
                    sta.push(v);
                    used_cnt++;
                }
            }
        }
        if (used_cnt == V) {
            return sorted;
        }
        else {
            return std::vector<int>(0);
        }
    }
    vector<Edge>& operator[](int x) {
        return graph[x];
    }
};
vector<vector<pair<int,int>>> G;
vector<bool> used;
vector<int> dp;
DAG g;
int cnt;
void dfs(int v, int p = -1) {
    for(auto e : G[v]) {
        if (e.first == p) continue;
        if (!used[e.first] && dp[v] - dp[e.first] == e.second) {
            used[e.first] = true;
            dfs(e.first,v);
            cnt++;
        }
    }
}
int main() {
    int n;cin >> n;
    int m;cin >> m;
    G.resize(n);
    g = DAG(n);
    for (int i = 0; i < m; i++) {
        int a,b,c;cin >> a >> b >> c;
        g.add_edge(a,b,c);
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }
    auto vec = g.topological_sort();
    dp.resize(n,0);
    for(int v : vec) {
        for(auto e : g[v]) {
            if (dp[e.to] < dp[v] + e.cost) {
                dp[e.to] = dp[v] + e.cost;
            }
        }
    }
    used.resize(n,false);
    used[n-1] = true;
    cnt++;
    dfs(n-1);
    cout << dp[n-1] << " " << n - cnt << "/" << n << endl;
    return 0;
}
0