Submission #226476

# Submission time Handle Problem Language Result Execution time Memory
226476 2020-04-23T23:11:37 Z thebes Olympic Bus (JOI20_ho_t4) C++14
100 / 100
364 ms 162656 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
#define pb push_back

const int MN = 202, MM = 5e4+5;
int N, M, i, j, x, y, w[MM], c[MM], u[MM], vs[MN], ban[MM], par[MN]; ll ans, dis[MN], a[MM][MN], b[MM][MN], cost[MM];
vector<pii> adj[MN], rev[MN];
vi tree; pii ed[MM];

void dij(int src,bool dir){
    memset(par,-1,sizeof(par));
    memset(vs,0,sizeof(vs));
    for(i=1;i<=N;i++)
        dis[i]=1LL<<55;
    dis[src]=0;
    int cur=src; ll mn;
    while(cur!=-1){
        vs[cur]=1;
        if(dir==1){
            for(auto v : adj[cur]){
                if(ban[v.second]) continue;
                if(dis[cur]+w[v.second]<dis[v.first]){
                    dis[v.first]=dis[cur]+w[v.second];
                    par[v.first]=v.second;
                }
            }
        }
        else{
            for(auto v : rev[cur]){
                if(ban[v.second]) continue;
                if(dis[cur]+w[v.second]<dis[v.first]){
                    dis[v.first]=dis[cur]+w[v.second];
                    par[v.first]=v.second;
                }
            }
        }
        mn=1LL<<50; cur=-1;
        for(i=1;i<=N;i++){
            if(vs[i]) continue;
            if(dis[i]<mn){
                mn=dis[i];
                cur=i;
            }
        }
    }
    for(i=1;i<=N;i++){
        if(par[i]!=-1) u[par[i]]=1;
    }
}

int main(){
    mt19937 mt(time(0));
    for(scanf("%d%d",&N,&M),i=1;i<=M;i++){
        scanf("%d%d%d%d",&x,&y,&w[i],&c[i]);
        ed[i]={x,y};
        adj[x].pb({y,i});
        rev[y].pb({x,i});
    }
    for(i=1;i<=N;i++){
        shuffle(adj[i].begin(),adj[i].end(),mt);
        shuffle(rev[i].begin(),rev[i].end(),mt);
    }
    for(int k=0;k<2;k++){
        memset(u,0,sizeof(u));
        dij(1,1^k);
        tree.clear();
        for(i=1;i<=M;i++){
            if(u[i]) tree.pb(i);
            else{
                for(j=1;j<=N;j++)
                    a[i][j]=dis[j];
            }
        }
        assert(tree.size()<=N);
        for(auto v : tree){
            ban[v]=1;
            dij(1,1^k);
            for(j=1;j<=N;j++)
                a[v][j]=dis[j];
            ban[v]=0;
        }
        memset(u,0,sizeof(u));
        dij(N,k);
        tree.clear();
        for(i=1;i<=M;i++){
            if(u[i]) tree.pb(i);
            else{
                for(j=1;j<=N;j++)
                    b[i][j]=dis[j];
            }
        }
        assert(tree.size()<=N);
        for(auto v : tree){
            ban[v]=1;
            dij(N,k);
            for(j=1;j<=N;j++)
                b[v][j]=dis[j];
            ban[v]=0;
        }
        for(j=1;j<=M;j++){
            if(!k) cost[j]+=min(a[j][N],a[j][ed[j].second]+w[j]+b[j][ed[j].first]);
            else cost[j]+=min(a[j][N],a[j][ed[j].first]+w[j]+b[j][ed[j].second]);
        }
    }
    dij(1,1);
    ans+=dis[N];
    dij(N,1);
    ans+=dis[1];
    for(j=1;j<=M;j++)
        ans=min(ans,cost[j]+c[j]);
    if(ans>(1LL<<50)) printf("-1\n");
    else printf("%lld\n",ans);
    return 0;
}

Compilation message

In file included from /usr/include/c++/7/cassert:44:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
                 from ho_t4.cpp:1:
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:79:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         assert(tree.size()<=N);
                ~~~~~~~~~~~^~~
ho_t4.cpp:97:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         assert(tree.size()<=N);
                ~~~~~~~~~~~^~~
ho_t4.cpp:58:28: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(scanf("%d%d",&N,&M),i=1;i<=M;i++){
         ~~~~~~~~~~~~~~~~~~~^~~~
ho_t4.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d%d",&x,&y,&w[i],&c[i]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 60 ms 3960 KB Output is correct
2 Correct 8 ms 1408 KB Output is correct
3 Correct 94 ms 3840 KB Output is correct
4 Correct 89 ms 3832 KB Output is correct
5 Correct 7 ms 3840 KB Output is correct
6 Correct 12 ms 1408 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 640 KB Output is correct
9 Correct 6 ms 3712 KB Output is correct
10 Correct 91 ms 3832 KB Output is correct
11 Correct 94 ms 3968 KB Output is correct
12 Correct 98 ms 3832 KB Output is correct
13 Correct 25 ms 3840 KB Output is correct
14 Correct 61 ms 3840 KB Output is correct
15 Correct 59 ms 3840 KB Output is correct
16 Correct 58 ms 3832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 330 ms 161324 KB Output is correct
2 Correct 292 ms 161272 KB Output is correct
3 Correct 294 ms 161272 KB Output is correct
4 Correct 102 ms 7032 KB Output is correct
5 Correct 73 ms 3832 KB Output is correct
6 Correct 24 ms 2560 KB Output is correct
7 Correct 6 ms 1792 KB Output is correct
8 Correct 5 ms 640 KB Output is correct
9 Correct 169 ms 161272 KB Output is correct
10 Correct 167 ms 161272 KB Output is correct
11 Correct 236 ms 161460 KB Output is correct
12 Correct 264 ms 161400 KB Output is correct
13 Correct 238 ms 161272 KB Output is correct
14 Correct 268 ms 161272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 3832 KB Output is correct
2 Correct 17 ms 1408 KB Output is correct
3 Correct 204 ms 125816 KB Output is correct
4 Correct 12 ms 1408 KB Output is correct
5 Correct 255 ms 161400 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 4 ms 512 KB Output is correct
8 Correct 159 ms 161272 KB Output is correct
9 Correct 158 ms 161272 KB Output is correct
10 Correct 225 ms 161400 KB Output is correct
11 Correct 218 ms 161272 KB Output is correct
12 Correct 215 ms 161400 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 5 ms 768 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 5 ms 640 KB Output is correct
17 Correct 5 ms 640 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 229 ms 161392 KB Output is correct
20 Correct 240 ms 161272 KB Output is correct
21 Correct 209 ms 161276 KB Output is correct
22 Correct 221 ms 161400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 3960 KB Output is correct
2 Correct 8 ms 1408 KB Output is correct
3 Correct 94 ms 3840 KB Output is correct
4 Correct 89 ms 3832 KB Output is correct
5 Correct 7 ms 3840 KB Output is correct
6 Correct 12 ms 1408 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 640 KB Output is correct
9 Correct 6 ms 3712 KB Output is correct
10 Correct 91 ms 3832 KB Output is correct
11 Correct 94 ms 3968 KB Output is correct
12 Correct 98 ms 3832 KB Output is correct
13 Correct 25 ms 3840 KB Output is correct
14 Correct 61 ms 3840 KB Output is correct
15 Correct 59 ms 3840 KB Output is correct
16 Correct 58 ms 3832 KB Output is correct
17 Correct 330 ms 161324 KB Output is correct
18 Correct 292 ms 161272 KB Output is correct
19 Correct 294 ms 161272 KB Output is correct
20 Correct 102 ms 7032 KB Output is correct
21 Correct 73 ms 3832 KB Output is correct
22 Correct 24 ms 2560 KB Output is correct
23 Correct 6 ms 1792 KB Output is correct
24 Correct 5 ms 640 KB Output is correct
25 Correct 169 ms 161272 KB Output is correct
26 Correct 167 ms 161272 KB Output is correct
27 Correct 236 ms 161460 KB Output is correct
28 Correct 264 ms 161400 KB Output is correct
29 Correct 238 ms 161272 KB Output is correct
30 Correct 268 ms 161272 KB Output is correct
31 Correct 59 ms 3832 KB Output is correct
32 Correct 17 ms 1408 KB Output is correct
33 Correct 204 ms 125816 KB Output is correct
34 Correct 12 ms 1408 KB Output is correct
35 Correct 255 ms 161400 KB Output is correct
36 Correct 5 ms 512 KB Output is correct
37 Correct 4 ms 512 KB Output is correct
38 Correct 159 ms 161272 KB Output is correct
39 Correct 158 ms 161272 KB Output is correct
40 Correct 225 ms 161400 KB Output is correct
41 Correct 218 ms 161272 KB Output is correct
42 Correct 215 ms 161400 KB Output is correct
43 Correct 4 ms 512 KB Output is correct
44 Correct 5 ms 768 KB Output is correct
45 Correct 6 ms 512 KB Output is correct
46 Correct 5 ms 640 KB Output is correct
47 Correct 5 ms 640 KB Output is correct
48 Correct 5 ms 512 KB Output is correct
49 Correct 229 ms 161392 KB Output is correct
50 Correct 240 ms 161272 KB Output is correct
51 Correct 209 ms 161276 KB Output is correct
52 Correct 221 ms 161400 KB Output is correct
53 Correct 303 ms 161272 KB Output is correct
54 Correct 322 ms 161400 KB Output is correct
55 Correct 317 ms 161248 KB Output is correct
56 Correct 87 ms 3836 KB Output is correct
57 Correct 87 ms 3832 KB Output is correct
58 Correct 261 ms 129144 KB Output is correct
59 Correct 252 ms 129016 KB Output is correct
60 Correct 274 ms 129016 KB Output is correct
61 Correct 265 ms 129016 KB Output is correct
62 Correct 259 ms 129016 KB Output is correct
63 Correct 261 ms 129020 KB Output is correct
64 Correct 364 ms 129144 KB Output is correct
65 Correct 338 ms 130040 KB Output is correct
66 Correct 312 ms 130040 KB Output is correct
67 Correct 81 ms 129644 KB Output is correct
68 Correct 172 ms 162424 KB Output is correct
69 Correct 191 ms 162400 KB Output is correct
70 Correct 272 ms 162424 KB Output is correct
71 Correct 269 ms 162652 KB Output is correct
72 Correct 237 ms 162552 KB Output is correct
73 Correct 272 ms 162424 KB Output is correct
74 Correct 255 ms 162656 KB Output is correct