답안 #584281

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
584281 2022-06-27T07:16:20 Z 반딧불(#8375) Wild Boar (JOI18_wild_boar) C++17
47 / 100
18000 ms 215408 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];
vector<pair<ll, int> > distVec[2002][4002];

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++){
        vector<pair<ll, int> > vec;
        for(int j=0; j<m*2; j++){
            for(auto c: link[i]){ /// c: i���� ������ ����
                distVec[i][j].push_back(make_pair(dist[c][j]+edge[c].v, c));
                sort(distVec[i][j].begin(), distVec[i][j].end());
                if((int)distVec[i][j].size() >= 3) distVec[i][j].pop_back();
            }
        }
    }
}

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;
        vector<pair<ll, int> > vec;
        for(int c=0; c<m*2; c++){
            if(edge[c].e == arr[d-1]){
                vec.push_back(make_pair(DP[!b][c], c));
                sort(vec.begin(), vec.end());
                if((int)vec.size() >= 3) vec.pop_back();
            }
        }

        for(int c=0; c<m*2; c++){ /// d�� ���ϴ� ���� ���� ��ȣ
            if(edge[c].e != arr[d]) continue;
            for(pair<ll, int> p1: vec){ /// ��� ������ ���� �����
                for(pair<ll, int> p2: distVec[arr[d-1]][c]){ /// d�� ���ϴ� �����
                    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:97:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     scanf("%d %d %d %d", &n, &m, &k, &l);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:99:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         scanf("%d %d %lld", &edge[i*2].s, &edge[i*2].e, &edge[i*2].v);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:111:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |     for(int i=1; i<=l; i++) scanf("%d", &arr[i]);
      |                             ~~~~~^~~~~~~~~~~~~~~
wild_boar.cpp:114:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |         scanf("%d %d", &x, &y);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 188492 KB Output is correct
2 Correct 98 ms 188640 KB Output is correct
3 Correct 92 ms 188524 KB Output is correct
4 Correct 87 ms 188620 KB Output is correct
5 Correct 89 ms 188620 KB Output is correct
6 Correct 87 ms 188524 KB Output is correct
7 Correct 89 ms 188560 KB Output is correct
8 Correct 88 ms 188596 KB Output is correct
9 Correct 86 ms 188608 KB Output is correct
10 Correct 92 ms 188600 KB Output is correct
11 Correct 90 ms 188588 KB Output is correct
12 Correct 87 ms 188584 KB Output is correct
13 Correct 105 ms 188596 KB Output is correct
14 Correct 103 ms 188532 KB Output is correct
15 Correct 96 ms 188556 KB Output is correct
16 Correct 90 ms 188536 KB Output is correct
17 Correct 85 ms 188596 KB Output is correct
18 Correct 85 ms 188644 KB Output is correct
19 Correct 88 ms 188548 KB Output is correct
20 Correct 90 ms 188584 KB Output is correct
21 Correct 87 ms 188620 KB Output is correct
22 Correct 86 ms 188548 KB Output is correct
23 Correct 88 ms 188528 KB Output is correct
24 Correct 85 ms 188528 KB Output is correct
25 Correct 88 ms 188544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 188492 KB Output is correct
2 Correct 98 ms 188640 KB Output is correct
3 Correct 92 ms 188524 KB Output is correct
4 Correct 87 ms 188620 KB Output is correct
5 Correct 89 ms 188620 KB Output is correct
6 Correct 87 ms 188524 KB Output is correct
7 Correct 89 ms 188560 KB Output is correct
8 Correct 88 ms 188596 KB Output is correct
9 Correct 86 ms 188608 KB Output is correct
10 Correct 92 ms 188600 KB Output is correct
11 Correct 90 ms 188588 KB Output is correct
12 Correct 87 ms 188584 KB Output is correct
13 Correct 105 ms 188596 KB Output is correct
14 Correct 103 ms 188532 KB Output is correct
15 Correct 96 ms 188556 KB Output is correct
16 Correct 90 ms 188536 KB Output is correct
17 Correct 85 ms 188596 KB Output is correct
18 Correct 85 ms 188644 KB Output is correct
19 Correct 88 ms 188548 KB Output is correct
20 Correct 90 ms 188584 KB Output is correct
21 Correct 87 ms 188620 KB Output is correct
22 Correct 86 ms 188548 KB Output is correct
23 Correct 88 ms 188528 KB Output is correct
24 Correct 85 ms 188528 KB Output is correct
25 Correct 88 ms 188544 KB Output is correct
26 Correct 111 ms 189040 KB Output is correct
27 Correct 150 ms 191052 KB Output is correct
28 Correct 142 ms 191056 KB Output is correct
29 Correct 405 ms 198908 KB Output is correct
30 Correct 677 ms 199052 KB Output is correct
31 Correct 1256 ms 199088 KB Output is correct
32 Correct 1268 ms 199080 KB Output is correct
33 Correct 354 ms 201152 KB Output is correct
34 Correct 336 ms 201064 KB Output is correct
35 Correct 1022 ms 200772 KB Output is correct
36 Correct 777 ms 200552 KB Output is correct
37 Correct 351 ms 201468 KB Output is correct
38 Correct 294 ms 202460 KB Output is correct
39 Correct 502 ms 201592 KB Output is correct
40 Correct 297 ms 202484 KB Output is correct
41 Correct 327 ms 202468 KB Output is correct
42 Correct 421 ms 202160 KB Output is correct
43 Correct 284 ms 203240 KB Output is correct
44 Correct 279 ms 203160 KB Output is correct
45 Correct 268 ms 203032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 188492 KB Output is correct
2 Correct 98 ms 188640 KB Output is correct
3 Correct 92 ms 188524 KB Output is correct
4 Correct 87 ms 188620 KB Output is correct
5 Correct 89 ms 188620 KB Output is correct
6 Correct 87 ms 188524 KB Output is correct
7 Correct 89 ms 188560 KB Output is correct
8 Correct 88 ms 188596 KB Output is correct
9 Correct 86 ms 188608 KB Output is correct
10 Correct 92 ms 188600 KB Output is correct
11 Correct 90 ms 188588 KB Output is correct
12 Correct 87 ms 188584 KB Output is correct
13 Correct 105 ms 188596 KB Output is correct
14 Correct 103 ms 188532 KB Output is correct
15 Correct 96 ms 188556 KB Output is correct
16 Correct 90 ms 188536 KB Output is correct
17 Correct 85 ms 188596 KB Output is correct
18 Correct 85 ms 188644 KB Output is correct
19 Correct 88 ms 188548 KB Output is correct
20 Correct 90 ms 188584 KB Output is correct
21 Correct 87 ms 188620 KB Output is correct
22 Correct 86 ms 188548 KB Output is correct
23 Correct 88 ms 188528 KB Output is correct
24 Correct 85 ms 188528 KB Output is correct
25 Correct 88 ms 188544 KB Output is correct
26 Correct 111 ms 189040 KB Output is correct
27 Correct 150 ms 191052 KB Output is correct
28 Correct 142 ms 191056 KB Output is correct
29 Correct 405 ms 198908 KB Output is correct
30 Correct 677 ms 199052 KB Output is correct
31 Correct 1256 ms 199088 KB Output is correct
32 Correct 1268 ms 199080 KB Output is correct
33 Correct 354 ms 201152 KB Output is correct
34 Correct 336 ms 201064 KB Output is correct
35 Correct 1022 ms 200772 KB Output is correct
36 Correct 777 ms 200552 KB Output is correct
37 Correct 351 ms 201468 KB Output is correct
38 Correct 294 ms 202460 KB Output is correct
39 Correct 502 ms 201592 KB Output is correct
40 Correct 297 ms 202484 KB Output is correct
41 Correct 327 ms 202468 KB Output is correct
42 Correct 421 ms 202160 KB Output is correct
43 Correct 284 ms 203240 KB Output is correct
44 Correct 279 ms 203160 KB Output is correct
45 Correct 268 ms 203032 KB Output is correct
46 Correct 504 ms 215408 KB Output is correct
47 Execution timed out 18069 ms 202476 KB Time limit exceeded
48 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 188492 KB Output is correct
2 Correct 98 ms 188640 KB Output is correct
3 Correct 92 ms 188524 KB Output is correct
4 Correct 87 ms 188620 KB Output is correct
5 Correct 89 ms 188620 KB Output is correct
6 Correct 87 ms 188524 KB Output is correct
7 Correct 89 ms 188560 KB Output is correct
8 Correct 88 ms 188596 KB Output is correct
9 Correct 86 ms 188608 KB Output is correct
10 Correct 92 ms 188600 KB Output is correct
11 Correct 90 ms 188588 KB Output is correct
12 Correct 87 ms 188584 KB Output is correct
13 Correct 105 ms 188596 KB Output is correct
14 Correct 103 ms 188532 KB Output is correct
15 Correct 96 ms 188556 KB Output is correct
16 Correct 90 ms 188536 KB Output is correct
17 Correct 85 ms 188596 KB Output is correct
18 Correct 85 ms 188644 KB Output is correct
19 Correct 88 ms 188548 KB Output is correct
20 Correct 90 ms 188584 KB Output is correct
21 Correct 87 ms 188620 KB Output is correct
22 Correct 86 ms 188548 KB Output is correct
23 Correct 88 ms 188528 KB Output is correct
24 Correct 85 ms 188528 KB Output is correct
25 Correct 88 ms 188544 KB Output is correct
26 Correct 111 ms 189040 KB Output is correct
27 Correct 150 ms 191052 KB Output is correct
28 Correct 142 ms 191056 KB Output is correct
29 Correct 405 ms 198908 KB Output is correct
30 Correct 677 ms 199052 KB Output is correct
31 Correct 1256 ms 199088 KB Output is correct
32 Correct 1268 ms 199080 KB Output is correct
33 Correct 354 ms 201152 KB Output is correct
34 Correct 336 ms 201064 KB Output is correct
35 Correct 1022 ms 200772 KB Output is correct
36 Correct 777 ms 200552 KB Output is correct
37 Correct 351 ms 201468 KB Output is correct
38 Correct 294 ms 202460 KB Output is correct
39 Correct 502 ms 201592 KB Output is correct
40 Correct 297 ms 202484 KB Output is correct
41 Correct 327 ms 202468 KB Output is correct
42 Correct 421 ms 202160 KB Output is correct
43 Correct 284 ms 203240 KB Output is correct
44 Correct 279 ms 203160 KB Output is correct
45 Correct 268 ms 203032 KB Output is correct
46 Correct 504 ms 215408 KB Output is correct
47 Execution timed out 18069 ms 202476 KB Time limit exceeded
48 Halted 0 ms 0 KB -