結果

問題 No.1602 With Animals into Institute 2
ユーザー ei1333333ei1333333
提出日時 2021-07-10 14:55:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,847 bytes
コンパイル時間 2,827 ms
コンパイル使用メモリ 233,944 KB
実行使用メモリ 814,620 KB
最終ジャッジ日時 2023-09-14 19:29:29
合計ジャッジ時間 6,504 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 MLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = 1e9 + 7;
// const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

struct edge {
  int to, cost, type, idx;
};
using Graph = vector< vector< edge > >;

/**
 * @brief Union-Find
 * @docs docs/union-find.md
 */
struct UnionFind {
  vector< int > data;

  UnionFind() = default;

  explicit UnionFind(size_t sz) : data(sz, -1) {}

  bool unite(int x, int y) {
    x = find(x), y = find(y);
    if(x == y) return false;
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return true;
  }

  int find(int k) {
    if(data[k] < 0) return (k);
    return data[k] = find(data[k]);
  }

  int size(int k) {
    return -data[find(k)];
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }
};

struct sssp {
  vector< int64 > dist;
  vector< int > parent, depth;
};

sssp dijkstra(const Graph &g, int s) {
  int n = (int) g.size();
  vector< int64 > dist(n, infll);
  vector< int > depth(n), parent(n);
  using pi = pair< int64, int >;
  priority_queue< pi, vector< pi >, greater<> > que;
  que.emplace(0, s);
  dist[s] = 0;
  depth[s] = 0;
  while(not que.empty()) {
    auto[d, u] = que.top();
    que.pop();
    if(dist[u] != d) {
      continue;
    }
    for(auto e : g[u]) {
      int v = e.to;
      int64 d2 = d + e.cost;
      if(dist[v] > d2) {
        dist[v] = d2;
        depth[v] = depth[u] + 1;
        parent[v] = u;
        que.emplace(d2, v);
      }
    }
  }
  return (sssp) {dist, parent, depth};
}

vector< int64 > solve(const Graph &g, int s) {
  int n = (int) g.size();
  vector< int64 > dist(n, infll);
  UnionFind uf(n);
  using pi = tuple< int64, int, int >;
  priority_queue< pi, vector< pi >, greater<> > que;
  auto sp = dijkstra(g, s);
  vector< vector< int > > is_consistent(n);

  {
    queue< pair< int, int > > que2;
    que2.emplace(s, 0);
    vector< int > psi(n);
    while(not que2.empty()) {
      auto[u, bit] = que2.front();
      que2.pop();
      bool add = false;
      for(auto e : g[u]) {
        int v = e.to;
        if(sp.parent[v] == u and sp.dist[v] == sp.dist[u] + e.cost) {
          que2.emplace(v, bit ^ e.type);
          psi[e.to] = bit ^ e.type;
          add = true;
          if(psi[v]) {
            dist[v] = sp.dist[v];
          }
          break;
        }
      }
    }
    for(int u = 0; u < n; u++) {
      for(auto e : g[u]) {
        int v = e.to;
        is_consistent[u].emplace_back((psi[u] ^ e.type) == psi[v]);
      }
    }
  }

  for(int u = 0; u < n; u++) {
    int i = 0;
    for(auto &e : g[u]) {
      int v = e.to;
      if(u < v and not is_consistent[u][i]) {
        que.emplace(sp.dist[u] + sp.dist[v] + e.cost, u, i);
      }
      ++i;
    }
  }
  while(not que.empty()) {
    auto[h, u0, i] = que.top();
    que.pop();
    int v0 = g[u0][i].to;
    int u = uf.find(u0);
    int v = uf.find(v0);
    vector< int > bs;
    while(u != v) {
      if(sp.depth[u] > sp.depth[v]) {
        bs.emplace_back(u);
        u = uf.find(sp.parent[u]);
      } else {
        bs.emplace_back(v);
        v = uf.find(sp.parent[v]);
      }
    }
    for(int x : bs) {
      uf.unite(u, x);
      chmin(dist[x], h - sp.dist[x]);
      int ptr = 0;
      for(auto &e : g[x]) {
        if(is_consistent[x][ptr]) {
          que.emplace(dist[x] + sp.dist[e.to] + e.cost, x, ptr);
        }
        ++ptr;
      }
    }
  }
  return dist;
}

int main() {
  int N, M, K;
  cin >> N >> M >> K;
  Graph g(N);
  for(int i = 0; i < M; i++) {
    int a, b, c;
    string s;
    cin >> a >> b >> c >> s;
    --a, --b;
    int d = 0;
    for(int j = 0; j < K; j++) {
      if(s[j] == '1') d |= 1 << j;
    }
    g[a].emplace_back((edge) {b, c, d, i});
    g[b].emplace_back((edge) {a, c, d, i});
  }
  auto shortest_path = solve(g, N - 1);
  for(int i = 0; i + 1 < N; i++) {
    if(shortest_path[i] >= infll) cout << -1 << "\n";
    else cout << shortest_path[i] << "\n";
  }
}
0