結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tonegawatonegawa
提出日時 2021-02-12 08:14:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,776 bytes
コンパイル時間 1,800 ms
コンパイル使用メモリ 133,304 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-26 04:20:24
合計ジャッジ時間 7,994 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1 ms
4,384 KB
testcase_23 WA -
testcase_24 AC 2 ms
4,380 KB
testcase_25 WA -
testcase_26 AC 2 ms
4,384 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 OLE -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 OLE -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 2 ms
4,384 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 OLE -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
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, 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;
          std::cout << v << " " << e.to << " " << e.idx << '\n';
          pre[e.to] = &e;
          q.push(p(dist[e.to], e.to));
        }
      }
    }
    /*if use undirected edge*/
    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);
  if(ans.second.empty()) printf("%d\n", -1);
  else printf("%lld\n", ans.first);
}
0