답안 #705675

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
705675 2023-03-05T00:17:29 Z cig32 Olympic Bus (JOI20_ho_t4) C++17
37 / 100
1000 ms 8340 KB
#include "bits/stdc++.h"
#define int long long
using namespace std;
struct edge {
  int u, v, c, d;
};
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  edge e[m+1];
  for(int i=1; i<=m; i++) {
    cin >> e[i].u >> e[i].v >> e[i].c >> e[i].d;
  }
  int dist1[n+1][n+1], distn[n+1][n+1];
  int rev1[n+1][n+1], revn[n+1][n+1]; // b=0: nothing 
  for(int i=0; i<=n; i++) {
    for(int j=0; j<=n; j++) {
      dist1[i][j] = distn[i][j] = rev1[i][j] = revn[i][j] = 1e18;
    }
    dist1[i][1] = 0;
    distn[i][n] = 0;
    rev1[i][1] = 0;
    revn[i][n] = 0;
  }
  for(int b=0; b<=n; b++) {
    vector<pair<int, int> > adj[n+1], rev[n+1];
    bool vis[n+1];
    for(int i=1; i<=m; i++) {
      if(e[i].u != b && e[i].v != b) {
        adj[e[i].u].push_back({e[i].v, e[i].c});
        rev[e[i].v].push_back({e[i].u, e[i].c});
      }
    }
    priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > pq;
    for(int i=1; i<=n; i++) pq.push({dist1[b][i], i});
    for(int i=1; i<=n; i++) vis[i] = 0;
    while(pq.size()) {
      pair<int,int> t = pq.top(); pq.pop();
      if(!vis[t.second]) {
        for(pair<int, int> x: adj[t.second]) {
          if(!vis[x.first] && dist1[b][x.first] > dist1[b][t.second]+x.second) {
            dist1[b][x.first] = dist1[b][t.second]+x.second;
            pq.push({dist1[b][x.first], x.first});
          }
        }
      }
    }
    for(int i=1; i<=n; i++) pq.push({distn[b][i], i});
    for(int i=1; i<=n; i++) vis[i] = 0;
    while(pq.size()) {
      pair<int,int> t = pq.top(); pq.pop();
      if(!vis[t.second]) {
        for(pair<int, int> x: adj[t.second]) {
          if(!vis[x.first] && distn[b][x.first] > distn[b][t.second]+x.second) {
            distn[b][x.first] = distn[b][t.second]+x.second;
            pq.push({distn[b][x.first], x.first});
          }
        }
      }
    }
    for(int i=1; i<=n; i++) pq.push({rev1[b][i], i});
    for(int i=1; i<=n; i++) vis[i] = 0;
    while(pq.size()) {
      pair<int,int> t = pq.top(); pq.pop();
      if(!vis[t.second]) {
        for(pair<int, int> x: rev[t.second]) {
          if(!vis[x.first] && rev1[b][x.first] > rev1[b][t.second]+x.second) {
            rev1[b][x.first] = rev1[b][t.second]+x.second;
            pq.push({rev1[b][x.first], x.first});
          }
        }
      }
    }
    for(int i=1; i<=n; i++) pq.push({revn[b][i], i});
    for(int i=1; i<=n; i++) vis[i] = 0;
    while(pq.size()) {
      pair<int,int> t = pq.top(); pq.pop();
      if(!vis[t.second]) {
        for(pair<int, int> x: rev[t.second]) {
          if(!vis[x.first] && revn[b][x.first] > revn[b][t.second]+x.second) {
            revn[b][x.first] = revn[b][t.second]+x.second;
            pq.push({revn[b][x.first], x.first});
          }
        }
      }
    }
  }
  int ans = dist1[0][n] + distn[0][1];
  vector<pair<int, int> > adj[n+1], rev[n+1];
  vector<int> adj_id[n+1];
  for(int i=1; i<=m; i++) {
    adj_id[e[i].u].push_back(i);
    adj[e[i].u].push_back({e[i].v, e[i].c});
    rev[e[i].v].push_back({e[i].u, e[i].c});
  }
  int cnt[n+1][n+1];
  for(int i=0; i<=n; i++) {
    for(int j=0; j<=n; j++) {
      cnt[i][j] = 0;
    }
  }
  for(int i=1; i<=m; i++) {
    cnt[e[i].u][e[i].v]++;
  }
  for(int i=1; i<=m; i++) {
    int forward, backward;
    forward = dist1[e[i].u][e[i].v] + e[i].c;
    int mi = (e[i].u == n ? 0 : 1e18);
    for(pair<int,int> x: adj[e[i].u]) {
      mi = min(mi, x.second + revn[e[i].u][x.first]);
    }
    forward += mi;
    forward = min(forward, dist1[e[i].u][n]);
    
    int pt1 = (e[i].u == 1 ? 0 : 1e18);
    int pt2 = (e[i].u == n ? 0 : 1e18);
    for(pair<int,int> x: rev[e[i].u]) {
      pt1 = min(pt1, dist1[e[i].u][x.first] + x.second);
    }
    int it=0;
    for(pair<int,int> x: adj[e[i].u]) {
      if(adj_id[e[i].u][it] == i) {
        it++;
        continue;
      }
      it++;
      pt2 = min(pt2, revn[e[i].u][x.first] + x.second);
    }
    forward = min(forward, pt1 + pt2);
    backward = distn[e[i].u][e[i].v] + e[i].c;
    mi = (e[i].u == 1 ? 0 : 1e18);
    for(pair<int,int> x: adj[e[i].u]) {
      mi = min(mi, x.second + rev1[e[i].u][x.first]);
    }
    backward += mi;
    backward = min(backward, distn[e[i].u][1]);
    pt1 = (e[i].u == n ? 0 : 1e18);
    pt2 = (e[i].u == 1 ? 0 : 1e18);
    for(pair<int,int> x: rev[e[i].u]) {
      pt1 = min(pt1, distn[e[i].u][x.first] + x.second);
    }
    it=0;
    for(pair<int,int> x: adj[e[i].u]) {
      if(adj_id[e[i].u][it] == i) {
        it++;
        continue;
      }
      it++;
      pt2 = min(pt2, rev1[e[i].u][x.first] + x.second);
    }
    backward = min(backward, pt1 + pt2);
    ans = min(ans, forward + backward + e[i].d);
    //cout << "flip edge " << i << ": " << forward << " + " << backward << " + " << e[i].d << " = " << forward + backward + e[i].d << "\n";
  }
  cout << (ans <= 1e12 ? ans : -1) << "\n";
}
/*
5 6
1 2 1 10000
2 5 1 10000
5 3 1 10000
2 3 10 1
2 4 1 10000
4 1 1 10000

2 2
1 2 3 4
1 2 3 9

3 4
1 2 3 4
2 3 3 9
2 1 3 10
2 3 3 11

2 7
1 2 1 1
1 2 1 7
1 2 1 3
1 2 1 4
1 2 1 5
1 2 1 6
1 2 1 9
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 1996 KB Output is correct
2 Correct 14 ms 1876 KB Output is correct
3 Correct 57 ms 1940 KB Output is correct
4 Correct 55 ms 1944 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 17 ms 1876 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 6 ms 340 KB Output is correct
10 Correct 62 ms 1940 KB Output is correct
11 Correct 61 ms 1944 KB Output is correct
12 Correct 60 ms 1996 KB Output is correct
13 Correct 41 ms 1944 KB Output is correct
14 Correct 46 ms 1952 KB Output is correct
15 Correct 46 ms 1996 KB Output is correct
16 Correct 46 ms 1964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 817 ms 6736 KB Output is correct
2 Correct 834 ms 6636 KB Output is correct
3 Correct 833 ms 6992 KB Output is correct
4 Correct 60 ms 2040 KB Output is correct
5 Correct 39 ms 1944 KB Output is correct
6 Correct 23 ms 1912 KB Output is correct
7 Correct 15 ms 1900 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 633 ms 7864 KB Output is correct
10 Correct 620 ms 7864 KB Output is correct
11 Correct 754 ms 7916 KB Output is correct
12 Correct 753 ms 7904 KB Output is correct
13 Correct 748 ms 7992 KB Output is correct
14 Correct 788 ms 8340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 1960 KB Output is correct
2 Correct 18 ms 1876 KB Output is correct
3 Correct 459 ms 5852 KB Output is correct
4 Correct 17 ms 1876 KB Output is correct
5 Correct 656 ms 6892 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 605 ms 6736 KB Output is correct
9 Correct 565 ms 6700 KB Output is correct
10 Correct 626 ms 6744 KB Output is correct
11 Correct 572 ms 6540 KB Output is correct
12 Correct 647 ms 6864 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 324 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 618 ms 8000 KB Output is correct
20 Correct 583 ms 7648 KB Output is correct
21 Correct 633 ms 7564 KB Output is correct
22 Correct 584 ms 7584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 1996 KB Output is correct
2 Correct 14 ms 1876 KB Output is correct
3 Correct 57 ms 1940 KB Output is correct
4 Correct 55 ms 1944 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 17 ms 1876 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 6 ms 340 KB Output is correct
10 Correct 62 ms 1940 KB Output is correct
11 Correct 61 ms 1944 KB Output is correct
12 Correct 60 ms 1996 KB Output is correct
13 Correct 41 ms 1944 KB Output is correct
14 Correct 46 ms 1952 KB Output is correct
15 Correct 46 ms 1996 KB Output is correct
16 Correct 46 ms 1964 KB Output is correct
17 Correct 817 ms 6736 KB Output is correct
18 Correct 834 ms 6636 KB Output is correct
19 Correct 833 ms 6992 KB Output is correct
20 Correct 60 ms 2040 KB Output is correct
21 Correct 39 ms 1944 KB Output is correct
22 Correct 23 ms 1912 KB Output is correct
23 Correct 15 ms 1900 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 633 ms 7864 KB Output is correct
26 Correct 620 ms 7864 KB Output is correct
27 Correct 754 ms 7916 KB Output is correct
28 Correct 753 ms 7904 KB Output is correct
29 Correct 748 ms 7992 KB Output is correct
30 Correct 788 ms 8340 KB Output is correct
31 Correct 46 ms 1960 KB Output is correct
32 Correct 18 ms 1876 KB Output is correct
33 Correct 459 ms 5852 KB Output is correct
34 Correct 17 ms 1876 KB Output is correct
35 Correct 656 ms 6892 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 605 ms 6736 KB Output is correct
39 Correct 565 ms 6700 KB Output is correct
40 Correct 626 ms 6744 KB Output is correct
41 Correct 572 ms 6540 KB Output is correct
42 Correct 647 ms 6864 KB Output is correct
43 Correct 0 ms 212 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 0 ms 324 KB Output is correct
47 Correct 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 618 ms 8000 KB Output is correct
50 Correct 583 ms 7648 KB Output is correct
51 Correct 633 ms 7564 KB Output is correct
52 Correct 584 ms 7584 KB Output is correct
53 Correct 873 ms 7484 KB Output is correct
54 Correct 874 ms 7624 KB Output is correct
55 Correct 876 ms 7552 KB Output is correct
56 Correct 52 ms 1976 KB Output is correct
57 Correct 57 ms 1972 KB Output is correct
58 Correct 655 ms 6180 KB Output is correct
59 Correct 752 ms 6276 KB Output is correct
60 Correct 693 ms 6216 KB Output is correct
61 Correct 689 ms 6240 KB Output is correct
62 Correct 691 ms 6188 KB Output is correct
63 Correct 683 ms 6236 KB Output is correct
64 Execution timed out 1062 ms 6336 KB Time limit exceeded
65 Halted 0 ms 0 KB -