結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー milanis48663220milanis48663220
提出日時 2020-12-17 02:00:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,278 bytes
コンパイル時間 1,101 ms
コンパイル使用メモリ 108,208 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 10:27:23
合計ジャッジ時間 8,019 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 3 ms
4,348 KB
testcase_07 AC 11 ms
4,348 KB
testcase_08 AC 12 ms
4,348 KB
testcase_09 WA -
testcase_10 AC 12 ms
4,348 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 11 ms
4,348 KB
testcase_15 AC 12 ms
4,348 KB
testcase_16 WA -
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 7 ms
4,348 KB
testcase_19 AC 85 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 231 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 1 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 9 ms
4,348 KB
testcase_29 AC 196 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 33 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 WA -
testcase_38 AC 3 ms
4,348 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 12 ms
4,348 KB
testcase_44 AC 7 ms
4,348 KB
testcase_45 AC 565 ms
4,348 KB
testcase_46 AC 24 ms
4,348 KB
testcase_47 AC 172 ms
4,348 KB
testcase_48 AC 181 ms
4,348 KB
testcase_49 AC 19 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 AC 178 ms
4,348 KB
testcase_55 AC 179 ms
4,348 KB
testcase_56 AC 179 ms
4,348 KB
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const ll INF = 1e+15;

typedef pair<ll, int> P;
typedef pair<int, int> Pii;

struct edge{
    int to;
    ll cost;
};

int t, n, m;
vector<edge> G[2000];
ll d[2000];
set<int> prevv[2000];

ll ans = INF;
void dijkstra(int s){
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d, d+n, INF);
    d[s] = 0;
    for(int i = 0; i < n; i++) prevv[i].clear();
    que.push(P(0, s));
    while(!que.empty()){
        P p = que.top();que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        for(edge e : G[v]){
            if(d[v] + e.cost < d[e.to]){
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
                prevv[e.to].clear();
                prevv[e.to].insert(v);
            }else if(d[v] + e.cost < d[e.to]){
                prevv[e.to].insert(v);
            }
            if(t == 1 && e.to == s){
                chmin(ans, d[v]+e.cost);
            }
        }
    }
}

void solve(){
    for(int i = 0; i < n; i++){
        dijkstra(i);
        if(t == 1) continue;
        for(edge e : G[i]){
            if(prevv[e.to].size() == 1){
                if(prevv[e.to].count(i) == 0) chmin(ans, d[e.to]+e.cost);
            }else{
                chmin(ans, d[e.to]+e.cost);
            }
        }
    }
    if(ans == INF){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> t >> n >> m;
    for(int i = 0; i < m; i++){
        int u, v; ll w;
        cin >> u >> v >> w;
        u--; v--;
        G[u].push_back((edge){v, w});
        if(t == 0) G[v].push_back((edge){u, w});
    }
    solve();
}
0