Submission #1016407

# Submission time Handle Problem Language Result Execution time Memory
1016407 2024-07-08T04:26:02 Z Aiperiii Olympic Bus (JOI20_ho_t4) C++14
100 / 100
673 ms 2520 KB
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
//#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=5e4+5;
vector <pair <pair <int,int> ,int> > g[205];
int dist[205][205];
vector <int> a,b,c,d;
priority_queue <pair <int,int> > pq;
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            dist[i][j]=1e9;
        }
        dist[i][i]=0;
    }
    a.pb(0);b.pb(0);c.pb(0);d.pb(0);
    for(int i=1;i<=m;i++){
        int A,B,C,D;
        cin>>A>>B>>C>>D;
        a.pb(A);b.pb(B);c.pb(C);d.pb(D);
        dist[a[i]][b[i]]=min(dist[a[i]][b[i]],c[i]);
        g[a[i]].pb({{b[i],c[i]},i});
        
    }
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
            }
        }
    }
    
    int mn=2e9;
    if(dist[1][n]<1e9 && dist[n][1]<1e9)mn=min(dist[1][n]+dist[n][1],mn);
    //cout<<mn<<"\n";
    for(int i=1;i<=m;i++){
        int new1n=min(dist[1][n],dist[1][b[i]]+dist[a[i]][n]+c[i]);
        int newn1=min(dist[n][1],dist[n][b[i]]+dist[a[i]][1]+c[i]);
        if(new1n==1e9 or newn1==1e9 or new1n+newn1+d[i]>=mn)continue;
        
        vector <int> dis(n+1,1e9);
        dis[1]=0;
        pq.push({0,1});
        while(!pq.empty()){
            int v=pq.top().ss;
            pq.pop();
            if(v==b[i]){
                if(dis[a[i]]>dis[b[i]]+c[i]){
                    dis[a[i]]=dis[b[i]]+c[i];
                    pq.push({dis[a[i]],a[i]});
                }
            }
            for(auto to : g[v]){
                if(to.ss==i)continue;
                if(dis[to.ff.ff]>dis[v]+to.ff.ss){
                    dis[to.ff.ff]=dis[v]+to.ff.ss;
                    pq.push({dis[to.ff.ff],to.ff.ff});
                }
            }
        }
        if(dis[n]!=1e9){
            int ans=dis[n];
            for(int j=1;j<=n;j++){
                dis[j]=1e9;
            }
            dis[n]=0;
            pq.push({0,n});
            while(!pq.empty()){
                int v=pq.top().ss;
                pq.pop();
                if(v==b[i]){
                    if(dis[a[i]]>dis[b[i]]+c[i]){
                        dis[a[i]]=dis[b[i]]+c[i];
                        pq.push({dis[a[i]],a[i]});
                    }
                }
                for(auto to : g[v]){
                    if(to.ss==i)continue;
                    if(dis[to.ff.ff]>dis[v]+to.ff.ss){
                        dis[to.ff.ff]=dis[v]+to.ff.ss;
                        pq.push({dis[to.ff.ff],to.ff.ff});
                    }
                }
            }
            if(dis[1]<1e9 && d[i]<1e9)mn=min(mn,ans+dis[1]+d[i]);
        }
    }
    if(mn==2e9)mn=-1;
    cout<<mn<<"\n";
    
}
//g[1]-r
//dis[0] 1
//dis[1] 1r
//dis[2] n
//dis[3] nr

/*

*/


# Verdict Execution time Memory Grader output
1 Correct 6 ms 604 KB Output is correct
2 Correct 5 ms 604 KB Output is correct
3 Correct 5 ms 604 KB Output is correct
4 Correct 6 ms 672 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 5 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 73 ms 604 KB Output is correct
11 Correct 294 ms 652 KB Output is correct
12 Correct 14 ms 600 KB Output is correct
13 Correct 8 ms 600 KB Output is correct
14 Correct 6 ms 604 KB Output is correct
15 Correct 7 ms 604 KB Output is correct
16 Correct 7 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 2256 KB Output is correct
2 Correct 21 ms 2468 KB Output is correct
3 Correct 21 ms 2256 KB Output is correct
4 Correct 9 ms 604 KB Output is correct
5 Correct 7 ms 672 KB Output is correct
6 Correct 5 ms 604 KB Output is correct
7 Correct 5 ms 600 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 53 ms 2372 KB Output is correct
10 Correct 30 ms 2256 KB Output is correct
11 Correct 19 ms 2292 KB Output is correct
12 Correct 18 ms 2256 KB Output is correct
13 Correct 16 ms 2256 KB Output is correct
14 Correct 18 ms 2508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 600 KB Output is correct
2 Correct 4 ms 604 KB Output is correct
3 Correct 12 ms 2124 KB Output is correct
4 Correct 5 ms 604 KB Output is correct
5 Correct 16 ms 2224 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 15 ms 2256 KB Output is correct
9 Correct 16 ms 2444 KB Output is correct
10 Correct 17 ms 2432 KB Output is correct
11 Correct 14 ms 2256 KB Output is correct
12 Correct 13 ms 2328 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 14 ms 2520 KB Output is correct
20 Correct 14 ms 2460 KB Output is correct
21 Correct 18 ms 2280 KB Output is correct
22 Correct 16 ms 2256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 604 KB Output is correct
2 Correct 5 ms 604 KB Output is correct
3 Correct 5 ms 604 KB Output is correct
4 Correct 6 ms 672 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 5 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 73 ms 604 KB Output is correct
11 Correct 294 ms 652 KB Output is correct
12 Correct 14 ms 600 KB Output is correct
13 Correct 8 ms 600 KB Output is correct
14 Correct 6 ms 604 KB Output is correct
15 Correct 7 ms 604 KB Output is correct
16 Correct 7 ms 604 KB Output is correct
17 Correct 14 ms 2256 KB Output is correct
18 Correct 21 ms 2468 KB Output is correct
19 Correct 21 ms 2256 KB Output is correct
20 Correct 9 ms 604 KB Output is correct
21 Correct 7 ms 672 KB Output is correct
22 Correct 5 ms 604 KB Output is correct
23 Correct 5 ms 600 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 53 ms 2372 KB Output is correct
26 Correct 30 ms 2256 KB Output is correct
27 Correct 19 ms 2292 KB Output is correct
28 Correct 18 ms 2256 KB Output is correct
29 Correct 16 ms 2256 KB Output is correct
30 Correct 18 ms 2508 KB Output is correct
31 Correct 5 ms 600 KB Output is correct
32 Correct 4 ms 604 KB Output is correct
33 Correct 12 ms 2124 KB Output is correct
34 Correct 5 ms 604 KB Output is correct
35 Correct 16 ms 2224 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 15 ms 2256 KB Output is correct
39 Correct 16 ms 2444 KB Output is correct
40 Correct 17 ms 2432 KB Output is correct
41 Correct 14 ms 2256 KB Output is correct
42 Correct 13 ms 2328 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 14 ms 2520 KB Output is correct
50 Correct 14 ms 2460 KB Output is correct
51 Correct 18 ms 2280 KB Output is correct
52 Correct 16 ms 2256 KB Output is correct
53 Correct 17 ms 2256 KB Output is correct
54 Correct 15 ms 2252 KB Output is correct
55 Correct 14 ms 2256 KB Output is correct
56 Correct 5 ms 676 KB Output is correct
57 Correct 5 ms 604 KB Output is correct
58 Correct 18 ms 2132 KB Output is correct
59 Correct 22 ms 2132 KB Output is correct
60 Correct 28 ms 2132 KB Output is correct
61 Correct 19 ms 2132 KB Output is correct
62 Correct 149 ms 2132 KB Output is correct
63 Correct 282 ms 2132 KB Output is correct
64 Correct 154 ms 2128 KB Output is correct
65 Correct 567 ms 2248 KB Output is correct
66 Correct 673 ms 2128 KB Output is correct
67 Correct 9 ms 1872 KB Output is correct
68 Correct 32 ms 2256 KB Output is correct
69 Correct 31 ms 2256 KB Output is correct
70 Correct 17 ms 2368 KB Output is correct
71 Correct 21 ms 2424 KB Output is correct
72 Correct 18 ms 2256 KB Output is correct
73 Correct 18 ms 2432 KB Output is correct
74 Correct 21 ms 2488 KB Output is correct