답안 #584350

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

int n,m,t,l;
typedef pair<long long,long long> 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];
long long d[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+1;i++) {
        vis[i]=false;
        dist[i]=INF;
    }
    dist[st]=0;
    while (!pq.empty()) {
        P now=pq.top();
        pq.pop();
        if (vis[now.second]) {
            continue;
        }
//printf("..%d\n",now.second);
        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));
//printf("%d %lld %d %lld %lld\n",now.second,dist[nt],nt,dist[now.second],adj[now.second][i].second);
            }
        }
    }
}

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<=n;i++) {
        adj[n+1].push_back(P(i,c[i]));
for(int j=0;j<adj[i].size();j++) {
adj[i][j].second*=2;
}
    }
    dijk(n+1,0,0,0);
    for(int i=1;i<=n;i++) {
        d[i]=dist[i];
for(int j=0;j<adj[i].size();j++) adj[i][j].second/=2;
    }
    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];
        if (i!=l&&adj[now].size()==1) {
            printf("-1");
            return 0;
        }
        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+d[now]);
        pos[i]=ind;
//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:36:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |         for(int i=0;i<adj[now.second].size();i++) {
      |                     ~^~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:63:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for(int j=0;j<adj[i].size();j++) {
      |                     ~^~~~~~~~~~~~~~
wild_boar.cpp:71:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 | for(int j=0;j<adj[i].size();j++) {
      |             ~^~~~~~~~~~~~~~
wild_boar.cpp:78:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 | for(int j=0;j<adj[i].size();j++) adj[i][j].second/=2;
      |             ~^~~~~~~~~~~~~~
wild_boar.cpp:97:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |         for(int j=0;j<adj[now].size();j++) {
      |                     ~^~~~~~~~~~~~~~~~
wild_boar.cpp:102:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         for(int j=0;j<adj[now].size();j++) {
      |                     ~^~~~~~~~~~~~~~~~
wild_boar.cpp:111:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         for(int j=0;j<adj[now].size();j++) {
      |                     ~^~~~~~~~~~~~~~~~
wild_boar.cpp:119:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |         for(int j=0;j<adj[now].size();j++) {
      |                     ~^~~~~~~~~~~~~~~~
wild_boar.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |     scanf("%d %d %d %d",&n,&m,&t,&l);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:57:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |         scanf("%d %d %d",&u,&v,&w);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |         scanf("%d",&arr[i]);
      |         ~~~~~^~~~~~~~~~~~~~
wild_boar.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   84 |     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 0 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 1 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 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 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 1 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 0 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 1 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 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 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 1 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 8 ms 724 KB Output is correct
28 Correct 10 ms 724 KB Output is correct
29 Incorrect 2809 ms 2680 KB Output isn't correct
30 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 0 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 1 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 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 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 1 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 8 ms 724 KB Output is correct
28 Correct 10 ms 724 KB Output is correct
29 Incorrect 2809 ms 2680 KB Output isn't correct
30 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 0 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 1 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 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 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 1 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 8 ms 724 KB Output is correct
28 Correct 10 ms 724 KB Output is correct
29 Incorrect 2809 ms 2680 KB Output isn't correct
30 Halted 0 ms 0 KB -