답안 #896544

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
896544 2024-01-01T16:18:58 Z ace5 Olympic Bus (JOI20_ho_t4) C++17
100 / 100
289 ms 5768 KB
#include <bits/stdc++.h>

using namespace std;

#define int int64_t

const int maxn = 201;
const int INF = 1e18;

vector<pair<int,int>> g[maxn];
vector<int> c,d;
vector<pair<int,int>> r;
int nr[maxn][maxn];
int dp[maxn][maxn];
int rr[maxn][maxn];
vector<bool> ru;

void rec(int s,int t)
{
    if(dp[s][t] == INF)
    {
        return ;
    }
    if(rr[s][t] == -1)
    {
        ru[nr[s][t]] = 1;
        return ;
    }
    int rp = rr[s][t];
    rec(s,rp);
    rec(rp,t);
    return ;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for(int i =0;i < n;++i)
    {
        for(int j = 0;j < n;++j)
        {
            nr[i][j] = -1;
        }
    }
    for(int i =0;i < m;++i)
    {
        int u,v,e,f;
        cin >> u >> v >> e >> f;
        u--;
        v--;
        g[u].push_back({v,i});
        c.push_back(e);
        d.push_back(f);
        r.push_back({u,v});
        if(nr[u][v] == -1 || c[nr[u][v]] > e)
        {
            nr[u][v] = i;
        }
    }
   // cout << 1;
    for(int i =0;i < n;++i)
    {
        for(int j = 0;j < n;++j)
        {
            if(i == j)
            {
                dp[i][j] = 0;
            }
            else
                dp[i][j] = (nr[i][j] == -1 ? INF : c[nr[i][j]]);
            rr[i][j] = -1;
        }
    }
  //  cout << 1;
    for(int k = 0;k < n;++k)
    {
        for(int i = 0;i < n;++i)
        {
            for(int j = 0;j < n;++j)
            {
                if(dp[i][j] > dp[i][k] + dp[k][j])
                {
                    dp[i][j] = dp[i][k] + dp[k][j];
                    rr[i][j] = k;
                }
            }
        }
    }
   // cout << 1;
    ru.resize(r.size());
    rec(0,n-1);
    rec(n-1,0);
    int64_t ans = dp[0][n-1] + dp[n-1][0];
   // cout << ans << ' ';
    for(int i = 0;i < m;++i)
    {
        if(!ru[i])
        {
            //cout << i << ' ';
            ans = min(ans,d[i] + min(dp[0][r[i].second] + c[i] + dp[r[i].first][n-1],dp[0][n-1]) + min(dp[n-1][r[i].second] + c[i] + dp[r[i].first][0],dp[n-1][0]));
        }
        else
        {
            int64_t tres = d[i];
            vector<vector<pair<int,int>>> g2(n);
            for(int j = 0;j < m;++j)
            {
                if(j != i)
                {
                    g2[r[j].first].push_back({r[j].second,c[j]});
                }
                else
                {
                    g2[r[j].second].push_back({r[j].first,c[j]});
                }
            }
            vector<int> vis(n);
            vector<int> d(n,INF);
            d[0] = 0;
            while(true)
            {
                int pm = -1;
                for(int k = 0;k < n;++k)
                {
                    if(!vis[k] && (pm == -1 || d[pm] > d[k]))
                    {
                        pm = k;
                    }
                }
                if(pm == -1 || d[pm] == INF)
                {
                    break;
                }
                vis[pm] = 1;
                for(auto [u,dd] : g2[pm])
                {
                    if(d[pm] + dd < d[u])
                    {
                        d[u] = d[pm] + dd;
                    }
                }
            }
            tres += d[n-1];
            vis.clear();
            vis.resize(n);
            d.clear();
            d.resize(n);
            for(int j = 0;j < n;++j)
            {
                d[j] = INF;
            }
            d[n-1] = 0;
            while(true)
            {
                int pm = -1;
                for(int k = 0;k < n;++k)
                {
                    if(!vis[k] && (pm == -1 || d[pm] > d[k]))
                    {
                        pm = k;
                    }
                }
                if(pm == -1 || d[pm] == INF)
                {
                    break;
                }
                vis[pm] = 1;
                for(auto [u,dd] : g2[pm])
                {
                    if(d[pm] + dd < d[u])
                    {
                        d[u] = d[pm] + dd;
                    }
                }
            }
            tres += d[0];
            ans = min(ans,tres);
        }
    }
    cout << (ans >= INF ? -1 : ans);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 1368 KB Output is correct
2 Correct 8 ms 1372 KB Output is correct
3 Correct 14 ms 1476 KB Output is correct
4 Correct 13 ms 1484 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 9 ms 1432 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 504 KB Output is correct
10 Correct 52 ms 1500 KB Output is correct
11 Correct 67 ms 1368 KB Output is correct
12 Correct 62 ms 1368 KB Output is correct
13 Correct 13 ms 1372 KB Output is correct
14 Correct 11 ms 1372 KB Output is correct
15 Correct 10 ms 1372 KB Output is correct
16 Correct 11 ms 1476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5496 KB Output is correct
2 Correct 34 ms 5428 KB Output is correct
3 Correct 34 ms 5532 KB Output is correct
4 Correct 11 ms 1372 KB Output is correct
5 Correct 12 ms 1372 KB Output is correct
6 Correct 9 ms 1372 KB Output is correct
7 Correct 8 ms 1448 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 24 ms 4240 KB Output is correct
10 Correct 25 ms 4328 KB Output is correct
11 Correct 36 ms 5508 KB Output is correct
12 Correct 32 ms 5416 KB Output is correct
13 Correct 33 ms 5508 KB Output is correct
14 Correct 33 ms 5768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 1372 KB Output is correct
2 Correct 8 ms 1436 KB Output is correct
3 Correct 22 ms 4264 KB Output is correct
4 Correct 8 ms 1372 KB Output is correct
5 Correct 24 ms 5356 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 29 ms 4172 KB Output is correct
9 Correct 21 ms 4236 KB Output is correct
10 Correct 26 ms 5324 KB Output is correct
11 Correct 27 ms 5448 KB Output is correct
12 Correct 27 ms 5564 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 344 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 24 ms 5620 KB Output is correct
20 Correct 24 ms 5284 KB Output is correct
21 Correct 25 ms 5360 KB Output is correct
22 Correct 26 ms 5360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 1368 KB Output is correct
2 Correct 8 ms 1372 KB Output is correct
3 Correct 14 ms 1476 KB Output is correct
4 Correct 13 ms 1484 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 9 ms 1432 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 504 KB Output is correct
10 Correct 52 ms 1500 KB Output is correct
11 Correct 67 ms 1368 KB Output is correct
12 Correct 62 ms 1368 KB Output is correct
13 Correct 13 ms 1372 KB Output is correct
14 Correct 11 ms 1372 KB Output is correct
15 Correct 10 ms 1372 KB Output is correct
16 Correct 11 ms 1476 KB Output is correct
17 Correct 34 ms 5496 KB Output is correct
18 Correct 34 ms 5428 KB Output is correct
19 Correct 34 ms 5532 KB Output is correct
20 Correct 11 ms 1372 KB Output is correct
21 Correct 12 ms 1372 KB Output is correct
22 Correct 9 ms 1372 KB Output is correct
23 Correct 8 ms 1448 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 24 ms 4240 KB Output is correct
26 Correct 25 ms 4328 KB Output is correct
27 Correct 36 ms 5508 KB Output is correct
28 Correct 32 ms 5416 KB Output is correct
29 Correct 33 ms 5508 KB Output is correct
30 Correct 33 ms 5768 KB Output is correct
31 Correct 13 ms 1372 KB Output is correct
32 Correct 8 ms 1436 KB Output is correct
33 Correct 22 ms 4264 KB Output is correct
34 Correct 8 ms 1372 KB Output is correct
35 Correct 24 ms 5356 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 29 ms 4172 KB Output is correct
39 Correct 21 ms 4236 KB Output is correct
40 Correct 26 ms 5324 KB Output is correct
41 Correct 27 ms 5448 KB Output is correct
42 Correct 27 ms 5564 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 344 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 24 ms 5620 KB Output is correct
50 Correct 24 ms 5284 KB Output is correct
51 Correct 25 ms 5360 KB Output is correct
52 Correct 26 ms 5360 KB Output is correct
53 Correct 42 ms 5500 KB Output is correct
54 Correct 39 ms 5400 KB Output is correct
55 Correct 38 ms 5440 KB Output is correct
56 Correct 12 ms 1372 KB Output is correct
57 Correct 11 ms 1480 KB Output is correct
58 Correct 166 ms 4268 KB Output is correct
59 Correct 187 ms 4272 KB Output is correct
60 Correct 278 ms 4496 KB Output is correct
61 Correct 166 ms 4392 KB Output is correct
62 Correct 226 ms 4256 KB Output is correct
63 Correct 250 ms 4472 KB Output is correct
64 Correct 203 ms 4728 KB Output is correct
65 Correct 228 ms 4720 KB Output is correct
66 Correct 289 ms 4704 KB Output is correct
67 Correct 11 ms 4220 KB Output is correct
68 Correct 25 ms 4232 KB Output is correct
69 Correct 32 ms 4292 KB Output is correct
70 Correct 36 ms 5376 KB Output is correct
71 Correct 32 ms 5384 KB Output is correct
72 Correct 31 ms 5284 KB Output is correct
73 Correct 43 ms 5620 KB Output is correct
74 Correct 42 ms 5500 KB Output is correct