답안 #563880

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
563880 2022-05-18T08:05:05 Z tqbfjotld Wild Boar (JOI18_wild_boar) C++14
62 / 100
18000 ms 377996 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int INF = 1000000000000000000LL;
pair<int,pair<int,int> > edgel[4005];
vector<int> inedge[2005];
vector<int> outedge[2005];

int distm[4005][4005];

pair<int,int> thing1[2005][4005]; //node, edge
pair<int,int> thing2[2005][4005];

int n,m;
int T,L;

int arr[100005];

void handle(int &dist1, int &e1, int &dist2, int &e2, pair<int,int> opt){
    if (e1==opt.second){
        if (opt.first<=dist1){
            dist1 = opt.first;
        }
    }
    else if (e2==opt.second){
        if (opt.first<=dist2){
            dist2 = opt.first;
        }
    }
    else if (opt.first<=dist1){
        dist2 = dist1;
        e2 = e1;
        dist1 = opt.first;
        e1 = opt.second;
    }
    else if (opt.first<=dist2){
        dist2 = opt.first;
        e2 = opt.second;
    }
    if (dist1>dist2){
        swap(dist1,dist2);
        swap(e1,e2);
    }
}

main(){
    scanf("%lld%lld",&n,&m);
    scanf("%lld%lld",&T,&L);
    for (int x = 0; x<m; x++){
        int a,b,c;
        scanf("%lld%lld%lld",&a,&b,&c);
        edgel[2*x] = {c,{a,b}};
        edgel[2*x+1] = {c,{b,a}};
        inedge[a].push_back(2*x+1);
        outedge[a].push_back(2*x);
        inedge[b].push_back(2*x);
        outedge[b].push_back(2*x+1);
    }
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
    for (int x = 0; x<2*m; x++){
        for (int y = 0; y<2*m; y++){
            distm[x][y] = INF;
        }
        distm[x][x] = 0;
        pq.push({0,x});
        while (!pq.empty()){
            int u = pq.top().second;
            int d = pq.top().first;
            pq.pop();
            if (d>distm[x][u]) continue;
            for (auto y : outedge[edgel[u].second.second]){
                if ((y^1)==u) continue;
                if (d+edgel[y].first<distm[x][y]){
                    distm[x][y] = d+edgel[y].first;
                    pq.push({distm[x][y],y});
                }
            }
        }
    }
    for (int x = 0; x<=2*m; x++){
        for (int y = 1; y<=m; y++){
            thing1[y][x] = {INF,-1};
            thing2[y][x] = {INF,-1};
        }
    }

    for (int x = 0; x<2*m; x++){
        for (int y = 0; y<2*m; y++){
            if (distm[x][y]<INF){
                pair<int,int> nw = {distm[x][y],y};
                if (nw<=thing1[edgel[y].second.second][x]){
                    thing2[edgel[y].second.second][x] = thing1[edgel[y].second.second][x];
                    thing1[edgel[y].second.second][x] = nw;
                }
                else if (nw<=thing2[edgel[y].second.second][x]){
                    thing2[edgel[y].second.second][x] = nw;
                }
            }
        }
    }


    for (int x = 0; x<L; x++){
        scanf("%lld",&arr[x+1]);
    }
    for (int i = 0; i<T; i++){
        int a,b;
        scanf("%lld%lld",&a,&b);
        arr[a] = b;
        int dist1 = INF;
        int e1 = -1;
        int dist2 = INF;
        int e2 = -1;
        for (auto x : outedge[arr[1]]){
            auto t1 = thing1[arr[2]][x];
            t1.first += edgel[x].first;
            if (e1==t1.second){
                if (t1.first<=dist1){
                    dist1 = t1.first;
                }
            }
            else if (e2==t1.second){
                if (t1.first<=dist2){
                    dist2 = t1.first;
                }
            }
            else if (t1.first<=dist1){
                dist2 = dist1;
                e2 = e1;
                dist1 = t1.first;
                e1 = t1.second;
            }
            else if (t1.first<=dist2){
                dist2 = t1.first;
                e2 = t1.second;
            }
            if (dist1>dist2){
                swap(dist1,dist2);
                swap(e1,e2);
            }

            auto t2 = thing2[arr[2]][x];
            t2.first += edgel[x].first;
            if (e1==t2.second){
                if (t2.first<=dist1){
                    dist1 = t2.first;
                }
            }
            else if (e2==t2.second){
                if (t2.first<=dist2){
                    dist2 = t2.first;
                }
            }
            else if (t2.first<=dist1){
                dist2 = dist1;
                e2 = e1;
                dist1 = t2.first;
                e1 = t2.second;
            }
            else if (t2.first<=dist2){
                dist2 = t2.first;
                e2 = t2.second;
            }
            if (dist1>dist2){
                swap(dist1,dist2);
                swap(e1,e2);
            }
        }
        if (dist1==INF){
            printf("-1\n");
            continue;
        }
        for (int x = 3; x<=L; x++){
            //printf("vals are %lld %lld %lld %lld\n",dist1,e1,dist2,e2);

            int ndist1 = INF;
            int ne1 = -1;
            int ndist2 = INF;
            int ne2 = -1;
            auto t1 = thing1[arr[x]][e1];
            t1.first += dist1;
            handle(ndist1,ne1,ndist2,ne2,t1);
            auto t2 = thing2[arr[x]][e1];
            t2.first += dist1;
            handle(ndist1,ne1,ndist2,ne2,t2);
            auto t3 = thing1[arr[x]][e2];
            t3.first += dist2;
            handle(ndist1,ne1,ndist2,ne2,t3);
            auto t4 = thing2[arr[x]][e2];
            t4.first += dist2;
            handle(ndist1,ne1,ndist2,ne2,t4);

            dist1 = ndist1;
            dist2 = ndist2;
            e1 = ne1;
            e2 = ne2;

            if (dist1==INF){
                printf("-1\n");
                break;
            }
        }
        if (dist1==INF) continue;
        printf("%lld\n",dist1);
    }
}

Compilation message

wild_boar.cpp:47:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   47 | main(){
      | ^~~~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     scanf("%lld%lld",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
wild_boar.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |     scanf("%lld%lld",&T,&L);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
wild_boar.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         scanf("%lld%lld%lld",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         scanf("%lld",&arr[x+1]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~
wild_boar.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         scanf("%lld%lld",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 0 ms 596 KB Output is correct
24 Correct 1 ms 480 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 0 ms 596 KB Output is correct
24 Correct 1 ms 480 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 9 ms 3668 KB Output is correct
28 Correct 9 ms 3668 KB Output is correct
29 Correct 61 ms 14464 KB Output is correct
30 Correct 62 ms 14612 KB Output is correct
31 Correct 63 ms 14548 KB Output is correct
32 Correct 65 ms 14448 KB Output is correct
33 Correct 59 ms 14496 KB Output is correct
34 Correct 59 ms 14572 KB Output is correct
35 Correct 60 ms 14484 KB Output is correct
36 Correct 60 ms 14604 KB Output is correct
37 Correct 60 ms 14480 KB Output is correct
38 Correct 58 ms 14504 KB Output is correct
39 Correct 53 ms 14532 KB Output is correct
40 Correct 58 ms 14512 KB Output is correct
41 Correct 58 ms 14596 KB Output is correct
42 Correct 50 ms 14524 KB Output is correct
43 Correct 53 ms 14440 KB Output is correct
44 Correct 51 ms 14524 KB Output is correct
45 Correct 28 ms 14568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 0 ms 596 KB Output is correct
24 Correct 1 ms 480 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 9 ms 3668 KB Output is correct
28 Correct 9 ms 3668 KB Output is correct
29 Correct 61 ms 14464 KB Output is correct
30 Correct 62 ms 14612 KB Output is correct
31 Correct 63 ms 14548 KB Output is correct
32 Correct 65 ms 14448 KB Output is correct
33 Correct 59 ms 14496 KB Output is correct
34 Correct 59 ms 14572 KB Output is correct
35 Correct 60 ms 14484 KB Output is correct
36 Correct 60 ms 14604 KB Output is correct
37 Correct 60 ms 14480 KB Output is correct
38 Correct 58 ms 14504 KB Output is correct
39 Correct 53 ms 14532 KB Output is correct
40 Correct 58 ms 14512 KB Output is correct
41 Correct 58 ms 14596 KB Output is correct
42 Correct 50 ms 14524 KB Output is correct
43 Correct 53 ms 14440 KB Output is correct
44 Correct 51 ms 14524 KB Output is correct
45 Correct 28 ms 14568 KB Output is correct
46 Correct 153 ms 32020 KB Output is correct
47 Correct 4915 ms 377720 KB Output is correct
48 Correct 4403 ms 377612 KB Output is correct
49 Correct 3552 ms 377788 KB Output is correct
50 Correct 3786 ms 377652 KB Output is correct
51 Correct 4269 ms 377592 KB Output is correct
52 Correct 2645 ms 377804 KB Output is correct
53 Correct 2644 ms 377840 KB Output is correct
54 Correct 2641 ms 377728 KB Output is correct
55 Correct 2649 ms 377852 KB Output is correct
56 Correct 2644 ms 377808 KB Output is correct
57 Correct 2660 ms 377620 KB Output is correct
58 Correct 2600 ms 377628 KB Output is correct
59 Correct 2502 ms 377632 KB Output is correct
60 Correct 2509 ms 377728 KB Output is correct
61 Correct 2386 ms 377740 KB Output is correct
62 Correct 2269 ms 377788 KB Output is correct
63 Correct 2229 ms 377660 KB Output is correct
64 Correct 919 ms 377700 KB Output is correct
65 Correct 913 ms 377612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 0 ms 596 KB Output is correct
24 Correct 1 ms 480 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 9 ms 3668 KB Output is correct
28 Correct 9 ms 3668 KB Output is correct
29 Correct 61 ms 14464 KB Output is correct
30 Correct 62 ms 14612 KB Output is correct
31 Correct 63 ms 14548 KB Output is correct
32 Correct 65 ms 14448 KB Output is correct
33 Correct 59 ms 14496 KB Output is correct
34 Correct 59 ms 14572 KB Output is correct
35 Correct 60 ms 14484 KB Output is correct
36 Correct 60 ms 14604 KB Output is correct
37 Correct 60 ms 14480 KB Output is correct
38 Correct 58 ms 14504 KB Output is correct
39 Correct 53 ms 14532 KB Output is correct
40 Correct 58 ms 14512 KB Output is correct
41 Correct 58 ms 14596 KB Output is correct
42 Correct 50 ms 14524 KB Output is correct
43 Correct 53 ms 14440 KB Output is correct
44 Correct 51 ms 14524 KB Output is correct
45 Correct 28 ms 14568 KB Output is correct
46 Correct 153 ms 32020 KB Output is correct
47 Correct 4915 ms 377720 KB Output is correct
48 Correct 4403 ms 377612 KB Output is correct
49 Correct 3552 ms 377788 KB Output is correct
50 Correct 3786 ms 377652 KB Output is correct
51 Correct 4269 ms 377592 KB Output is correct
52 Correct 2645 ms 377804 KB Output is correct
53 Correct 2644 ms 377840 KB Output is correct
54 Correct 2641 ms 377728 KB Output is correct
55 Correct 2649 ms 377852 KB Output is correct
56 Correct 2644 ms 377808 KB Output is correct
57 Correct 2660 ms 377620 KB Output is correct
58 Correct 2600 ms 377628 KB Output is correct
59 Correct 2502 ms 377632 KB Output is correct
60 Correct 2509 ms 377728 KB Output is correct
61 Correct 2386 ms 377740 KB Output is correct
62 Correct 2269 ms 377788 KB Output is correct
63 Correct 2229 ms 377660 KB Output is correct
64 Correct 919 ms 377700 KB Output is correct
65 Correct 913 ms 377612 KB Output is correct
66 Correct 326 ms 1996 KB Output is correct
67 Correct 228 ms 110860 KB Output is correct
68 Correct 820 ms 377076 KB Output is correct
69 Correct 856 ms 377092 KB Output is correct
70 Correct 928 ms 377844 KB Output is correct
71 Execution timed out 18081 ms 377996 KB Time limit exceeded
72 Halted 0 ms 0 KB -