Submission #696201

# Submission time Handle Problem Language Result Execution time Memory
696201 2023-02-05T20:05:44 Z whitedragon Race (IOI11_race) C++17
43 / 100
3000 ms 46388 KB
#include "race.h"
#include<iostream>
#include<vector>
 
using namespace std;
 
#define ll long long
 
ll INF;
ll min_cost;
 
struct vertex
{
    int y;
    ll cost;
};
 
void dfs_1(int x, vector<int>& visited, vector<vector<vertex>>& con, 
vector<int>& sub_tree, vector<int>& father, vector<int>& dead_ver)
{
    //cout<<"#"<<x<<endl;
    visited[x]=1;
 
    for(int i=0;i<con[x].size();i++)
    {
        int y=con[x][i].y;
        //int cost=con[x][i].cost;
 
        if(dead_ver[y]!=1 && visited[y]==0)
        {
            father[y]=x;
            dfs_1(y,visited,con,sub_tree,father,dead_ver);
            sub_tree[x]+=sub_tree[y]+1;
        }
    }
 
    //cout<<"#"<<x<<" "<<sub_tree[x]<<endl;
}
 
int find_centr(int x, vector<vector<vertex>>& con, vector<int>& sub_tree, 
vector<int>& father, vector<int>& dead_ver, int tree_size)
{
    for(int i=0;i<con[x].size();i++)
    {
        int y=con[x][i].y;
 
        if(dead_ver[y]!=1 && father[x]!=y && sub_tree[y]+1>(tree_size/2))
        {
            
            //cout<<"^"<<y<<" "<<sub_tree[y]+1<<" "<<tree_size<<endl;
            return find_centr(y,con,sub_tree,father,dead_ver,tree_size);
        }
    }
 
    return x;
}
 
void dfs_2(int x, vector<vector<vertex>>& con, vector<int>& visited, ll k,
vector<int>& dead_ver, vector<ll>& lvl, vector<ll>& paths_of_length, vector<ll>& cost_of_way)
{
    visited[x]=1;
    //cout<<"&&"<<x<<endl;
    if(cost_of_way[x]<=k)
    {
        //caly koszt to najmniejszy dojsuca wczesniej + moj
        min_cost=min(min_cost,lvl[x]+paths_of_length[k-cost_of_way[x]]);
        //cout<<"cost kannnnnnnnn "<<lvl[x]<<" "<<paths_of_length[k-cost_of_way[x]]<<endl;
    }
 
    for(int i=0;i<con[x].size();i++)
    {
        int y=con[x][i].y;
        ll cost=con[x][i].cost;
 
        if(dead_ver[y]!=1 && visited[y]==0)
        {
            lvl[y]=lvl[x]+1;
            cost_of_way[y]=cost_of_way[x]+cost;
            dfs_2(y,con,visited,k,dead_ver,lvl,paths_of_length,cost_of_way);
        }
    }
}
 
void dfs_3(int x, vector<vector<vertex>>& con, vector<int>& visited, vector<ll>& lvl,
vector<ll>& cost_of_way, vector<ll>& paths_of_length, vector<int>& dead_ver, ll k)
{
    visited[x]=1;
 
    if(cost_of_way[x]<=k)
    {
        paths_of_length[cost_of_way[x]]=min(paths_of_length[cost_of_way[x]],lvl[x]);
    }
 
    for(int i=0;i<con[x].size();i++)
    {
        int y=con[x][i].y;
 
        if(dead_ver[y]!=1 && visited[y]==0)
        {
            dfs_3(y,con,visited,lvl,cost_of_way,paths_of_length,dead_ver,k);
        }
    }
}
 
void count_from_centr(int x, vector<int>& visited_1, vector<int>& visited_2,
vector<vector<vertex>>& con,vector<int>& dead_ver, vector<ll>& lvl, int k,
 vector<ll>& cost_of_way)
{
    vector<ll> paths_of_length(k+1,INF);
    //vector<ll> cost_of_way(k+1,INF);
    paths_of_length[0]=0;
    lvl[x]=0;
    cost_of_way[x]=0;
    visited_1[x]=1;
    visited_2[x]=1;
 
    for(int i=0;i<con[x].size();i++)
    {
        int y=con[x][i].y;
        ll cost=con[x][i].cost;
 
        if(dead_ver[y]!=1 && visited_1[y]==0)
        {
            lvl[y]=1;
            cost_of_way[y]=cost;
            //cout<<"&"<<y<<endl;
            //dfs2
            dfs_2(y,con,visited_1,k,dead_ver,lvl,paths_of_length,cost_of_way);
            //dfs3
            dfs_3(y,con,visited_2,lvl,cost_of_way,paths_of_length,dead_ver,k);
        }
    }
}
 
int best_path(int N, int K, int H[][2], int L[])
{
    int n=N;
    ll k=K;

    min_cost=n+1;
    INF=n+1;
 
    vector<vector<vertex>> con(n+1);
 
    for(int i=0;i<n-1;i++)
    {
        int a=H[i][0];
        int b=H[i][1];
 
        a++;
        b++;
 
        //cout<<a<<" "<<b<<" "<<L[i]<<endl;
 
        vertex A;
        A.y=b;
        A.cost=L[i];
 
        con[a].push_back(A);
 
        vertex B;
        B.y=a;
        B.cost=L[i];
 
        con[b].push_back(B);
    }
 
    vector<int> dead_ver(n+1,0);
    int dead_count=0;
 
    while (dead_count!=n)
    {
        vector<int> visited(n+1,0);
        vector<int> sub_tree(n+1,0);
        vector<int> father(n+1);
        vector<ll> lvl(n+1);
        vector<int> visited_1(n+1,0);
        vector<int> visited_2(n+1,0);
        vector<ll> cost_of_way(n+1,INF);
 
        //cout<<"turnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn"<<endl;
 
        for(int x=1;x<=n;x++)
        {
            if(visited[x]!=0)
            {
                continue;
            }
 
            if(dead_ver[x]==1)
            {
                continue;
            }
 
            father[x]=x;
            //cout<<"dfsssssssssssssssssssssssssss"<<endl;
            dfs_1(x,visited,con,sub_tree,father,dead_ver);
 
            int cen_id=find_centr(x,con,sub_tree,father,dead_ver,sub_tree[x]+1);
            //cout<<"centrrr "<<cen_id<<endl;
            count_from_centr(cen_id,visited_1,visited_2,con,dead_ver,lvl,k,cost_of_way);
 
            //remove
            dead_ver[cen_id]=1;
            dead_count++;
        }
    }
 
    if(min_cost==INF)
    {
        return -1;
    }
 
    return min_cost;
    
}

Compilation message

race.cpp: In function 'void dfs_1(int, std::vector<int>&, std::vector<std::vector<vertex> >&, std::vector<int>&, std::vector<int>&, std::vector<int>&)':
race.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<vertex>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i=0;i<con[x].size();i++)
      |                 ~^~~~~~~~~~~~~~
race.cpp: In function 'int find_centr(int, std::vector<std::vector<vertex> >&, std::vector<int>&, std::vector<int>&, std::vector<int>&, int)':
race.cpp:43:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<vertex>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |     for(int i=0;i<con[x].size();i++)
      |                 ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs_2(int, std::vector<std::vector<vertex> >&, std::vector<int>&, long long int, std::vector<int>&, std::vector<long long int>&, std::vector<long long int>&, std::vector<long long int>&)':
race.cpp:70:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<vertex>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i=0;i<con[x].size();i++)
      |                 ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs_3(int, std::vector<std::vector<vertex> >&, std::vector<int>&, std::vector<long long int>&, std::vector<long long int>&, std::vector<long long int>&, std::vector<int>&, long long int)':
race.cpp:94:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<vertex>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |     for(int i=0;i<con[x].size();i++)
      |                 ~^~~~~~~~~~~~~~
race.cpp: In function 'void count_from_centr(int, std::vector<int>&, std::vector<int>&, std::vector<std::vector<vertex> >&, std::vector<int>&, std::vector<long long int>&, int, std::vector<long long int>&)':
race.cpp:117:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<vertex>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |     for(int i=0;i<con[x].size();i++)
      |                 ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 304 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 438 ms 7636 KB Output is correct
23 Correct 295 ms 6356 KB Output is correct
24 Correct 359 ms 7196 KB Output is correct
25 Correct 336 ms 6996 KB Output is correct
26 Correct 115 ms 3076 KB Output is correct
27 Correct 369 ms 6748 KB Output is correct
28 Correct 70 ms 2004 KB Output is correct
29 Correct 113 ms 2920 KB Output is correct
30 Correct 152 ms 3304 KB Output is correct
31 Correct 234 ms 5576 KB Output is correct
32 Correct 258 ms 5988 KB Output is correct
33 Correct 323 ms 6560 KB Output is correct
34 Correct 204 ms 5076 KB Output is correct
35 Correct 310 ms 6796 KB Output is correct
36 Correct 387 ms 7724 KB Output is correct
37 Correct 245 ms 6664 KB Output is correct
38 Correct 169 ms 4436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 338 ms 13940 KB Output is correct
20 Correct 320 ms 14208 KB Output is correct
21 Correct 318 ms 14364 KB Output is correct
22 Correct 282 ms 14276 KB Output is correct
23 Correct 306 ms 14680 KB Output is correct
24 Correct 125 ms 13856 KB Output is correct
25 Correct 360 ms 18748 KB Output is correct
26 Correct 152 ms 23204 KB Output is correct
27 Correct 296 ms 28768 KB Output is correct
28 Correct 976 ms 46388 KB Output is correct
29 Correct 1074 ms 45000 KB Output is correct
30 Correct 288 ms 28820 KB Output is correct
31 Correct 284 ms 28776 KB Output is correct
32 Correct 524 ms 28916 KB Output is correct
33 Correct 834 ms 27636 KB Output is correct
34 Correct 778 ms 28620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 312 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 304 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 438 ms 7636 KB Output is correct
23 Correct 295 ms 6356 KB Output is correct
24 Correct 359 ms 7196 KB Output is correct
25 Correct 336 ms 6996 KB Output is correct
26 Correct 115 ms 3076 KB Output is correct
27 Correct 369 ms 6748 KB Output is correct
28 Correct 70 ms 2004 KB Output is correct
29 Correct 113 ms 2920 KB Output is correct
30 Correct 152 ms 3304 KB Output is correct
31 Correct 234 ms 5576 KB Output is correct
32 Correct 258 ms 5988 KB Output is correct
33 Correct 323 ms 6560 KB Output is correct
34 Correct 204 ms 5076 KB Output is correct
35 Correct 310 ms 6796 KB Output is correct
36 Correct 387 ms 7724 KB Output is correct
37 Correct 245 ms 6664 KB Output is correct
38 Correct 169 ms 4436 KB Output is correct
39 Correct 338 ms 13940 KB Output is correct
40 Correct 320 ms 14208 KB Output is correct
41 Correct 318 ms 14364 KB Output is correct
42 Correct 282 ms 14276 KB Output is correct
43 Correct 306 ms 14680 KB Output is correct
44 Correct 125 ms 13856 KB Output is correct
45 Correct 360 ms 18748 KB Output is correct
46 Correct 152 ms 23204 KB Output is correct
47 Correct 296 ms 28768 KB Output is correct
48 Correct 976 ms 46388 KB Output is correct
49 Correct 1074 ms 45000 KB Output is correct
50 Correct 288 ms 28820 KB Output is correct
51 Correct 284 ms 28776 KB Output is correct
52 Correct 524 ms 28916 KB Output is correct
53 Correct 834 ms 27636 KB Output is correct
54 Correct 778 ms 28620 KB Output is correct
55 Correct 29 ms 1736 KB Output is correct
56 Correct 13 ms 1692 KB Output is correct
57 Correct 134 ms 14560 KB Output is correct
58 Correct 44 ms 13460 KB Output is correct
59 Execution timed out 3079 ms 24740 KB Time limit exceeded
60 Halted 0 ms 0 KB -