結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー milanis48663220milanis48663220
提出日時 2020-12-17 02:42:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 2,277 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 102,400 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 10:31:32
合計ジャッジ時間 5,042 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 4 ms
4,348 KB
testcase_08 AC 12 ms
4,348 KB
testcase_09 AC 246 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 169 ms
4,348 KB
testcase_12 AC 32 ms
4,348 KB
testcase_13 AC 83 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 25 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 46 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 231 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 6 ms
4,348 KB
testcase_29 AC 75 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 147 ms
4,348 KB
testcase_34 AC 66 ms
4,348 KB
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 3 ms
4,348 KB
testcase_38 AC 20 ms
4,348 KB
testcase_39 AC 5 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 4 ms
4,348 KB
testcase_44 AC 4 ms
4,348 KB
testcase_45 AC 215 ms
4,348 KB
testcase_46 AC 10 ms
4,348 KB
testcase_47 AC 77 ms
4,348 KB
testcase_48 AC 68 ms
4,348 KB
testcase_49 AC 8 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 19 ms
4,348 KB
testcase_53 AC 11 ms
4,348 KB
testcase_54 AC 100 ms
4,348 KB
testcase_55 AC 101 ms
4,348 KB
testcase_56 AC 101 ms
4,348 KB
testcase_57 AC 214 ms
4,348 KB
testcase_58 AC 216 ms
4,348 KB
testcase_59 AC 215 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

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];
int v[2000], u[2000];
ll w[2000];

ll ans = INF;
void dijkstra(int s){
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d, d+n, INF);
    d[s] = 0;
    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));
            }
            if(t == 1 && e.to == s){
                chmin(ans, d[v]+e.cost);
            }
            
        }
    }
}

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

void solve_undi(){
    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++) G[j].clear();
        for(int j = 0; j < m; j++) {
            if(i == j) continue;
            G[u[j]].push_back((edge){v[j], w[j]});
            G[v[j]].push_back((edge){u[j], w[j]});
        }
        dijkstra(v[i]);
        chmin(ans, d[u[i]]+w[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;
    cin >> t >> n >> m;
    for(int i = 0; i < m; i++){
        cin >> u[i] >> v[i] >> w[i];
        u[i]--; v[i]--;
        if(t == 1)G[u[i]].push_back((edge){v[i], w[i]});
    }
    if(t == 0) solve_undi();
    else solve_di();
}
0