結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tnakao0123tnakao0123
提出日時 2020-12-19 18:43:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,615 bytes
コンパイル時間 803 ms
コンパイル使用メモリ 98,700 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 09:26:30
合計ジャッジ時間 4,771 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 long long LINF = 0x7f7f7f7f7f7f7f7fLL;

/* typedef */

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

/* global variables */

vpii nbrs[MAX_N];
int ps[MAX_N], cis[MAX_N];
ll ds[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));
  }

  ll mind = LINF;

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

    for (int u = st; u >= 0;) {
      vpii &nbru = nbrs[u];
      if (cis[u] < nbru.size() && nbru[cis[u]].first == ps[u]) cis[u]++;
      if (cis[u] < nbru.size()) {
	pii e = nbru[cis[u]++];
	int v = e.first;
	ll vd = ds[u] + e.second;
	if (ds[v] < LINF) {
	  ll d = vd - ds[v];
	  if (mind > d) mind = d;
	}
	else {
	  ps[v] = u;
	  ds[v] = vd;
	  u = v;
	}
      }
      else {
	cis[u] = 0;
	ds[u] = LINF;
	u = ps[u];
      }
    }
  }

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