結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー outlineoutline
提出日時 2021-01-05 03:47:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 2,094 bytes
コンパイル時間 1,739 ms
コンパイル使用メモリ 147,700 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 12:30:28
合計ジャッジ時間 5,277 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 192 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 122 ms
6,940 KB
testcase_12 AC 11 ms
6,940 KB
testcase_13 AC 50 ms
6,944 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 12 ms
6,944 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 62 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 185 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 39 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 8 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 48 ms
6,944 KB
testcase_34 AC 23 ms
6,944 KB
testcase_35 AC 8 ms
6,944 KB
testcase_36 AC 5 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 12 ms
6,944 KB
testcase_39 AC 4 ms
6,944 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,944 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 5 ms
6,944 KB
testcase_44 AC 4 ms
6,940 KB
testcase_45 AC 149 ms
6,940 KB
testcase_46 AC 6 ms
6,940 KB
testcase_47 AC 20 ms
6,940 KB
testcase_48 AC 48 ms
6,944 KB
testcase_49 AC 6 ms
6,940 KB
testcase_50 AC 2 ms
6,940 KB
testcase_51 AC 2 ms
6,940 KB
testcase_52 AC 10 ms
6,944 KB
testcase_53 AC 6 ms
6,940 KB
testcase_54 AC 104 ms
6,944 KB
testcase_55 AC 104 ms
6,940 KB
testcase_56 AC 102 ms
6,944 KB
testcase_57 AC 133 ms
6,944 KB
testcase_58 AC 135 ms
6,940 KB
testcase_59 AC 137 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T, N, M;
    cin >> T >> N >> M;
    using edge = tuple<int, int, int>;
    vector<vector<edge>> graph(N);
    vector<int> u(M), v(M), w(M);
    for(int i = 0; i < M; ++i){
        cin >> u[i] >> v[i] >> w[i];
        --u[i], --v[i];
        graph[u[i]].emplace_back(v[i], w[i], i);
        if(T == 0)  graph[v[i]].emplace_back(u[i], w[i], i);
    }

    constexpr ll inf = 1e+17;
    ll ans = inf;
    for(int ban = 0; ban < M; ++ban){
        vector<ll> dist(N, inf);
        dist[v[ban]] = 0;
        using Data = pair<ll, int>;
        priority_queue<Data, vector<Data>, greater<Data>> que;
        que.emplace(0, v[ban]);
        while(!que.empty()){
            auto [d, from] = que.top();
            que.pop();
            if(d > dist[from])  continue;
            for(auto [to, cost, id] : graph[from]){
                if(id != ban && chmin(dist[to], dist[from] + cost)){
                    que.emplace(dist[to], to);
                }
            }
        }
        chmin(ans, dist[u[ban]] + w[ban]);
    }

    if(ans == inf)  ans = -1;
    cout << ans << endl;

    return 0;
}
0