結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー kawara_y_kyoprokawara_y_kyopro
提出日時 2020-12-17 08:44:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 575 ms / 2,000 ms
コード長 2,650 bytes
コンパイル時間 1,789 ms
コンパイル使用メモリ 136,200 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 11:19:54
合計ジャッジ時間 8,004 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 575 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 286 ms
4,348 KB
testcase_12 AC 23 ms
4,348 KB
testcase_13 AC 106 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 227 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 73 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 517 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 77 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 14 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 176 ms
4,348 KB
testcase_34 AC 79 ms
4,348 KB
testcase_35 AC 27 ms
4,348 KB
testcase_36 AC 82 ms
4,348 KB
testcase_37 AC 12 ms
4,348 KB
testcase_38 AC 118 ms
4,348 KB
testcase_39 AC 23 ms
4,348 KB
testcase_40 AC 2 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 6 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 320 ms
4,348 KB
testcase_46 AC 11 ms
4,348 KB
testcase_47 AC 70 ms
4,348 KB
testcase_48 AC 70 ms
4,348 KB
testcase_49 AC 9 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 25 ms
4,348 KB
testcase_53 AC 13 ms
4,348 KB
testcase_54 AC 115 ms
4,348 KB
testcase_55 AC 116 ms
4,348 KB
testcase_56 AC 115 ms
4,348 KB
testcase_57 AC 382 ms
4,348 KB
testcase_58 AC 389 ms
4,348 KB
testcase_59 AC 382 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
// #include <atcoder/all>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vii = V<int>;
using vvll = V<vll>;
using vvii = V< V<int> >;
using PII = P<int, int>;
using PLL = P<ll, ll>;
#define RevREP(i,n,a) for(ll i=n;i>a;i--)  // (a,n]
#define REP(i,a,n) for(ll i=a;i<n;i++)  // [a,n)
#define rep(i, n) REP(i,0,n)
#define ALL(v) v.begin(),v.end()
#define eb emplace_back
#define pb push_back
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) for(int sz=0;sz<int(vec.size());sz++) std::cerr<<vec[sz]<<(sz==int(vec.size())-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

using PLI = P<ll, int>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t, n, m; cin >> t >> n >> m;
    V< V<PII> > edges(n);
    rep(i, m) {
        int u, v, w; cin >> u >> v >> w;
        u--, v--;
        edges[u].eb(PII(v, w));
        if (t == 0) edges[v].eb(PII(u, w));
    }

    ll ans = HIGHINF;
    rep(i, n) {
        for (PII to: edges[i]) {
            auto [ton, tow] = to;
            vll dist(n, HIGHINF);
            priority_queue<PLI> pq;
            pq.push(PLI(0, ton));
            while (pq.size() > 0) {
                PLI q = pq.top(); pq.pop();
                if (dist[q.second] < -q.first) continue;
                dist[q.second] = -q.first;
                for (PII e: edges[q.second]) {
                    if (dist[e.first] < ll(-q.first) + e.second) continue;
                    if ((q.second == i and e.first == ton) or (t == 0 and q.second == ton and e.first == i)) continue;
                    pq.push(PLI(-(ll(-q.first) + e.second), e.first));
                }
            }
            if (dist[i] < HIGHINF) chmin(ans, dist[i] + tow);
        }
    }
    if (ans == HIGHINF) ans = -1;
    cout << ans << '\n';
    return 0;
}
0