結果

問題 No.2915 辺更新価値最大化
ユーザー ecottea
提出日時 2024-10-04 22:26:56
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 23,544 bytes
コンパイル時間 5,295 ms
コンパイル使用メモリ 292,248 KB
最終ジャッジ日時 2025-02-24 15:25:51
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#ifndef HIDDEN_IN_VS //
//
#define _CRT_SECURE_NO_WARNINGS
//
#include <bits/stdc++.h>
using namespace std;
//
using ll = long long; using ull = unsigned long long; // -2^63 2^63 = 9e18int -2^31 2^31 = 2e9
using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>;
using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;
//
const double PI = acos(-1);
int DX[4] = { 1, 0, -1, 0 }; // 4
int DY[4] = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF;
//
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;
//
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x)))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x)))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 n-1
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s t
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s t
#define repe(v, a) for(const auto& v : (a)) // a
#define repea(v, a) for(auto& v : (a)) // a
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d
#define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} //
#define EXIT(a) {cout << (a) << endl; exit(0);} //
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) //
//
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // true
    
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // true
    
template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); }
template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // mod
//
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }
#endif //
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#ifdef _MSC_VER
#include "localACL.hpp"
#endif
//using mint = modint1000000007;
using mint = modint998244353;
//using mint = static_modint<100>;
//using mint = modint; // mint::set_mod(m);
namespace atcoder {
inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>;
#endif
#ifdef _MSC_VER // Visual Studio
#include "local.hpp"
#else // gcc
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; }
template <size_t N> inline int lsb(const bitset<N>& b) { return b._Find_first(); }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(...)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE MLE
#endif
// Union-Find
/*
* Potential_union_find<T>(int n) : O(n)
* n Union-Find
*
* bool set_diff(int a, int b, T d) : O(α(n))
* v[b] - v[a] = d false
*
* bool same(int a, int b) : O(α(n))
* a b
*
* T get_diff(int a, int b) : O(α(n))
* v[b] - v[a] INFL
*
* int leader(int a) : O(α(n))
* a
*
* int size(int a) : O(α(n))
* a
*
* int size() : O(1)
*
*
* vv<piT> groups() : O(n α(n))
* (, )
*/
template <class T>
class Potential_union_find {
int n; //
int m; //
// parent_or_size[i] : i
// i
// -1
vi parent_or_size;
// pot[i] : i
// i
vector<T> pot;
public:
// n Union-Find
Potential_union_find(int n_) : n(n_), m(n), parent_or_size(n, -1), pot(n) {
// verify : https://judge.yosupo.jp/problem/unionfind_with_potential
}
Potential_union_find() : n(0), m(0) {}
// a, b v[b] - v[a]
bool set_diff(int a, int b, T d) {
// verify : https://judge.yosupo.jp/problem/unionfind_with_potential
// a, b ra, rb
int ra = leader(a);
int rb = leader(b);
//
//
if (ra == rb) return pot[b] - pot[a] == d;
// ra rb
if (-parent_or_size[ra] < -parent_or_size[rb]) {
swap(a, b);
swap(ra, rb);
d *= -1;
}
// ra
parent_or_size[ra] += parent_or_size[rb];
parent_or_size[rb] = ra;
pot[rb] = pot[a] - pot[b] + d;
// 1
m--;
return true;
}
// a, b
bool same(int a, int b) {
// verify : https://judge.yosupo.jp/problem/unionfind_with_potential
//
return leader(a) == leader(b);
}
// v[b] - v[a]
T get_diff(int a, int b) {
// verify : https://judge.yosupo.jp/problem/unionfind_with_potential
if (!same(a, b)) return T(INFL);
//
return pot[b] - pot[a];
}
// a
int leader(int a) {
// a
int pa = parent_or_size[a];
if (pa < 0) return a;
// a a pa ra
// pa ra
int ra = leader(pa);
// a ra a ra
parent_or_size[a] = ra;
pot[a] += pot[pa];
return ra;
}
// a
int size(int a) {
// a 調
return -parent_or_size[leader(a)];
}
//
int size() const {
// verify : https://yukicoder.me/problems/no/2251
return m;
}
// (, )
vector<vector<pair<int, T>>> groups() {
// verify : https://atcoder.jp/contests/code-festival-2016-quala/tasks/codefestival_2016_qualA_d
vector<vector<pair<int, T>>> res(m);
vi r_to_i(n, -1); int i = 0;
rep(a, n) {
int r = leader(a);
if (r_to_i[r] == -1) r_to_i[r] = i++;
res[r_to_i[r]].emplace_back(a, pot[a]);
}
return res;
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, Potential_union_find d) {
repe(g, d.groups()) {
repe(v, g) os << v << " ";
os << endl;
}
return os;
}
#endif
};
//rollback Union-Find
/*
* Rollback_union_find(int n) : O(n)
* n Union-Find
*
* merge(int a, int b) : O(log n)
* a b
*
* bool same(int a, int b) : O(log n)
* a b
*
* int leader(int a) : O(log n)
* a
*
* int size(int a) : O(log n)
* a
*
* int size() : O(1)
*
*
* vvi groups() : O(n log n)
*
*
* snapshot() : O(1)
*
*
* rollback() : O(1)
*
*/
class Rollback_union_find {
// : https://snuke.hatenablog.com/entry/2016/07/01/000000
int n; //
int m; //
// parent_or_size[i] : i
// i
// -1
vi parent_or_size;
//
stack<pii> history;
public:
// n Union-Find
Rollback_union_find(int n) : n(n), m(n), parent_or_size(n, -1) {
// verify : https://codeforces.com/gym/100513/problem/A
}
Rollback_union_find() : n(0), m(0) {} //
// a, b
void merge(int a, int b) {
// verify : https://codeforces.com/gym/100513/problem/A
// a, b ra, rb
int ra = leader(a);
int rb = leader(b);
//
if (ra == rb) return;
// ra rb
if (-parent_or_size[ra] < -parent_or_size[rb]) swap(ra, rb);
//
history.emplace(ra, parent_or_size[ra]);
history.emplace(rb, parent_or_size[rb]);
// ra
parent_or_size[ra] += parent_or_size[rb];
parent_or_size[rb] = ra;
// 1
m--;
}
//
void snapshot() {
// verify : https://atcoder.jp/contests/abc302/tasks/abc302_h
history.emplace(INF, m);
}
//
void rollback() {
// verify : https://atcoder.jp/contests/abc302/tasks/abc302_h
while (true) {
auto [i, v] = history.top(); history.pop();
if (i == INF) {
m = v;
break;
}
parent_or_size[i] = v;
}
}
// a, b
bool same(int a, int b) {
// verify : https://codeforces.com/gym/100513/problem/A
//
return leader(a) == leader(b);
}
// a
int leader(int a) {
// a
int pa = parent_or_size[a];
if (pa < 0) return a;
// a a pa ra
int ra = leader(pa);
// rollback
return ra;
}
// a
int size(int a) {
// a 調
return -parent_or_size[leader(a)];
}
//
int size() {
return m;
}
//
vvi groups() {
vvi res(m);
vi r_to_i(n, -1); int i = 0;
rep(a, n) {
int r = leader(a);
if (r_to_i[r] == -1) r_to_i[r] = i++;
res[r_to_i[r]].push_back(a);
}
return res;
}
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, Rollback_union_find d) {
repe(g, d.groups()) {
repe(v, g) os << v << " ";
os << "/ ";
}
return os;
}
#endif
};
//offline dynamic connectivity
/*
* Offline_dynamic_connectivity(int n) : O(1)
* n
*
* add_edge(int u, int v) : O(1)
* u-v
*
* erase_edge(int u, int v) : O(1)
* u-v
*
* add_query(int id) : O(1)
* id
*
* solve(const function<void(int)>& f) : O(Q log Q log n)
*
* f(id) id
*
* rollback Union-Find
*/
class Offline_dynamic_connectivity {
// : https://ei1333.github.io/luzhiled/snippets/other/offline-dynamic-connectivity.html
int T = 1; //
vector<pii> es; // e = u-v u < v
vector<unordered_map<int, int>> e_id; // ID
vi e_cnt; //
vi appear; //
vector<tuple<int, int, int>> ex; // ID
vvi qs; //
vvi seg; //
// [l..r) id
void add_interval(int l, int r, int id) {
l += T;
r += T;
while (l < r) {
if (l & 1) {
seg[l].emplace_back(id);
l++;
}
if (r & 1) {
seg[r - 1].emplace_back(id);
}
l >>= 1;
r >>= 1;
}
}
//
void rf(int k, const function<void(int)>& f) {
uf.snapshot();
repe(id, seg[k]) {
auto [u, v] = es[id];
uf.merge(u, v);
}
if (k < T) {
rf(2 * k, f);
rf(2 * k + 1, f);
}
else {
repe(qid, qs[k - T]) f(qid);
}
uf.rollback();
}
public:
int n; //
Rollback_union_find uf;
// n
Offline_dynamic_connectivity(int n) : e_id(n), qs(1), n(n), uf(n) {
// verify : https://atcoder.jp/contests/abc301/tasks/abc301_h
}
Offline_dynamic_connectivity() : n(0) {}
// u-v
void add_edge(int u, int v) {
// verify : https://atcoder.jp/contests/abc301/tasks/abc301_h
if (u > v) swap(u, v);
auto it = e_id[u].find(v);
if (it == e_id[u].end()) {
e_id[u][v] = sz(es);
e_cnt.emplace_back(1);
appear.emplace_back(T);
es.emplace_back(u, v);
}
else {
int id = it->second;
if (e_cnt[id] == 0) appear[id] = T;
e_cnt[id]++;
}
qs.emplace_back(vi());
T++;
}
// u-v
void erase_edge(int u, int v) {
// verify : https://atcoder.jp/contests/abc301/tasks/abc301_h
if (u > v) swap(u, v);
Assert(e_id[u].count(v));
int id = e_id[u][v];
Assert(e_cnt[id] > 0);
e_cnt[id]--;
if (e_cnt[id] == 0) ex.emplace_back(appear[id], T, id);
qs.emplace_back(vi());
T++;
}
// id
void add_query(int id) {
// verify : https://atcoder.jp/contests/abc301/tasks/abc301_h
qs.back().emplace_back(id);
}
//
void solve(const function<void(int)>& f) {
// verify : https://atcoder.jp/contests/abc301/tasks/abc301_h
//
seg.resize(T * 2);
for (auto& [l, r, id] : ex) add_interval(l, r, id);
rep(id, sz(e_cnt)) if (e_cnt[id] > 0) add_interval(appear[id], T, id);
//
rf(1, f);
}
/* f
auto f = [&](int id) {
return res[id] = G.uf.same(u[id], v[id]);
};
*/
};
//
void WA() {
int n, m, q;
cin >> n >> m >> q;
vi u(m), v(m); vl w(m);
rep(j, m) cin >> u[j] >> v[j] >> w[j];
--u; --v;
Potential_union_find<ll> d(n);
rep(j, m) {
d.set_diff(u[j], v[j], w[j]);
}
vl pot(n);
auto cs = d.groups();
repe(c, cs) for (auto [i, p] : c) pot[i] = p;
vi ex(m, 1);
Offline_dynamic_connectivity O(n);
rep(j, m) O.add_edge(u[j], v[j]);
rep(j, q) {
int p;
cin >> p;
p--;
if (ex[p]) {
O.erase_edge(u[p], v[p]);
ex[p] = 0;
}
else {
O.add_edge(u[p], v[p]);
ex[p] = 1;
}
O.add_query(j);
}
vl res(q);
auto f = [&](int id) {
return res[id] = O.uf.same(0, n - 1) ? pot[n - 1] - pot[0] : -INFL;
};
O.solve(f);
rep(j, q) {
if (res[j] == -INFL) cout << "NaN" << "\n";
else cout << res[j] << "\n";
}
}
//
/*
* to :
* cost :
*/
struct WEdge {
// verify : https://judge.yosupo.jp/problem/shortest_path
int to; //
ll cost; //
WEdge() : to(-1), cost(-INFL) {}
WEdge(int to, ll cost) : to(to), cost(cost) {}
//
operator int() const { return to; }
#ifdef _MSC_VER
friend ostream& operator<<(ostream& os, const WEdge& e) {
os << '(' << e.to << ',' << e.cost << ')';
return os;
}
#endif
};
//
/*
* WGraph g
* g[v] : v
*
* verify : https://judge.yosupo.jp/problem/shortest_path
*/
using WGraph = vector<vector<WEdge>>;
//O(n m)
/*
* g
* st
* st
*/
vl bellman_ford(const WGraph& g, int st) {
// verify : https://onlinejudge.u-aizu.ac.jp/courses/library/5/GRL/all/GRL_1_B
//
// min-plus
int n = sz(g);
vl dist(n, INFL); //
dist[st] = 0;
rep(i, n) {
bool updated = false;
//
rep(s, n) {
repe(e, g[s]) {
// () + () < () ()
// INFL INFL
// st
if (dist[s] != INFL && dist[s] + e.cost < dist[e.to]) {
dist[e.to] = dist[s] + e.cost;
updated = true;
}
}
}
//
if (!updated) return dist;
}
// n
// st
return vl();
}
//O(n + m log n)
/*
* g u[0..n)
* st
*
* u[t] - u[s] ≦ ( s→t )
*/
vl potentialed_dijkstra(const WGraph& g, const vl& u, int st) {
// verify : https://atcoder.jp/contests/abc237/tasks/abc237_e
//
// g s u[s] s→t c[s][t]
// c[s][t] = (u[t] - u[s]) + r[s][t] (r[s][t] >= 0)
//
// Δu r
//
// s→...→t u u[t] - u[s]
// rot grad = 0
// r r
//
// 使
//
// O(2^n)
//
// https://theory-and-me.hatenablog.com/entry/2019/09/08/182442
int n = sz(g);
vl dist(n, INFL); //
dist[st] = 0;
// (, )
priority_queue_rev<pli> q;
q.push({ 0, st });
while (!q.empty()) {
auto [c, s] = q.top(); q.pop();
//
if (dist[s] < c) continue;
repe(e, g[s]) {
// r :
ll r = e.cost - (u[e.to] - u[s]);
// 辿
if (chmin(dist[e.to], dist[s] + r)) q.push({ dist[e.to], e.to });
}
}
// Δu
rep(i, n) dist[i] += u[i] - u[st];
return dist;
}
int main() {
// input_from_file("input.txt");
// output_to_file("output.txt");
int n, m, q;
cin >> n >> m >> q;
vi u(m), v(m); vl w(m);
rep(j, m) cin >> u[j] >> v[j] >> w[j];
--u; --v;
WGraph g(n);
rep(j, m) g[u[j]].push_back({ v[j], -w[j] });
dumpel(g);
auto pot = bellman_ford(g, 0);
dump(pot);
vi ex(m, 1);
rep(j, q) {
int p;
cin >> p;
p--;
ex[p] ^= 1;
WGraph g2(n);
rep(j, m) if (ex[j]) g2[u[j]].push_back({ v[j], -w[j] });
dumpel(g2);
auto dist = potentialed_dijkstra(g2, pot, 0);
dump(dist);
if (dist[n - 1] == INFL) cout << "NaN" << "\n";
else cout << -dist[n - 1] << "\n";
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0