답안 #227162

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
227162 2020-04-26T09:37:27 Z ho94949 Olympic Bus (JOI20_ho_t4) C++17
100 / 100
327 ms 2552 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200, MAXM = 50'000, INF = 0x3f3f3f3f;

int N, M;
int U[MAXM], V[MAXM], C[MAXM], D[MAXM];
bool issp[MAXM]; // is vertex contained in shortest path DAG? ((1, N) -> x, (1, N) <- x)

int adj[MAXN][MAXN]; int from[MAXN]; bool vis[MAXN];

int dis0[MAXN], dis0r[MAXN], disN[MAXN], disNr[MAXN], dis[MAXN];

int dijk(int s, int e)
{
    memset(dis, 0x3f, sizeof dis);
    memset(vis, 0, sizeof vis);
    dis[s] = 0;
    for(int iter = 0; iter<N; ++iter)
    {
        int minv = INF, mini = -1;
        for(int i=0; i<N; ++i) if(!vis[i])
            if(minv > dis[i])
            {
                minv = dis[i];
                mini = i;
            }
        if(mini == -1) break;
        if(mini == e) return minv;
        vis[mini] = true;
        for(int i=0; i<N; ++i) if(!vis[i])
            if(dis[i] > dis[mini] + adj[mini][i])
                dis[i] = dis[mini] + adj[mini][i];
    }
    return INF;
}

int exc(int e)
{
    memset(adj, 0x3f, sizeof adj);
    for(int i=0; i<M; ++i)
    {
        int u = U[i], v = V[i], c = C[i];
        if(i==e) swap(u, v);
        adj[u][v] = min(adj[u][v], c);
    }
    return dijk(0, N-1) + dijk(N-1, 0);
}

void SP(int s, int* S, int *E, int *dis)
{
    memset(adj, -1, sizeof adj);
    memset(dis, 0x3f, sizeof(int)*N);
    memset(vis, 0, sizeof vis);

    for(int i=0; i<M; ++i)
    {
        int u = S[i], v = E[i];
        if(adj[u][v] == -1 || C[adj[u][v]] > C[i])
            adj[u][v] = i;
    }
    dis[s] = 0;
    for(int iter=0; iter<N; ++iter)
    {
        int minv = INF, mini = -1;
        for(int i=0; i<N; ++i) if(!vis[i])
            if(dis[i] < minv)
                minv = dis[i], mini = i;
        if(mini == -1) break;


        vis[mini] = true;
        issp[from[mini]] = true;

        for(int i=0; i<N; ++i)
            if(!vis[i] && adj[mini][i]!=-1)
            {
                int e = adj[mini][i];
                if(dis[i] > dis[mini] + C[e])
                {
                    dis[i] = dis[mini] + C[e];
                    from[i] = e;
                }
            }
    }

}

int main()
{
    scanf("%d%d", &N, &M);
    for(int i=0; i<M; ++i)
    {
        scanf("%d%d%d%d", U+i, V+i, C+i, D+i);
        --U[i]; --V[i];
    }
    SP(0, U, V, dis0); SP(0, V, U, dis0r);
    SP(N-1, U, V, disN); SP(N-1, V, U, disNr);
    
    long long ans = dis0[N-1] + disN[0];
    for(int i=0; i<M; ++i)
    {
        long long ret = D[i];
        long long u = U[i], v = V[i], c = C[i];
        if(issp[i]) ret += exc(i);
        else
        {
            ret += min((long long) dis0[N-1], dis0[v] + c + disNr[u]);
            ret += min((long long) disN[0], disN[v] + c + dis0r[u]);
        }
        ans = min(ans, ret);
    }
    if(ans >= INF) ans = -1;
    printf("%lld\n", ans);
}

Compilation message

ho_t4.cpp: In function 'int main()':
ho_t4.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &M);
     ~~~~~^~~~~~~~~~~~~~~~
ho_t4.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%d%d", U+i, V+i, C+i, D+i);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 114 ms 600 KB Output is correct
4 Correct 154 ms 512 KB Output is correct
5 Correct 8 ms 512 KB Output is correct
6 Correct 7 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 101 ms 512 KB Output is correct
11 Correct 141 ms 632 KB Output is correct
12 Correct 125 ms 632 KB Output is correct
13 Correct 63 ms 512 KB Output is correct
14 Correct 108 ms 512 KB Output is correct
15 Correct 95 ms 512 KB Output is correct
16 Correct 123 ms 632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 275 ms 2384 KB Output is correct
2 Correct 299 ms 2428 KB Output is correct
3 Correct 249 ms 2432 KB Output is correct
4 Correct 32 ms 512 KB Output is correct
5 Correct 71 ms 512 KB Output is correct
6 Correct 7 ms 512 KB Output is correct
7 Correct 6 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 190 ms 2552 KB Output is correct
10 Correct 183 ms 2508 KB Output is correct
11 Correct 268 ms 2432 KB Output is correct
12 Correct 256 ms 2552 KB Output is correct
13 Correct 278 ms 2424 KB Output is correct
14 Correct 248 ms 2552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 512 KB Output is correct
2 Correct 11 ms 512 KB Output is correct
3 Correct 264 ms 1920 KB Output is correct
4 Correct 11 ms 512 KB Output is correct
5 Correct 327 ms 2400 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 200 ms 2168 KB Output is correct
9 Correct 206 ms 2168 KB Output is correct
10 Correct 320 ms 2424 KB Output is correct
11 Correct 263 ms 2168 KB Output is correct
12 Correct 260 ms 2296 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 4 ms 512 KB Output is correct
15 Correct 5 ms 512 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 4 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 319 ms 2296 KB Output is correct
20 Correct 271 ms 2168 KB Output is correct
21 Correct 269 ms 2176 KB Output is correct
22 Correct 277 ms 2268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 114 ms 600 KB Output is correct
4 Correct 154 ms 512 KB Output is correct
5 Correct 8 ms 512 KB Output is correct
6 Correct 7 ms 512 KB Output is correct
7 Correct 5 ms 512 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 101 ms 512 KB Output is correct
11 Correct 141 ms 632 KB Output is correct
12 Correct 125 ms 632 KB Output is correct
13 Correct 63 ms 512 KB Output is correct
14 Correct 108 ms 512 KB Output is correct
15 Correct 95 ms 512 KB Output is correct
16 Correct 123 ms 632 KB Output is correct
17 Correct 275 ms 2384 KB Output is correct
18 Correct 299 ms 2428 KB Output is correct
19 Correct 249 ms 2432 KB Output is correct
20 Correct 32 ms 512 KB Output is correct
21 Correct 71 ms 512 KB Output is correct
22 Correct 7 ms 512 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 5 ms 512 KB Output is correct
25 Correct 190 ms 2552 KB Output is correct
26 Correct 183 ms 2508 KB Output is correct
27 Correct 268 ms 2432 KB Output is correct
28 Correct 256 ms 2552 KB Output is correct
29 Correct 278 ms 2424 KB Output is correct
30 Correct 248 ms 2552 KB Output is correct
31 Correct 86 ms 512 KB Output is correct
32 Correct 11 ms 512 KB Output is correct
33 Correct 264 ms 1920 KB Output is correct
34 Correct 11 ms 512 KB Output is correct
35 Correct 327 ms 2400 KB Output is correct
36 Correct 5 ms 512 KB Output is correct
37 Correct 5 ms 512 KB Output is correct
38 Correct 200 ms 2168 KB Output is correct
39 Correct 206 ms 2168 KB Output is correct
40 Correct 320 ms 2424 KB Output is correct
41 Correct 263 ms 2168 KB Output is correct
42 Correct 260 ms 2296 KB Output is correct
43 Correct 5 ms 512 KB Output is correct
44 Correct 4 ms 512 KB Output is correct
45 Correct 5 ms 512 KB Output is correct
46 Correct 5 ms 512 KB Output is correct
47 Correct 4 ms 512 KB Output is correct
48 Correct 5 ms 512 KB Output is correct
49 Correct 319 ms 2296 KB Output is correct
50 Correct 271 ms 2168 KB Output is correct
51 Correct 269 ms 2176 KB Output is correct
52 Correct 277 ms 2268 KB Output is correct
53 Correct 281 ms 2424 KB Output is correct
54 Correct 301 ms 2304 KB Output is correct
55 Correct 231 ms 2304 KB Output is correct
56 Correct 138 ms 512 KB Output is correct
57 Correct 59 ms 512 KB Output is correct
58 Correct 167 ms 1920 KB Output is correct
59 Correct 189 ms 2040 KB Output is correct
60 Correct 252 ms 2168 KB Output is correct
61 Correct 167 ms 2040 KB Output is correct
62 Correct 188 ms 1920 KB Output is correct
63 Correct 252 ms 2048 KB Output is correct
64 Correct 163 ms 1912 KB Output is correct
65 Correct 176 ms 2048 KB Output is correct
66 Correct 229 ms 1920 KB Output is correct
67 Correct 25 ms 1912 KB Output is correct
68 Correct 190 ms 2432 KB Output is correct
69 Correct 189 ms 2552 KB Output is correct
70 Correct 302 ms 2424 KB Output is correct
71 Correct 302 ms 2552 KB Output is correct
72 Correct 264 ms 2424 KB Output is correct
73 Correct 298 ms 2552 KB Output is correct
74 Correct 302 ms 2512 KB Output is correct