結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー milanis48663220milanis48663220
提出日時 2020-12-17 00:52:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,253 bytes
コンパイル時間 1,550 ms
コンパイル使用メモリ 107,776 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 10:10:40
合計ジャッジ時間 2,764 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 2 ms
4,348 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 2 ms
4,348 KB
testcase_05 RE -
testcase_06 AC 2 ms
4,348 KB
testcase_07 WA -
testcase_08 RE -
testcase_09 RE -
testcase_10 WA -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 WA -
testcase_16 RE -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 RE -
testcase_21 RE -
testcase_22 AC 2 ms
4,348 KB
testcase_23 WA -
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 RE -
testcase_28 RE -
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 RE -
testcase_34 RE -
testcase_35 WA -
testcase_36 WA -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 WA -
testcase_42 RE -
testcase_43 WA -
testcase_44 WA -
testcase_45 RE -
testcase_46 AC 2 ms
4,348 KB
testcase_47 RE -
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
権限があれば一括ダウンロードができます

ソースコード

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 n, m;
vector<edge> G[2000];
ll d[2000];
bool used[2000];
set<int> prevv[2000];

ll ans = INF;
void dijkstra(int s){
    priority_queue<P, vector<P>, greater<P>> que;
    d[s] = 0;
    que.push(P(0, s));
    while(!que.empty()){
        P p = que.top();que.pop();
        int v = p.second;
        used[v] = true;
        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(used[e.to]){
                if(prevv[v].size() == 1){
                    if(prevv[v].count(e.to) == 0)chmin(ans, d[v] + e.cost - d[e.to]);
                }else{
                    chmin(ans, d[v] + e.cost - d[e.to]);
                }
            }
        }
    }
}

void solve(){
    for(int i = 0; i < n; i++) d[i] = INF;
    for(int i = 0; i < n; i++){
        if(d[i] == INF) dijkstra(i);
    }
    if(ans == INF){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int t; 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});
    }
    if(t == 0) return -1;
    solve();
}
0