結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tonegawatonegawa
提出日時 2021-02-12 07:36:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,892 bytes
コンパイル時間 1,329 ms
コンパイル使用メモリ 133,704 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-26 00:01:08
合計ジャッジ時間 5,473 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

/*---tips---
  重み付きの有向グラフについて、重みの和が最小になるサイクルを探す O(N(N + M)logN)
  自己辺は閉路と見なさない(含める場合、e.from = e.toの辺を列挙するだけでいい)
  多重辺があっても良い

  無向グラフに使う場合、edgeのidxにuniqueな値を持たせて逆流できないようにすればいい
  verify:
*/
struct edge{
  int from, to, idx;
  ll w;
  edge(int f, int t, int i, ll w=1):from(f), to(t), idx(i), w(w){}
};
pair<ll, vector<edge*>> mincost_cycle(vector<vector<edge>> &G){
  int n = G.size();
  ll INF = numeric_limits<ll>::max(), ans = INF;
  vector<edge*> min_cycle;
  vector<ll> dist(n);
  vector<edge*> pre(n);
  for(int i=0;i<n;i++){
    fill(dist.begin()+i+1, dist.end(), INF);
    fill(pre.begin()+i+1, pre.end(), nullptr);
    dist[i] = 0;
    using p = pair<ll, int>;
    priority_queue<p, vector<p>, greater<p>> q;
    q.push({0, i});
    int idx = -1;
    while(!q.empty()){
      p p_now = q.top(); q.pop();
      int v = p_now.second;
      if(dist[v] == INF || dist[v] < p_now.first || ans <= dist[v]) continue;
      for(edge &e:G[v]){
        if(pre[v] != nullptr && pre[v]->idx == e.idx) continue; //if use undirected edge
        if(e.to==i && dist[v] + e.w < ans){
          ans = dist[v] + e.w;
          idx = v;
          pre[i] = &e;
          continue;
        }
        if(e.to<i) continue;
        if(dist[e.to] > dist[v] + e.w){
          dist[e.to] = dist[v] + e.w;
          pre[e.to] = &e;
          q.push(p(dist[e.to], e.to));
        }
      }
    }
    if(idx == -1) continue;
    vector<edge*> lis;
    idx = i;
    while(pre[idx]){
      lis.push_back(pre[idx]);
      int k = pre[idx]->from;
      pre[idx] = nullptr;
      idx = k;
    }
    reverse(lis.begin(), lis.end());
    min_cycle = lis;
  }
  return {ans, min_cycle};
}

int main(){
  int t;scanf("%d", &t);
  int n, m;scanf("%d %d", &n, &m);
  vector<vector<edge>> G(n);
  for(int i=0;i<m;i++){
    int a, b, c;scanf("%d %d %d", &a, &b, &c);
    a--, b--;
    G[a].push_back(edge(a, b, i, c));
    if(t==0) G[b].push_back(edge(b, a, i, c));
  }
  auto ans = mincost_cycle(G);
  if(ans.second.empty()) printf("%d\n", -1);
  else printf("%lld\n", ans.first);
}
0