Submission #629936

# Submission time Handle Problem Language Result Execution time Memory
629936 2022-08-15T11:21:59 Z Abrar_Al_Samit Olympic Bus (JOI20_ho_t4) C++17
100 / 100
157 ms 5692 KB
#include<bits/stdc++.h>
using namespace std;

const int MX = 205;
const long long INF = 1e17;
const int MXM = 5e4 + 5;

struct Edge {
  int from, to, len, invert, id;
  bool vip;

  Edge() {vip = 0;}
  Edge(int _from, int _to, int _len, int _invert, int _id) {
    from = _from, to = _to, len = _len, invert = _invert, id = _id;
    vip = 0;
  }
  bool operator< (const Edge &ot) const {
    return make_pair(vip, id) < make_pair(ot.vip, ot.id);
  }
} List[MXM];

int n, m;
vector<Edge>g[MX], gt[MX];
long long to_one[MX], to_n[MX], from_one[MX], from_n[MX];
int dad[MX];
bool marked[MX];


void dijkstra(int sc, int graph, int ban, long long *a) {
  fill(a+1, a+n+1, INF);
  memset(marked, 0, sizeof marked);

  a[sc] = 0;
  for(int i=0; i<n; ++i) {
    int v = -1;
    for(int j=1; j<=n; ++j) if(!marked[j] && (v==-1 || a[v]>a[j])) {
      v = j;
    }
    if(a[v]==INF) break;
    marked[v] = 1;

    for(auto e : (graph==0?g:gt)[v]) if(e.id!=ban) {
      if(a[e.to]>a[v]+e.len) {
        a[e.to] = a[v]+e.len;
        dad[e.to] = e.id;
      }
    }
  }
}
void mark(int f, int t) {
  int cur = f;
  while(cur!=t) {
    List[dad[cur]].vip = 1;
    cur = List[dad[cur]].from;
  }
}
void PlayGround() {
  cin>>n>>m;
  for(int i=0; i<m; ++i) {
    int u, v, c, d;
    cin>>u>>v>>c>>d;
    g[u].push_back(Edge(u, v, c, d, i));
    gt[v].push_back(Edge(v, u, c, d, i));

    List[i] = {u, v, c, d, i};
  }

  dijkstra(1, 0, -1, from_one);
  if(from_one[n]!=INF) mark(n, 1);

  dijkstra(n, 0, -1, from_n);
  if(from_n[1]!=INF) mark(1, n);

  dijkstra(1, 1, -1, to_one);
  dijkstra(n, 1, -1, to_n);

  for(int i=1; i<=n; ++i) {
    for(auto &e : g[i]) {
      e = List[e.id];
    }
  }
  long long ans = min(INF, from_one[n] + from_n[1]);

  sort(List, List+m);

  for(int i=0; i<m; ++i) {
    auto e = List[i];
    if(e.vip) {
      dijkstra(1, 0, e.id, from_one);
      dijkstra(n, 0, e.id, from_n);
    }

    long long u, v, c, d;
    u = e.from, v = e.to, c = e.len, d = e.invert;

    //1->v->u->n->1
    ans = min(ans, from_one[v]+to_n[u]+from_n[1]+c+d);

    //1->v->u->n->v->u->1
    ans = min(ans, from_one[v]+c+to_n[u]+from_n[v]+c+to_one[u]+d);

    //1->n->v->u->1
    ans = min(ans, from_one[n]+from_n[v]+to_one[u]+c+d);
  }

  if(ans==INF) ans = -1;
  cout<<ans<<'\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  PlayGround();
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 3 ms 1492 KB Output is correct
4 Correct 4 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 41 ms 1492 KB Output is correct
11 Correct 56 ms 1492 KB Output is correct
12 Correct 55 ms 1572 KB Output is correct
13 Correct 2 ms 1492 KB Output is correct
14 Correct 3 ms 1492 KB Output is correct
15 Correct 2 ms 1492 KB Output is correct
16 Correct 3 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 5332 KB Output is correct
2 Correct 24 ms 5376 KB Output is correct
3 Correct 31 ms 5536 KB Output is correct
4 Correct 3 ms 1620 KB Output is correct
5 Correct 5 ms 1492 KB Output is correct
6 Correct 2 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 32 ms 5196 KB Output is correct
10 Correct 24 ms 5308 KB Output is correct
11 Correct 26 ms 5076 KB Output is correct
12 Correct 25 ms 4936 KB Output is correct
13 Correct 26 ms 5324 KB Output is correct
14 Correct 27 ms 5692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 20 ms 4100 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 22 ms 5208 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 24 ms 5196 KB Output is correct
9 Correct 19 ms 5208 KB Output is correct
10 Correct 24 ms 5308 KB Output is correct
11 Correct 22 ms 5332 KB Output is correct
12 Correct 21 ms 5404 KB Output is correct
13 Correct 1 ms 1492 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1492 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 1 ms 1492 KB Output is correct
19 Correct 23 ms 5460 KB Output is correct
20 Correct 28 ms 5324 KB Output is correct
21 Correct 23 ms 5364 KB Output is correct
22 Correct 28 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 3 ms 1492 KB Output is correct
4 Correct 4 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 41 ms 1492 KB Output is correct
11 Correct 56 ms 1492 KB Output is correct
12 Correct 55 ms 1572 KB Output is correct
13 Correct 2 ms 1492 KB Output is correct
14 Correct 3 ms 1492 KB Output is correct
15 Correct 2 ms 1492 KB Output is correct
16 Correct 3 ms 1492 KB Output is correct
17 Correct 28 ms 5332 KB Output is correct
18 Correct 24 ms 5376 KB Output is correct
19 Correct 31 ms 5536 KB Output is correct
20 Correct 3 ms 1620 KB Output is correct
21 Correct 5 ms 1492 KB Output is correct
22 Correct 2 ms 1492 KB Output is correct
23 Correct 1 ms 1492 KB Output is correct
24 Correct 1 ms 1492 KB Output is correct
25 Correct 32 ms 5196 KB Output is correct
26 Correct 24 ms 5308 KB Output is correct
27 Correct 26 ms 5076 KB Output is correct
28 Correct 25 ms 4936 KB Output is correct
29 Correct 26 ms 5324 KB Output is correct
30 Correct 27 ms 5692 KB Output is correct
31 Correct 4 ms 1492 KB Output is correct
32 Correct 1 ms 1492 KB Output is correct
33 Correct 20 ms 4100 KB Output is correct
34 Correct 1 ms 1492 KB Output is correct
35 Correct 22 ms 5208 KB Output is correct
36 Correct 1 ms 1492 KB Output is correct
37 Correct 1 ms 1492 KB Output is correct
38 Correct 24 ms 5196 KB Output is correct
39 Correct 19 ms 5208 KB Output is correct
40 Correct 24 ms 5308 KB Output is correct
41 Correct 22 ms 5332 KB Output is correct
42 Correct 21 ms 5404 KB Output is correct
43 Correct 1 ms 1492 KB Output is correct
44 Correct 1 ms 1492 KB Output is correct
45 Correct 1 ms 1492 KB Output is correct
46 Correct 1 ms 1492 KB Output is correct
47 Correct 1 ms 1492 KB Output is correct
48 Correct 1 ms 1492 KB Output is correct
49 Correct 23 ms 5460 KB Output is correct
50 Correct 28 ms 5324 KB Output is correct
51 Correct 23 ms 5364 KB Output is correct
52 Correct 28 ms 4940 KB Output is correct
53 Correct 24 ms 5076 KB Output is correct
54 Correct 31 ms 4864 KB Output is correct
55 Correct 33 ms 5124 KB Output is correct
56 Correct 4 ms 1492 KB Output is correct
57 Correct 4 ms 1596 KB Output is correct
58 Correct 79 ms 4236 KB Output is correct
59 Correct 112 ms 4180 KB Output is correct
60 Correct 111 ms 4192 KB Output is correct
61 Correct 75 ms 4180 KB Output is correct
62 Correct 90 ms 4252 KB Output is correct
63 Correct 108 ms 4224 KB Output is correct
64 Correct 122 ms 4564 KB Output is correct
65 Correct 132 ms 4436 KB Output is correct
66 Correct 157 ms 4536 KB Output is correct
67 Correct 14 ms 3944 KB Output is correct
68 Correct 22 ms 5236 KB Output is correct
69 Correct 21 ms 5212 KB Output is correct
70 Correct 29 ms 4948 KB Output is correct
71 Correct 25 ms 5100 KB Output is correct
72 Correct 26 ms 5224 KB Output is correct
73 Correct 25 ms 5460 KB Output is correct
74 Correct 26 ms 5272 KB Output is correct