Submission #647590

# Submission time Handle Problem Language Result Execution time Memory
647590 2022-10-03T09:06:05 Z ToroTN Cities (BOI16_cities) C++14
74 / 100
6000 ms 42256 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define X first
#define Y second
ll n,num,m,city[5],u_i,v_i,w_i,u,pow_2[6],val,dp[100005];
ll d[32][100005],some[32];
vector<pair<ll,ll> > g[100005];
vector<ll> bit[6];
priority_queue<pair<ll,ll> > pq;
void dij(ll arr[])
{
    for(int i=1;i<=n;i++)pq.push({-arr[i],i});
    while(!pq.empty())
    {
        u=pq.top().Y;
        pq.pop();
        for(auto [node,w]:g[u])
        {
            if(arr[u]+w<arr[node])
            {
                arr[node]=arr[u]+w;
                pq.push({-arr[node],node});
            }
        }
    }
}
int main()
{
    pow_2[0]=1;
    for(int i=1;i<=5;i++)pow_2[i]=pow_2[i-1]*2;
    scanf("%lld%lld%lld",&n,&num,&m);
    for(int i=0;i<=31;i++)
    {
        for(int j=1;j<=n;j++)
        {
            d[i][j]=1e18;
        }
    }
    for(int i=0;i<num;i++)scanf("%lld",&city[i]);
    for(int i=1;i<=m;i++)
    {
        scanf("%lld%lld%lld",&u_i,&v_i,&w_i);
        g[u_i].pb({v_i,w_i});
        g[v_i].pb({u_i,w_i});
    }
    for(int i=0;i<num;i++)
    {
        d[pow_2[i]][city[i]]=0;
        dij(d[pow_2[i]]);
    }
    for(int i=0;i<num-1;i++)
    {
        for(int j=i+1;j<num;j++)
        {
            val=(pow_2[i]|pow_2[j]);
            for(int k=1;k<=n;k++)
            {
                d[val][k]=d[pow_2[i]][k]+d[pow_2[j]][k];
            }
            dij(d[val]);
        }
    }
    for(int i=1;i<pow_2[num];i++)
    {
        ll kmp=0;
        for(int j=0;j<num;j++)
        {
            if((i|pow_2[j])==i)++kmp;
        }
        some[i]=kmp;
        bit[kmp].pb(i);
    }
    /*for(int i=1;i<=5;i++)
    {
        for(auto node:bit[i])
        {
            printf("%lld ",node);
        }
        printf("\n");
    }*/
    for(int i=3;i<=num;i++)
    {
        for(int j=1;j<=i/2;j++)
        {
            for(auto k:bit[j])
            {
                for(auto l:bit[i-j])
                {
                    val=(k|l);
                    if(some[val]==some[k]+some[l])
                    {
                        //printf("%lld %lld\n",k,l);
                        for(int c=1;c<=n;c++)
                        {
                            //d[val][c]=d[l][c]+d[k][c];
                            dp[c]=d[l][c]+d[k][c];
                        }
                        if(i!=5)
                        dij(dp);
                        for(int c=1;c<=n;c++)d[val][c]=min(d[val][c],dp[c]);
                    }
                }
            }
        }
    }
    ll ans=1e18;
    for(int i=1;i<=n;i++)
    {
        ans=min(ans,d[pow_2[num]-1][i]);
    }
    printf("%lld\n",ans);
}

Compilation message

cities.cpp: In function 'void dij(long long int*)':
cities.cpp:19:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   19 |         for(auto [node,w]:g[u])
      |                  ^
cities.cpp: In function 'int main()':
cities.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     scanf("%lld%lld%lld",&n,&num,&m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:41:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     for(int i=0;i<num;i++)scanf("%lld",&city[i]);
      |                           ~~~~~^~~~~~~~~~~~~~~~~
cities.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |         scanf("%lld%lld%lld",&u_i,&v_i,&w_i);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2832 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 885 ms 42256 KB Output is correct
2 Correct 786 ms 41272 KB Output is correct
3 Correct 433 ms 35256 KB Output is correct
4 Correct 82 ms 12116 KB Output is correct
5 Correct 348 ms 42184 KB Output is correct
6 Correct 70 ms 11988 KB Output is correct
7 Correct 7 ms 3156 KB Output is correct
8 Correct 4 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 3248 KB Output is correct
2 Correct 11 ms 3256 KB Output is correct
3 Correct 8 ms 3156 KB Output is correct
4 Correct 8 ms 3132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2439 ms 42176 KB Output is correct
2 Correct 2429 ms 41320 KB Output is correct
3 Correct 1903 ms 35368 KB Output is correct
4 Correct 1380 ms 28096 KB Output is correct
5 Correct 296 ms 15420 KB Output is correct
6 Correct 117 ms 14244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 6059 ms 42172 KB Time limit exceeded
2 Halted 0 ms 0 KB -