답안 #815692

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
815692 2023-08-08T18:45:04 Z anton Olympic Bus (JOI20_ho_t4) C++17
100 / 100
929 ms 4708 KB
#include<bits/stdc++.h>

using namespace std;
#define int long long
#define pii pair<int, int>


struct Edge{
    int u, v, c, d;
    Edge(){};
};

const int MAX_E = 5*1e4;
const int MAX_V = 200;
const int INF = 1e15;
int v, e;
Edge edges[MAX_E];
vector<pii> adj[MAX_V];

int find_len(int begin, int dest){
    vector<int> dist(v, INF);
    vector<pii> prev(v, pii(-1, -1));
    dist[begin] = 0;

    priority_queue<pii> pq;
    pq.push(pii(0, begin));

    while(pq.size()>0){
        auto cur = pq.top();
        pq.pop();

        int u = cur.second;
        int u_dist = -cur.first;

        if(dist[u] == u_dist){
            //cout<<u<<" "<<dist[u]<<endl;
            for(auto next: adj[u]){
                int next_dist = u_dist + edges[next.second].c;
                if(next_dist< dist[next.first]){
                    dist[next.first] = next_dist;
                    pq.push(pii(-next_dist, next.first));
                    prev[next.first].first = u;
                    prev[next.first].second = next.second;
                }
            }
        }
    }
    return dist[dest];
}
vector<int> find_path(int begin, int dest){
    vector<int> dist(v, INF);
    vector<pii> prev(v, pii(-1, -1));
    dist[begin] = 0;

    priority_queue<pii> pq;
    pq.push(pii(0, begin));

    while(pq.size()>0){
        auto cur = pq.top();
        pq.pop();

        int u = cur.second;
        int u_dist = -cur.first;

        if(dist[u] == u_dist){
            //cout<<u<<" "<<dist[u]<<endl;
            for(auto next: adj[u]){
                int next_dist = u_dist + edges[next.second].c;
                if(next_dist< dist[next.first]){
                    dist[next.first] = next_dist;
                    pq.push(pii(-next_dist, next.first));
                    prev[next.first].first = u;
                    prev[next.first].second = next.second;
                }
            }
        }
    }

    if(dist[dest] == INF){
        return vector<int>(0);
    }
    else{
        int cur = dest;
        vector<int> res;
        while(cur!=begin){
            res.push_back(prev[cur].second);
            cur = prev[cur].first;
        }
        return res;
    }
}

int d[MAX_V][MAX_V];
bool is_path[MAX_E];

void find_d(){
    for(int i = 0; i<v; i++){
        for(int j = 0; j<v; j++){
            d[i][j] = INF;
        }
        adj[i].clear();
    }

    for(int i = 0; i<v; i++){
        d[i][i] = 0;
    }
    for(int i = 0; i<e; i++){
            
        d[edges[i].u][edges[i].v] = min(d[edges[i].u][edges[i].v], edges[i].c);
        adj[edges[i].u].push_back(pii(edges[i].v, i));
    }
}

void floyd(){
    for(int mid= 0; mid<v; mid++){
        for(int f= 0; f<v; f++){
            for(int l = 0; l<v; l++){
                d[f][l] = min(d[f][l], d[f][mid] + d[mid][l]);
            }
        }
    }
}

signed main(){
    cin>>v>>e;


    for(int i = 0; i<e; i++){
        cin>>edges[i].u>>edges[i].v>>edges[i].c>>edges[i].d;
        edges[i].u--;
        edges[i].v--;

        //adj[edges[i].u].push_back(pii(edges[i].v, i));
        //d[edges[i].u][edges[i].v] = min(d[edges[i].u][edges[i].v], edges[i].c);
    }

    find_d();

    auto p1 = find_path(0, v-1);
    auto p2 = find_path(v-1, 0);

    if(p1.size()>201||p2.size()>201){
        return 4;
    }
    for(auto e : p1){
        is_path[e] = true;
    }
    for(auto e: p2){
        is_path[e] = true;
    }
    floyd();

    int res = d[0][v-1] + d[v-1][0];

    //cout<<go<<" "<<back<<endl;

    for(int i = 0; i<e; i++){
        if(!is_path[i]){
            int from = edges[i].v;
            int to = edges[i].u;
            res = min(res, min(d[0][v-1], d[0][from] + edges[i].c + d[to][v-1] ) + min(d[v-1][0], d[v-1][from] + edges[i].c + d[to][0])+  edges[i].d);
        }
    }

    for(int i = 0; i<e; i++){
        if(is_path[i]){
            swap(edges[i].u, edges[i].v);
            find_d();
            swap(edges[i].u, edges[i].v);
            res = min(res, find_len(0, v-1) + find_len(v-1, 0) +edges[i].d);
        }
    }

    if(res>=INF){
        cout<<-1<<endl;
        return 0;
    }

    cout<<res<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 596 KB Output is correct
2 Correct 8 ms 596 KB Output is correct
3 Correct 9 ms 700 KB Output is correct
4 Correct 10 ms 596 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 644 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 29 ms 704 KB Output is correct
11 Correct 43 ms 696 KB Output is correct
12 Correct 34 ms 700 KB Output is correct
13 Correct 9 ms 692 KB Output is correct
14 Correct 9 ms 596 KB Output is correct
15 Correct 11 ms 692 KB Output is correct
16 Correct 9 ms 696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 3548 KB Output is correct
2 Correct 50 ms 3516 KB Output is correct
3 Correct 52 ms 3512 KB Output is correct
4 Correct 10 ms 756 KB Output is correct
5 Correct 11 ms 596 KB Output is correct
6 Correct 8 ms 596 KB Output is correct
7 Correct 8 ms 596 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 47 ms 3400 KB Output is correct
10 Correct 45 ms 3384 KB Output is correct
11 Correct 59 ms 3436 KB Output is correct
12 Correct 51 ms 3500 KB Output is correct
13 Correct 52 ms 3564 KB Output is correct
14 Correct 52 ms 3664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 692 KB Output is correct
2 Correct 8 ms 596 KB Output is correct
3 Correct 38 ms 2656 KB Output is correct
4 Correct 8 ms 596 KB Output is correct
5 Correct 44 ms 3360 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 41 ms 3368 KB Output is correct
9 Correct 42 ms 4300 KB Output is correct
10 Correct 44 ms 4332 KB Output is correct
11 Correct 46 ms 4356 KB Output is correct
12 Correct 44 ms 4448 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 312 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 312 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 45 ms 4452 KB Output is correct
20 Correct 44 ms 4308 KB Output is correct
21 Correct 44 ms 4428 KB Output is correct
22 Correct 44 ms 4412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 596 KB Output is correct
2 Correct 8 ms 596 KB Output is correct
3 Correct 9 ms 700 KB Output is correct
4 Correct 10 ms 596 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 8 ms 644 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 29 ms 704 KB Output is correct
11 Correct 43 ms 696 KB Output is correct
12 Correct 34 ms 700 KB Output is correct
13 Correct 9 ms 692 KB Output is correct
14 Correct 9 ms 596 KB Output is correct
15 Correct 11 ms 692 KB Output is correct
16 Correct 9 ms 696 KB Output is correct
17 Correct 61 ms 3548 KB Output is correct
18 Correct 50 ms 3516 KB Output is correct
19 Correct 52 ms 3512 KB Output is correct
20 Correct 10 ms 756 KB Output is correct
21 Correct 11 ms 596 KB Output is correct
22 Correct 8 ms 596 KB Output is correct
23 Correct 8 ms 596 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 47 ms 3400 KB Output is correct
26 Correct 45 ms 3384 KB Output is correct
27 Correct 59 ms 3436 KB Output is correct
28 Correct 51 ms 3500 KB Output is correct
29 Correct 52 ms 3564 KB Output is correct
30 Correct 52 ms 3664 KB Output is correct
31 Correct 9 ms 692 KB Output is correct
32 Correct 8 ms 596 KB Output is correct
33 Correct 38 ms 2656 KB Output is correct
34 Correct 8 ms 596 KB Output is correct
35 Correct 44 ms 3360 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 41 ms 3368 KB Output is correct
39 Correct 42 ms 4300 KB Output is correct
40 Correct 44 ms 4332 KB Output is correct
41 Correct 46 ms 4356 KB Output is correct
42 Correct 44 ms 4448 KB Output is correct
43 Correct 1 ms 212 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 0 ms 312 KB Output is correct
46 Correct 0 ms 212 KB Output is correct
47 Correct 1 ms 312 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 45 ms 4452 KB Output is correct
50 Correct 44 ms 4308 KB Output is correct
51 Correct 44 ms 4428 KB Output is correct
52 Correct 44 ms 4412 KB Output is correct
53 Correct 49 ms 4496 KB Output is correct
54 Correct 54 ms 4408 KB Output is correct
55 Correct 56 ms 4604 KB Output is correct
56 Correct 9 ms 724 KB Output is correct
57 Correct 9 ms 604 KB Output is correct
58 Correct 177 ms 3568 KB Output is correct
59 Correct 204 ms 3688 KB Output is correct
60 Correct 277 ms 3672 KB Output is correct
61 Correct 174 ms 3660 KB Output is correct
62 Correct 209 ms 3580 KB Output is correct
63 Correct 269 ms 3664 KB Output is correct
64 Correct 654 ms 4212 KB Output is correct
65 Correct 725 ms 4216 KB Output is correct
66 Correct 929 ms 4188 KB Output is correct
67 Correct 31 ms 3412 KB Output is correct
68 Correct 48 ms 4600 KB Output is correct
69 Correct 47 ms 4636 KB Output is correct
70 Correct 55 ms 4576 KB Output is correct
71 Correct 52 ms 4660 KB Output is correct
72 Correct 51 ms 4556 KB Output is correct
73 Correct 52 ms 4708 KB Output is correct
74 Correct 55 ms 4680 KB Output is correct