結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tnakao0123tnakao0123
提出日時 2020-12-19 19:14:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 305 ms / 2,000 ms
コード長 2,159 bytes
コンパイル時間 1,470 ms
コンパイル使用メモリ 104,244 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 09:28:50
合計ジャッジ時間 5,686 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 4 ms
4,348 KB
testcase_09 AC 234 ms
4,348 KB
testcase_10 AC 5 ms
4,348 KB
testcase_11 AC 153 ms
4,348 KB
testcase_12 AC 21 ms
4,348 KB
testcase_13 AC 76 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 5 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 52 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 151 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 3 ms
4,348 KB
testcase_28 AC 4 ms
4,348 KB
testcase_29 AC 78 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 15 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 84 ms
4,348 KB
testcase_34 AC 51 ms
4,348 KB
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 3 ms
4,348 KB
testcase_39 AC 2 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 305 ms
4,348 KB
testcase_46 AC 12 ms
4,348 KB
testcase_47 AC 74 ms
4,348 KB
testcase_48 AC 71 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 14 ms
4,348 KB
testcase_53 AC 8 ms
4,348 KB
testcase_54 AC 102 ms
4,348 KB
testcase_55 AC 101 ms
4,348 KB
testcase_56 AC 102 ms
4,348 KB
testcase_57 AC 201 ms
4,348 KB
testcase_58 AC 202 ms
4,348 KB
testcase_59 AC 206 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1320.cc:  No.1320 Two Type Min Cost Cycle - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 2000;
const int MAX_M = 2000;
const long long LINF = 0x7f7f7f7f7f7f7f7fLL;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<pii> vpii;

struct Edge {
  int u, v, w;
  Edge() {}
  Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w) {}
};

/* global variables */

vpii nbrs[MAX_N];
Edge es[MAX_M];
ll ds[MAX_N];
int gs[MAX_N];

/* subroutines */

/* main */

int main() {
  int t, n, m;
  scanf("%d%d%d", &t, &n, &m);

  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);\
    u--, v--;
    nbrs[u].push_back(pii(v, w));
    if (t == 0) nbrs[v].push_back(pii(u, w));
    es[i] = Edge(u, v, w);
  }

  ll mind = LINF;

  for (int st = 0; st < n; st++) {
    memset(ds, 0x7f, sizeof(ds));
    ds[st] = 0;
    gs[st] = -1;

    priority_queue<pli> q;
    q.push(pli(0, st));

    while (! q.empty()) {
      pli u = q.top(); q.pop();
      ll ud = -u.first;
      int ui = u.second;
      if (ds[ui] != ud) continue;

      vpii &nbru = nbrs[ui];
      for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
	int vi = vit->first;
	ll vd = ud + vit->second;
	if (ds[vi] > vd) {
	  ds[vi] = vd;
	  gs[vi] = (ui == st) ? vi : gs[ui];
	  q.push(pli(-vd, vi));
	}
      }
    }

    for (int i = 0; i < m; i++) {
      Edge &e = es[i];
      if (t == 0) {
	if (ds[e.u] < LINF && ds[e.v] < LINF &&
	    gs[e.u] >= 0 && gs[e.v] >= 0 && gs[e.u] != gs[e.v]) {
	  ll d = ds[e.u] + ds[e.v] + e.w;
	  if (mind > d) mind = d;
	}
      }
      else {
	if (ds[e.u] < LINF && e.v == st) {
	  ll d = ds[e.u] + e.w;
	  if (mind > d) mind = d;
	}
      }
    }
  }

  printf("%lld\n", (mind >= LINF) ? -1LL : mind);
  return 0;
}
0