結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tonegawatonegawa
提出日時 2021-02-12 08:19:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 3,769 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 133,680 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-26 04:29:07
合計ジャッジ時間 4,680 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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, bool undirected){
  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, 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(undirected) if(pre[v] != nullptr && pre[v]->idx == e.idx) continue;
        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(undirected){
      edge *ud = nullptr;
      for(int j=i+1;j<n;j++){
        for(edge &e:G[j]){
          if(e.to<=i || dist[e.from] == INF || dist[e.to] == INF || (pre[e.to] && pre[e.to]->idx == e.idx) || (pre[e.from] && pre[e.from]->idx == e.idx)) continue;
          if(ans > dist[e.from] + dist[e.to] + e.w){
            ans = dist[e.from] + dist[e.to] + e.w;
            ud = &e;
          }
        }
      }
      if(ud){
        vector<edge*> left{ud};
        int tmp = ud->from;
        while(pre[tmp]&&tmp!=i){
          left.push_back(pre[tmp]);
          tmp = pre[tmp]->from;
        }
        reverse(left.begin(), left.end());
        tmp = ud->to;
        while(pre[tmp]&&tmp!=i){
          assert(pre[tmp]);
          left.push_back(pre[tmp]);
          tmp = pre[tmp]->from;
        }
        min_cycle = left;
      }
    }
    if(idx != -1){
      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, !t);
  if(ans.second.empty()) printf("%d\n", -1);
  else printf("%lld\n", ans.first);
}
0