Submission #584304

# Submission time Handle Problem Language Result Execution time Memory
584304 2022-06-27T07:34:47 Z 반딧불(#8375) Wild Boar (JOI18_wild_boar) C++17
47 / 100
18000 ms 22672 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Edge{
    int s, e, i; ll v;
    Edge(){}
    Edge(int s, int e, int i, ll v): s(s), e(e), i(i), v(v){}
};

struct dat{
    int x; ll y;
    dat(){}
    dat(int x, ll y): x(x), y(y){}
    bool operator<(const dat &r)const{
        return y>r.y;
    }
};

int n, m, k, l;
vector<int> link[2002];
vector<int> revLink[2002];
Edge edge[4002];
int arr[100002];
ll dist[4002][4002];
pair<ll, int> distVec[2002][4002][3];

void init(){
    for(int i=0; i<m*2; i++){
        for(int j=0; j<m*2; j++) dist[i][j] = 1e18;
        priority_queue<dat> pq;
        pq.push(dat(i, 0));
        while(!pq.empty()){
            dat tmp = pq.top(); pq.pop();
            if(dist[i][tmp.x] < 1e18) continue;
            int xe = tmp.x, x = edge[xe].e;
            dist[i][xe] = tmp.y;
            for(auto ye: link[x]){
                if((xe^ye) == 1) continue;
                pq.push(dat(ye, tmp.y + edge[ye].v));
            }
        }
    }

    /// ����
    for(int i=1; i<=n; i++){
        for(int j=0; j<m*2; j++){
            distVec[i][j][0] = distVec[i][j][1] = make_pair(1e18, -1);
            for(auto c: link[i]){ /// c: i���� ������ ����
                distVec[i][j][2] = make_pair(dist[c][j]+edge[c].v, c);
                for(int pnt=2; pnt>0; pnt--){
                    if(distVec[i][j][pnt-1] > distVec[i][j][pnt]){
                        swap(distVec[i][j][pnt-1], distVec[i][j][pnt]);
                    }
                    else break;
                }
            }
        }
    }
}

ll DP[2][4002];
void solve(){
    for(int i=0; i<m*2; i++) DP[0][i] = DP[1][i] = 1e18;
    for(int i=0; i<m*2; i++){
        if(edge[i].e != arr[2]) continue;
        for(auto s: link[arr[1]]){
            DP[0][i] = min(DP[0][i], dist[s][i]+edge[s].v);
        }
    }
    for(int d=3; d<=l; d++){
        int b = d%2;
        for(int i=0; i<m*2; i++) DP[b][i] = 1e18;
        pair<ll, int> vec[3];
        vec[0]=vec[1]=vec[2]=make_pair(1e18, -1);
        for(int c=0; c<m*2; c++){
            if(edge[c].e == arr[d-1]){
                vec[2] = make_pair(DP[!b][c], c);
                for(int pnt=2; pnt>0; pnt--){
                    if(vec[pnt-1] > vec[pnt]){
                        swap(vec[pnt-1], vec[pnt]);
                    }
                    else break;
                }
            }
        }

        for(int c=0; c<m*2; c++){ /// d�� ���ϴ� ���� ���� ��ȣ
            if(edge[c].e != arr[d]) continue;
            for(int pp=0; pp < 2; pp++){ /// ��� ������ ���� �����
                pair<ll, int> p1 = vec[pp];
                if(p1.second<0) continue;
                for(int qq = 0; qq < 2; qq++){ /// d�� ���ϴ� �����
                    pair<ll, int> p2 = distVec[arr[d-1]][c][qq];
                    if(p2.second<0) continue;
                    if((p1.second ^ p2.second) == 1) continue;
                    DP[b][c] = min(DP[b][c], p1.first + p2.first);
                }
            }
        }
    }
    ll ans = 1e18;
    for(int i=0; i<m*2; i++) if(edge[i].e == arr[l]) ans = min(ans, DP[l%2][i]);
    printf("%lld\n", ans == 1e18 ? -1 : ans);
}

int main(){
    scanf("%d %d %d %d", &n, &m, &k, &l);
    for(int i=0; i<m; i++){
        scanf("%d %d %lld", &edge[i*2].s, &edge[i*2].e, &edge[i*2].v);
        edge[i*2+1] = edge[i*2];
        swap(edge[i*2+1].s, edge[i*2+1].e);
        edge[i*2].i = i*2, edge[i*2+1].i = i*2+1;

        link[edge[i*2].s].push_back(i*2);
        link[edge[i*2+1].s].push_back(i*2+1);
        revLink[edge[i*2].e].push_back(i*2);
        revLink[edge[i*2+1].e].push_back(i*2+1);
    }
    init();

    for(int i=1; i<=l; i++) scanf("%d", &arr[i]);
    for(int i=1; i<=k; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        arr[x] = y;
        solve();
    }
}

Compilation message

wild_boar.cpp: In function 'int main()':
wild_boar.cpp:110:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |     scanf("%d %d %d %d", &n, &m, &k, &l);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:112:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |         scanf("%d %d %lld", &edge[i*2].s, &edge[i*2].e, &edge[i*2].v);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:124:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |     for(int i=1; i<=l; i++) scanf("%d", &arr[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~
wild_boar.cpp:127:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 528 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 528 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 532 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 528 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 528 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 532 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 43 ms 3480 KB Output is correct
28 Correct 44 ms 3420 KB Output is correct
29 Correct 272 ms 9384 KB Output is correct
30 Correct 452 ms 9644 KB Output is correct
31 Correct 1016 ms 9668 KB Output is correct
32 Correct 1014 ms 9772 KB Output is correct
33 Correct 197 ms 11092 KB Output is correct
34 Correct 206 ms 11088 KB Output is correct
35 Correct 785 ms 11188 KB Output is correct
36 Correct 578 ms 11176 KB Output is correct
37 Correct 208 ms 11092 KB Output is correct
38 Correct 167 ms 12580 KB Output is correct
39 Correct 310 ms 12636 KB Output is correct
40 Correct 164 ms 12620 KB Output is correct
41 Correct 170 ms 12644 KB Output is correct
42 Correct 261 ms 13716 KB Output is correct
43 Correct 158 ms 14212 KB Output is correct
44 Correct 144 ms 14304 KB Output is correct
45 Correct 111 ms 15936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 528 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 528 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 532 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 43 ms 3480 KB Output is correct
28 Correct 44 ms 3420 KB Output is correct
29 Correct 272 ms 9384 KB Output is correct
30 Correct 452 ms 9644 KB Output is correct
31 Correct 1016 ms 9668 KB Output is correct
32 Correct 1014 ms 9772 KB Output is correct
33 Correct 197 ms 11092 KB Output is correct
34 Correct 206 ms 11088 KB Output is correct
35 Correct 785 ms 11188 KB Output is correct
36 Correct 578 ms 11176 KB Output is correct
37 Correct 208 ms 11092 KB Output is correct
38 Correct 167 ms 12580 KB Output is correct
39 Correct 310 ms 12636 KB Output is correct
40 Correct 164 ms 12620 KB Output is correct
41 Correct 170 ms 12644 KB Output is correct
42 Correct 261 ms 13716 KB Output is correct
43 Correct 158 ms 14212 KB Output is correct
44 Correct 144 ms 14304 KB Output is correct
45 Correct 111 ms 15936 KB Output is correct
46 Correct 403 ms 22672 KB Output is correct
47 Execution timed out 18071 ms 14704 KB Time limit exceeded
48 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 528 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 480 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 528 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 532 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
27 Correct 43 ms 3480 KB Output is correct
28 Correct 44 ms 3420 KB Output is correct
29 Correct 272 ms 9384 KB Output is correct
30 Correct 452 ms 9644 KB Output is correct
31 Correct 1016 ms 9668 KB Output is correct
32 Correct 1014 ms 9772 KB Output is correct
33 Correct 197 ms 11092 KB Output is correct
34 Correct 206 ms 11088 KB Output is correct
35 Correct 785 ms 11188 KB Output is correct
36 Correct 578 ms 11176 KB Output is correct
37 Correct 208 ms 11092 KB Output is correct
38 Correct 167 ms 12580 KB Output is correct
39 Correct 310 ms 12636 KB Output is correct
40 Correct 164 ms 12620 KB Output is correct
41 Correct 170 ms 12644 KB Output is correct
42 Correct 261 ms 13716 KB Output is correct
43 Correct 158 ms 14212 KB Output is correct
44 Correct 144 ms 14304 KB Output is correct
45 Correct 111 ms 15936 KB Output is correct
46 Correct 403 ms 22672 KB Output is correct
47 Execution timed out 18071 ms 14704 KB Time limit exceeded
48 Halted 0 ms 0 KB -