답안 #584328

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
584328 2022-06-27T08:19:57 Z 조영욱(#8377) Wild Boar (JOI18_wild_boar) C++17
12 / 100
3461 ms 2880 KB
#include <bits/stdc++.h>
using namespace std;

int n,m,t,l;
typedef pair<long long,int> P;
vector<P> adj[4001];
int arr[100001];
long long dp[100001];
long long dp0[100001];
int pos[100001];
bool vis[4001];
long long dist[4001];
const long long INF=1e18;
long long c[2001];

void dijk(int st,int en,int a,int b) {
    priority_queue<P,vector<P>,greater<P>> pq;
    pq.push(P(0,st));
    for(int i=1;i<=n*2;i++) {
        vis[i]=false;
        dist[i]=INF;
    }
    dist[st]=0;
    while (!pq.empty()) {
        P now=pq.top();
        pq.pop();
        if (vis[now.second]) {
            continue;
        }
        vis[now.second]=true;
        if (now.second==en) {
            continue;
        }
        for(int i=0;i<adj[now.second].size();i++) {
            int nt=adj[now.second][i].first;
            if (now.second==a&&nt==b) {
                continue;
            }
            if (nt==a&&now.second==b) {
                continue;
            }
            if (dist[nt]>dist[now.second]+adj[now.second][i].second) {
                dist[nt]=dist[now.second]+adj[now.second][i].second;
                pq.push(P(dist[nt],nt));
            }
        }
    }
}

int main(void) {
    scanf("%d %d %d %d",&n,&m,&t,&l);
    for(int i=0;i<m;i++) {
        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        adj[u].push_back(P(v,w));
        adj[v].push_back(P(u,w));
    }
    for(int i=1;i<=n;i++) {
c[i]=INF;
        for(int j=0;j<adj[i].size();j++) {
            int nt=adj[i][j].first;
            dijk(i,nt,i,nt);
            c[i]=min(c[i],dist[nt]+adj[i][j].second);
        }
    }
    for(int i=1;i<=l;i++) {
        scanf("%d",&arr[i]);
    }
    int p,q;
    scanf("%d %d",&p,&q);
    arr[p]=q;
    dp[1]=0;
    dp0[1]=0;
    pos[1]=arr[1];
    for(int i=2;i<=l;i++) {
        int now=arr[i];
        vector<long long> vec(adj[now].size());
            dijk(arr[i-1],arr[i],0,0);
        for(int j=0;j<adj[now].size();j++) {
            int nt=adj[now][j].first;
            vec[j]=dist[nt]+adj[now][j].second+dp0[i-1];
        }
        dijk(arr[i-1],arr[i],pos[i-1],arr[i-1]);
        for(int j=0;j<adj[now].size();j++) {
            int nt=adj[now][j].first;
            if (nt==arr[i-1]&&arr[i]==pos[i-1]) {
                continue;
            }
            vec[j]=min(vec[j],dist[nt]+adj[now][j].second+dp[i-1]);
        }
        long long mn=INF;
        int ind=now;
        for(int j=0;j<adj[now].size();j++) {
            int nt=adj[now][j].first;
            if (vec[j]<mn) {
                ind=nt;
                mn=vec[j];
            }
        }
        long long mn2=INF;
        for(int j=0;j<adj[now].size();j++) {
            if (adj[now][j].first!=ind) {
                mn2=min(mn2,vec[j]);
            }
        }
        dp[i]=mn;
        dp0[i]=min(mn2,mn+c[now]);
        pos[i]=ind;
        if (mn==mn2) {
            pos[i]=arr[i];
        }
//printf("%lld %lld %d\n",dp[i],dp0[i],ind);
    }
    printf("%lld",dp[l]>=INF?-1:dp[l]);
}

Compilation message

wild_boar.cpp: In function 'void dijk(int, int, int, int)':
wild_boar.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |         for(int i=0;i<adj[now.second].size();i++) {
      |                     ~^~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:60:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |         for(int j=0;j<adj[i].size();j++) {
      |                     ~^~~~~~~~~~~~~~
wild_boar.cpp:79:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         for(int j=0;j<adj[now].size();j++) {
      |                     ~^~~~~~~~~~~~~~~~
wild_boar.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |         for(int j=0;j<adj[now].size();j++) {
      |                     ~^~~~~~~~~~~~~~~~
wild_boar.cpp:93:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         for(int j=0;j<adj[now].size();j++) {
      |                     ~^~~~~~~~~~~~~~~~
wild_boar.cpp:101:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |         for(int j=0;j<adj[now].size();j++) {
      |                     ~^~~~~~~~~~~~~~~~
wild_boar.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     scanf("%d %d %d %d",&n,&m,&t,&l);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |         scanf("%d %d %d",&u,&v,&w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         scanf("%d",&arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~
wild_boar.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     scanf("%d %d",&p,&q);
      |     ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 860 ms 2712 KB Output is correct
28 Correct 810 ms 2700 KB Output is correct
29 Correct 2886 ms 2672 KB Output is correct
30 Correct 2927 ms 2788 KB Output is correct
31 Correct 2350 ms 2724 KB Output is correct
32 Correct 2384 ms 2680 KB Output is correct
33 Incorrect 3461 ms 2880 KB Output isn't correct
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 860 ms 2712 KB Output is correct
28 Correct 810 ms 2700 KB Output is correct
29 Correct 2886 ms 2672 KB Output is correct
30 Correct 2927 ms 2788 KB Output is correct
31 Correct 2350 ms 2724 KB Output is correct
32 Correct 2384 ms 2680 KB Output is correct
33 Incorrect 3461 ms 2880 KB Output isn't correct
34 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 860 ms 2712 KB Output is correct
28 Correct 810 ms 2700 KB Output is correct
29 Correct 2886 ms 2672 KB Output is correct
30 Correct 2927 ms 2788 KB Output is correct
31 Correct 2350 ms 2724 KB Output is correct
32 Correct 2384 ms 2680 KB Output is correct
33 Incorrect 3461 ms 2880 KB Output isn't correct
34 Halted 0 ms 0 KB -