답안 #231223

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
231223 2020-05-13T05:32:28 Z xiaowuc1 Wild Boar (JOI18_wild_boar) C++14
100 / 100
9625 ms 639964 KB
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_set>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define derr if(1) cerr
typedef vector<int> vi;
// END NO SAD

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

struct edge {
  int from, to, w, id;
  edge(){}
  edge(int a, int b, int c, int d) {
    from=a;
    to=b;
    w=c;
    id=d;
  }
};
struct path {
  int f, l;
  ll w;
  path(){clear();}
  path(int a, int b, ll c) {
    f = a;
    l = b;
    w = c;
  }
  void clear() {
    f = -1;
    w=1e18;
  }
  bool valid() {
    return f >= 0;
  }
  bool operator==(const path& p) {
    return f == p.f && l==p.l && w==p.w;
  }
};
const ll INF = 1e14;
vector<int> edges[2000];
edge alledges[4000];
ll dp[4000][4000];
vector<path> bestpaths[2000][2000];
int n, m;

void calculatebestpaths(vector<path>& paths) {
  vector<path> ret(5);
  for(path out: paths) {
    if(out.w < ret[0].w) ret[0] = out;
  }
  // ret[1] doesn't match source edge
  for(path out: paths) {
    if(out.f == ret[0].f) continue;
    if(out.w < ret[1].w) ret[1] = out;
  }
  // ret[2] doesn't match target
  for(path out: paths) {
    if(out.l == ret[0].l) continue;
    if(out == ret[1]) continue;
    if(out.w < ret[2].w) ret[2] = out;
  }
  // ret[3] doesn't match source or ret[1] target
  for(path out: paths) {
    if(out.f == ret[0].f) continue;
    if(out.l == ret[1].l) continue;
    if(out.w < ret[3].w) ret[3] = out;
  }
  // ret[4] doesn't match target or ret[2] source
  for(path out: paths) {
    if(out.l == ret[0].l) continue;
    if(out.f == ret[2].f) continue;
    if(out.w < ret[4].w) ret[4] = out;
  }
  for(int i = 0; i < sz(ret); i++) {
    if(!ret[i].valid()) ret.erase(ret.begin() + i--);
  }
  paths.swap(ret);
}

void merge(vector<path>& ret, const vector<path>& a, const vector<path>& b) {
  ret.clear();
  for(path lhs: a) {
    for(path rhs: b) {
      if(alledges[lhs.l].to != alledges[rhs.f].from) continue;
      if((lhs.l^rhs.f) == 1) continue;
      ret.push_back(path(lhs.f, rhs.l, lhs.w + rhs.w));
    }
  }
  calculatebestpaths(ret);
}

const int KOOSAGA_SZ = 1 << 17;
vector<path> koosagatree[2 * KOOSAGA_SZ];
int numloc;
void upd(int idx) {
  idx += KOOSAGA_SZ;
  // assumes already set
  while(idx > 1) {
    idx /= 2;
    merge(koosagatree[idx], koosagatree[2*idx], koosagatree[2*idx+1]);
  }
}
ll qry() {
  int lhs = KOOSAGA_SZ;
  int rhs = KOOSAGA_SZ + numloc - 2;
  deque<vector<path>> lq, rq;
  while(lhs <= rhs) {
    if(lhs == rhs) {
      lq.push_back(koosagatree[lhs]);
      break;
    }
    if(lhs%2) lq.push_back(koosagatree[lhs++]);
    if(rhs%2==0) rq.push_front(koosagatree[rhs--]);
    lhs /= 2;
    rhs /= 2;
  }
  while(sz(rq)) {
    lq.push_back(rq.front());
    rq.pop_front();
  }
  assert(sz(lq));
  while(sz(lq) > 1) {
    auto a = lq.front(); lq.pop_front();
    auto b = lq.front(); lq.pop_front();
    vector<path> c;
    merge(c, a, b);
    lq.push_front(c);
  }
  if(sz(lq[0]) == 0) return -1;
  return lq[0].front().w;
}

void dijkstra(int srcid) {
  dp[srcid][srcid] = alledges[srcid].w;
  typedef pair<ll, int> pli;
  priority_queue<pli> q;
  q.push({-dp[srcid][srcid], srcid});
  while(sz(q)) {
    ll ww;
    int edgeid;
    tie(ww, edgeid) = q.top(); q.pop();
    ww *= -1;
    if(dp[srcid][edgeid] != ww) continue;
    for(int outid: edges[alledges[edgeid].to]) {
      if((edgeid^outid) == 1) continue;
      if(dp[srcid][outid] > dp[srcid][edgeid] + alledges[outid].w) {
        dp[srcid][outid] = dp[srcid][edgeid] + alledges[outid].w;
        q.push({-dp[srcid][outid], outid});
      }
    }
  }
}

int locs[100000];
void solve() {
  memset(dp, 1, sizeof(dp));
  int t;
  cin >> n >> m >> t >> numloc;
  for(int i = 0; i < m; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    a--; b--;
    alledges[2*i] = edge(a, b, w, 2*i);
    alledges[2*i+1] = edge(b, a, w, 2*i+1);
    edges[a].push_back(2*i);
    edges[b].push_back(2*i+1);
  }
  for(int i = 0; i < 2*m; i++) dijkstra(i);
  for(int i = 0; i < 2*m; i++) {
    int srcnode = alledges[i].from;
    for(int j = 0; j < 2*m; j++) {
      if(dp[i][j] > INF) continue;
      int destnode = alledges[j].to;
      bestpaths[srcnode][destnode].push_back(path(i, j, dp[i][j]));
    }
  }
  for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) {
    calculatebestpaths(bestpaths[i][j]);
  }
  for(int i = 0; i < numloc; i++) {
    cin >> locs[i];
    locs[i]--;
  }
  for(int i = 0; i + 1 < numloc; i++) {
    koosagatree[KOOSAGA_SZ + i] = bestpaths[locs[i]][locs[i+1]];
  }
  for(int x = KOOSAGA_SZ-1; x > 0; x--) {
    merge(koosagatree[x], koosagatree[2*x], koosagatree[2*x+1]);
  }
  while(t--) {
    int idx, val;
    cin >> idx >> val;
    locs[--idx] = --val;
    if(idx > 0) {
      koosagatree[KOOSAGA_SZ + idx - 1] = bestpaths[locs[idx-1]][locs[idx]];
      upd(idx-1);
    }
    if(idx + 1 < numloc) {
      koosagatree[KOOSAGA_SZ + idx] = bestpaths[locs[idx]][locs[idx+1]];
      upd(idx);
    }
    cout << qry() << "\n";
  }
}

// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 238072 KB Output is correct
2 Correct 126 ms 238076 KB Output is correct
3 Correct 125 ms 238060 KB Output is correct
4 Correct 127 ms 238072 KB Output is correct
5 Correct 125 ms 238200 KB Output is correct
6 Correct 121 ms 238076 KB Output is correct
7 Correct 123 ms 238200 KB Output is correct
8 Correct 129 ms 238072 KB Output is correct
9 Correct 123 ms 238072 KB Output is correct
10 Correct 122 ms 238072 KB Output is correct
11 Correct 122 ms 238072 KB Output is correct
12 Correct 127 ms 238024 KB Output is correct
13 Correct 126 ms 238076 KB Output is correct
14 Correct 128 ms 238072 KB Output is correct
15 Correct 127 ms 238200 KB Output is correct
16 Correct 129 ms 238072 KB Output is correct
17 Correct 125 ms 238132 KB Output is correct
18 Correct 140 ms 238072 KB Output is correct
19 Correct 125 ms 238072 KB Output is correct
20 Correct 126 ms 238072 KB Output is correct
21 Correct 125 ms 238072 KB Output is correct
22 Correct 126 ms 238200 KB Output is correct
23 Correct 125 ms 238084 KB Output is correct
24 Correct 126 ms 237976 KB Output is correct
25 Correct 124 ms 238072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 238072 KB Output is correct
2 Correct 126 ms 238076 KB Output is correct
3 Correct 125 ms 238060 KB Output is correct
4 Correct 127 ms 238072 KB Output is correct
5 Correct 125 ms 238200 KB Output is correct
6 Correct 121 ms 238076 KB Output is correct
7 Correct 123 ms 238200 KB Output is correct
8 Correct 129 ms 238072 KB Output is correct
9 Correct 123 ms 238072 KB Output is correct
10 Correct 122 ms 238072 KB Output is correct
11 Correct 122 ms 238072 KB Output is correct
12 Correct 127 ms 238024 KB Output is correct
13 Correct 126 ms 238076 KB Output is correct
14 Correct 128 ms 238072 KB Output is correct
15 Correct 127 ms 238200 KB Output is correct
16 Correct 129 ms 238072 KB Output is correct
17 Correct 125 ms 238132 KB Output is correct
18 Correct 140 ms 238072 KB Output is correct
19 Correct 125 ms 238072 KB Output is correct
20 Correct 126 ms 238072 KB Output is correct
21 Correct 125 ms 238072 KB Output is correct
22 Correct 126 ms 238200 KB Output is correct
23 Correct 125 ms 238084 KB Output is correct
24 Correct 126 ms 237976 KB Output is correct
25 Correct 124 ms 238072 KB Output is correct
26 Correct 125 ms 238072 KB Output is correct
27 Correct 148 ms 243448 KB Output is correct
28 Correct 143 ms 243320 KB Output is correct
29 Correct 294 ms 249172 KB Output is correct
30 Correct 292 ms 249104 KB Output is correct
31 Correct 282 ms 249072 KB Output is correct
32 Correct 283 ms 249008 KB Output is correct
33 Correct 299 ms 250572 KB Output is correct
34 Correct 299 ms 250616 KB Output is correct
35 Correct 265 ms 250348 KB Output is correct
36 Correct 284 ms 250468 KB Output is correct
37 Correct 313 ms 250616 KB Output is correct
38 Correct 298 ms 252444 KB Output is correct
39 Correct 285 ms 252280 KB Output is correct
40 Correct 304 ms 252592 KB Output is correct
41 Correct 324 ms 252524 KB Output is correct
42 Correct 271 ms 253560 KB Output is correct
43 Correct 293 ms 254840 KB Output is correct
44 Correct 326 ms 254712 KB Output is correct
45 Correct 204 ms 254884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 238072 KB Output is correct
2 Correct 126 ms 238076 KB Output is correct
3 Correct 125 ms 238060 KB Output is correct
4 Correct 127 ms 238072 KB Output is correct
5 Correct 125 ms 238200 KB Output is correct
6 Correct 121 ms 238076 KB Output is correct
7 Correct 123 ms 238200 KB Output is correct
8 Correct 129 ms 238072 KB Output is correct
9 Correct 123 ms 238072 KB Output is correct
10 Correct 122 ms 238072 KB Output is correct
11 Correct 122 ms 238072 KB Output is correct
12 Correct 127 ms 238024 KB Output is correct
13 Correct 126 ms 238076 KB Output is correct
14 Correct 128 ms 238072 KB Output is correct
15 Correct 127 ms 238200 KB Output is correct
16 Correct 129 ms 238072 KB Output is correct
17 Correct 125 ms 238132 KB Output is correct
18 Correct 140 ms 238072 KB Output is correct
19 Correct 125 ms 238072 KB Output is correct
20 Correct 126 ms 238072 KB Output is correct
21 Correct 125 ms 238072 KB Output is correct
22 Correct 126 ms 238200 KB Output is correct
23 Correct 125 ms 238084 KB Output is correct
24 Correct 126 ms 237976 KB Output is correct
25 Correct 124 ms 238072 KB Output is correct
26 Correct 125 ms 238072 KB Output is correct
27 Correct 148 ms 243448 KB Output is correct
28 Correct 143 ms 243320 KB Output is correct
29 Correct 294 ms 249172 KB Output is correct
30 Correct 292 ms 249104 KB Output is correct
31 Correct 282 ms 249072 KB Output is correct
32 Correct 283 ms 249008 KB Output is correct
33 Correct 299 ms 250572 KB Output is correct
34 Correct 299 ms 250616 KB Output is correct
35 Correct 265 ms 250348 KB Output is correct
36 Correct 284 ms 250468 KB Output is correct
37 Correct 313 ms 250616 KB Output is correct
38 Correct 298 ms 252444 KB Output is correct
39 Correct 285 ms 252280 KB Output is correct
40 Correct 304 ms 252592 KB Output is correct
41 Correct 324 ms 252524 KB Output is correct
42 Correct 271 ms 253560 KB Output is correct
43 Correct 293 ms 254840 KB Output is correct
44 Correct 326 ms 254712 KB Output is correct
45 Correct 204 ms 254884 KB Output is correct
46 Correct 376 ms 248312 KB Output is correct
47 Correct 6425 ms 554624 KB Output is correct
48 Correct 5835 ms 551860 KB Output is correct
49 Correct 5115 ms 578664 KB Output is correct
50 Correct 5363 ms 566976 KB Output is correct
51 Correct 6004 ms 565320 KB Output is correct
52 Correct 4344 ms 592988 KB Output is correct
53 Correct 4322 ms 595536 KB Output is correct
54 Correct 4316 ms 590812 KB Output is correct
55 Correct 4341 ms 593912 KB Output is correct
56 Correct 4384 ms 596212 KB Output is correct
57 Correct 4555 ms 595824 KB Output is correct
58 Correct 4464 ms 597240 KB Output is correct
59 Correct 4372 ms 601720 KB Output is correct
60 Correct 4384 ms 597860 KB Output is correct
61 Correct 4346 ms 597384 KB Output is correct
62 Correct 4258 ms 589720 KB Output is correct
63 Correct 4119 ms 583176 KB Output is correct
64 Correct 1898 ms 636756 KB Output is correct
65 Correct 1907 ms 636792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 238072 KB Output is correct
2 Correct 126 ms 238076 KB Output is correct
3 Correct 125 ms 238060 KB Output is correct
4 Correct 127 ms 238072 KB Output is correct
5 Correct 125 ms 238200 KB Output is correct
6 Correct 121 ms 238076 KB Output is correct
7 Correct 123 ms 238200 KB Output is correct
8 Correct 129 ms 238072 KB Output is correct
9 Correct 123 ms 238072 KB Output is correct
10 Correct 122 ms 238072 KB Output is correct
11 Correct 122 ms 238072 KB Output is correct
12 Correct 127 ms 238024 KB Output is correct
13 Correct 126 ms 238076 KB Output is correct
14 Correct 128 ms 238072 KB Output is correct
15 Correct 127 ms 238200 KB Output is correct
16 Correct 129 ms 238072 KB Output is correct
17 Correct 125 ms 238132 KB Output is correct
18 Correct 140 ms 238072 KB Output is correct
19 Correct 125 ms 238072 KB Output is correct
20 Correct 126 ms 238072 KB Output is correct
21 Correct 125 ms 238072 KB Output is correct
22 Correct 126 ms 238200 KB Output is correct
23 Correct 125 ms 238084 KB Output is correct
24 Correct 126 ms 237976 KB Output is correct
25 Correct 124 ms 238072 KB Output is correct
26 Correct 125 ms 238072 KB Output is correct
27 Correct 148 ms 243448 KB Output is correct
28 Correct 143 ms 243320 KB Output is correct
29 Correct 294 ms 249172 KB Output is correct
30 Correct 292 ms 249104 KB Output is correct
31 Correct 282 ms 249072 KB Output is correct
32 Correct 283 ms 249008 KB Output is correct
33 Correct 299 ms 250572 KB Output is correct
34 Correct 299 ms 250616 KB Output is correct
35 Correct 265 ms 250348 KB Output is correct
36 Correct 284 ms 250468 KB Output is correct
37 Correct 313 ms 250616 KB Output is correct
38 Correct 298 ms 252444 KB Output is correct
39 Correct 285 ms 252280 KB Output is correct
40 Correct 304 ms 252592 KB Output is correct
41 Correct 324 ms 252524 KB Output is correct
42 Correct 271 ms 253560 KB Output is correct
43 Correct 293 ms 254840 KB Output is correct
44 Correct 326 ms 254712 KB Output is correct
45 Correct 204 ms 254884 KB Output is correct
46 Correct 376 ms 248312 KB Output is correct
47 Correct 6425 ms 554624 KB Output is correct
48 Correct 5835 ms 551860 KB Output is correct
49 Correct 5115 ms 578664 KB Output is correct
50 Correct 5363 ms 566976 KB Output is correct
51 Correct 6004 ms 565320 KB Output is correct
52 Correct 4344 ms 592988 KB Output is correct
53 Correct 4322 ms 595536 KB Output is correct
54 Correct 4316 ms 590812 KB Output is correct
55 Correct 4341 ms 593912 KB Output is correct
56 Correct 4384 ms 596212 KB Output is correct
57 Correct 4555 ms 595824 KB Output is correct
58 Correct 4464 ms 597240 KB Output is correct
59 Correct 4372 ms 601720 KB Output is correct
60 Correct 4384 ms 597860 KB Output is correct
61 Correct 4346 ms 597384 KB Output is correct
62 Correct 4258 ms 589720 KB Output is correct
63 Correct 4119 ms 583176 KB Output is correct
64 Correct 1898 ms 636756 KB Output is correct
65 Correct 1907 ms 636792 KB Output is correct
66 Correct 216 ms 248056 KB Output is correct
67 Correct 773 ms 335480 KB Output is correct
68 Correct 1342 ms 628576 KB Output is correct
69 Correct 1719 ms 630228 KB Output is correct
70 Correct 2003 ms 632292 KB Output is correct
71 Correct 9076 ms 549328 KB Output is correct
72 Correct 8822 ms 561732 KB Output is correct
73 Correct 6743 ms 594552 KB Output is correct
74 Correct 6677 ms 597420 KB Output is correct
75 Correct 6800 ms 595368 KB Output is correct
76 Correct 7794 ms 577172 KB Output is correct
77 Correct 8976 ms 566208 KB Output is correct
78 Correct 9625 ms 563640 KB Output is correct
79 Correct 6806 ms 601016 KB Output is correct
80 Correct 6672 ms 601856 KB Output is correct
81 Correct 7513 ms 589984 KB Output is correct
82 Correct 6626 ms 603784 KB Output is correct
83 Correct 7322 ms 600332 KB Output is correct
84 Correct 6438 ms 586244 KB Output is correct
85 Correct 2997 ms 639964 KB Output is correct