Submission #1040362

# Submission time Handle Problem Language Result Execution time Memory
1040362 2024-08-01T02:24:38 Z Requiem Wild Boar (JOI18_wild_boar) C++17
100 / 100
4072 ms 692008 KB
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define endl '\n'
#define pi 3.14159265359
#define TASKNAME "wildboar"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef pair<ii,ii> iv;
const int MAXN = 2103;
int plan[100005];
int n,m,q,l,u,v;
struct path{
    int s,e,w;
    path(int _s,int _e,int _w){
        s = _s;
        e = _e;
        w = _w;
    }
    path(){
        s = e = w = 0;
    }
};
int f(int x){return (((x-1)^1)+1);}
 
vector<int> g[MAXN];
vector<path> option[MAXN][MAXN];
vector<path> tmp,res;
int from[2*MAXN],to[2*MAXN],w[2*MAXN],dist[2*MAXN],cnt[2*MAXN];
bool visited[MAXN*2];
void eliminate(vector<path>&e){
    res.clear();
    res.shrink_to_fit();
    for(int times=1;times<=5;times++){
        path t = path(0,0,INF);
        for(auto p: e){
            int samestart=0,sameend=0,sameboth=0;
            for(auto cur: res){
                if (p.s == cur.s) samestart++;
                if (p.e == cur.e) sameend++;
                if (p.e == cur.e and p.s == cur.s) sameboth++;
            }
            if (samestart >= 2 or sameend >= 2 or sameboth>=1) continue;
            if (p.w < t.w) t=p;
        }
        res.pb(t);
    }
    e = res;
    e.shrink_to_fit();
}
vector<path> st[400005];
void unite(int node){
    st[node].clear();
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            if (st[node<<1][i].e != st[node<<1|1][j].s){
                st[node].pb(path(st[node<<1][i].s,st[node<<1|1][j].e, st[node<<1][i].w + st[node<<1|1][j].w));
            }
        }
    }
    eliminate(st[node]);
}
void build(int node,int l,int r){
    if (l+1==r){
       st[node] = option[plan[l]][plan[r]];
       return;
    }
    int mid = (l+r)>>1;
    build(node<<1,l,mid);
    build(node<<1|1,mid,r);
    unite(node);
}
void update(int node,int l,int r,int pos,int val){
    if (l+1==r and l==pos){
        plan[pos] = val;
        st[node] = option[plan[l]][plan[r]];
        return;
    }
    if (l+1==r and r==pos){
        plan[pos] = val;
        st[node] = option[plan[l]][plan[r]];
        return;
    }
    int mid = (l+r)>>1;
    if (pos <= mid) update(node<<1,l,mid,pos,val);
    if (pos >= mid) update(node<<1|1,mid,r,pos,val);
    unite(node);
}
main()
{
    fast;
   // freopen(TASKNAME".inp","r",stdin);
   // freopen(TASKNAME".out","w",stdout);
    cin>>n>>m>>q>>l;
    for(int i=1;i<=m;i++){
        cin>>from[i*2]>>to[i*2]>>w[2*i];
        from[i*2-1] = to[i*2];
        to[i*2-1] = from[i*2];
        w[2*i-1] = w[i*2];
        g[from[i*2]].pb(2*i);
        g[to[i*2]].pb(2*i-1);
    }
    for(int i=1;i<=n;i++ ){
        for(auto edgeid: g[i]){
            for(int j=1;j<=2*m;j++){
                dist[j] = INF;
                cnt[j]=0;
                visited[j]=false;
            }
            priority_queue<ii,vector<ii>,greater<ii>> pq;
 
            dist[edgeid] = w[edgeid];
 
            pq.push({dist[edgeid],edgeid});
            while(!pq.empty()){
                int curedge = pq.top().se;
                int du = pq.top().fi;
                int node = to[curedge];
//                cerr<<i<<' '<<du<<' '<<node<<' '<<curedge<<endl;
 
                pq.pop();
 
                if (visited[curedge] or cnt[node] >= 2) continue;
                visited[curedge] = true;
                cnt[node]++;
 
                for(auto nxtedge: g[node]){
                    if (nxtedge == f(curedge)) continue;
                    if (dist[nxtedge] > dist[curedge] + w[nxtedge]){
                        dist[nxtedge] = dist[curedge] + w[nxtedge];
                        pq.push({dist[nxtedge],nxtedge});
                    }
                }
            }
            for(int j=1;j<=2*m;j++){
                option[from[edgeid]][to[j]].pb(path(edgeid,f(j),dist[j]));
            }
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            eliminate(option[i][j]);
//            for(auto x: option[i][j]){
////                cout<<x.s<<' '<<x.e<<' '<<x.w<<endl;
//            }
//            cout<<endl;
        }
    }
    for(int i=1;i<=l;i++){
        cin>>plan[i];
    }
    build(1,1,l);
    for(int i=1;i<=q;i++){
        int p,k;
        cin>>p>>k;
        update(1,1,l,p,k);
        int ans = INF;
        for(auto x: st[1]){
            ans = min(ans,x.w);
        }
        if (ans == INF) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
 
 
}

Compilation message

wild_boar.cpp:101:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  101 | main()
      | ^~~~
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:129:21: warning: unused variable 'du' [-Wunused-variable]
  129 |                 int du = pq.top().fi;
      |                     ^~
# Verdict Execution time Memory Grader output
1 Correct 51 ms 113744 KB Output is correct
2 Correct 45 ms 113756 KB Output is correct
3 Correct 44 ms 113756 KB Output is correct
4 Correct 50 ms 113744 KB Output is correct
5 Correct 45 ms 113756 KB Output is correct
6 Correct 48 ms 113748 KB Output is correct
7 Correct 44 ms 113748 KB Output is correct
8 Correct 44 ms 113684 KB Output is correct
9 Correct 45 ms 113748 KB Output is correct
10 Correct 43 ms 113748 KB Output is correct
11 Correct 43 ms 113756 KB Output is correct
12 Correct 44 ms 113748 KB Output is correct
13 Correct 45 ms 113648 KB Output is correct
14 Correct 42 ms 114008 KB Output is correct
15 Correct 45 ms 113756 KB Output is correct
16 Correct 44 ms 113744 KB Output is correct
17 Correct 47 ms 113748 KB Output is correct
18 Correct 45 ms 113744 KB Output is correct
19 Correct 45 ms 113744 KB Output is correct
20 Correct 45 ms 113752 KB Output is correct
21 Correct 44 ms 113744 KB Output is correct
22 Correct 44 ms 113752 KB Output is correct
23 Correct 46 ms 113752 KB Output is correct
24 Correct 46 ms 113744 KB Output is correct
25 Correct 46 ms 113760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 113744 KB Output is correct
2 Correct 45 ms 113756 KB Output is correct
3 Correct 44 ms 113756 KB Output is correct
4 Correct 50 ms 113744 KB Output is correct
5 Correct 45 ms 113756 KB Output is correct
6 Correct 48 ms 113748 KB Output is correct
7 Correct 44 ms 113748 KB Output is correct
8 Correct 44 ms 113684 KB Output is correct
9 Correct 45 ms 113748 KB Output is correct
10 Correct 43 ms 113748 KB Output is correct
11 Correct 43 ms 113756 KB Output is correct
12 Correct 44 ms 113748 KB Output is correct
13 Correct 45 ms 113648 KB Output is correct
14 Correct 42 ms 114008 KB Output is correct
15 Correct 45 ms 113756 KB Output is correct
16 Correct 44 ms 113744 KB Output is correct
17 Correct 47 ms 113748 KB Output is correct
18 Correct 45 ms 113744 KB Output is correct
19 Correct 45 ms 113744 KB Output is correct
20 Correct 45 ms 113752 KB Output is correct
21 Correct 44 ms 113744 KB Output is correct
22 Correct 44 ms 113752 KB Output is correct
23 Correct 46 ms 113752 KB Output is correct
24 Correct 46 ms 113744 KB Output is correct
25 Correct 46 ms 113760 KB Output is correct
26 Correct 45 ms 113872 KB Output is correct
27 Correct 96 ms 141136 KB Output is correct
28 Correct 93 ms 141136 KB Output is correct
29 Correct 178 ms 141136 KB Output is correct
30 Correct 173 ms 141136 KB Output is correct
31 Correct 160 ms 141128 KB Output is correct
32 Correct 165 ms 141092 KB Output is correct
33 Correct 196 ms 143268 KB Output is correct
34 Correct 182 ms 143052 KB Output is correct
35 Correct 155 ms 142856 KB Output is correct
36 Correct 164 ms 142932 KB Output is correct
37 Correct 180 ms 142932 KB Output is correct
38 Correct 177 ms 145492 KB Output is correct
39 Correct 183 ms 145432 KB Output is correct
40 Correct 182 ms 145492 KB Output is correct
41 Correct 182 ms 145604 KB Output is correct
42 Correct 162 ms 147024 KB Output is correct
43 Correct 178 ms 148816 KB Output is correct
44 Correct 175 ms 148552 KB Output is correct
45 Correct 141 ms 151460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 113744 KB Output is correct
2 Correct 45 ms 113756 KB Output is correct
3 Correct 44 ms 113756 KB Output is correct
4 Correct 50 ms 113744 KB Output is correct
5 Correct 45 ms 113756 KB Output is correct
6 Correct 48 ms 113748 KB Output is correct
7 Correct 44 ms 113748 KB Output is correct
8 Correct 44 ms 113684 KB Output is correct
9 Correct 45 ms 113748 KB Output is correct
10 Correct 43 ms 113748 KB Output is correct
11 Correct 43 ms 113756 KB Output is correct
12 Correct 44 ms 113748 KB Output is correct
13 Correct 45 ms 113648 KB Output is correct
14 Correct 42 ms 114008 KB Output is correct
15 Correct 45 ms 113756 KB Output is correct
16 Correct 44 ms 113744 KB Output is correct
17 Correct 47 ms 113748 KB Output is correct
18 Correct 45 ms 113744 KB Output is correct
19 Correct 45 ms 113744 KB Output is correct
20 Correct 45 ms 113752 KB Output is correct
21 Correct 44 ms 113744 KB Output is correct
22 Correct 44 ms 113752 KB Output is correct
23 Correct 46 ms 113752 KB Output is correct
24 Correct 46 ms 113744 KB Output is correct
25 Correct 46 ms 113760 KB Output is correct
26 Correct 45 ms 113872 KB Output is correct
27 Correct 96 ms 141136 KB Output is correct
28 Correct 93 ms 141136 KB Output is correct
29 Correct 178 ms 141136 KB Output is correct
30 Correct 173 ms 141136 KB Output is correct
31 Correct 160 ms 141128 KB Output is correct
32 Correct 165 ms 141092 KB Output is correct
33 Correct 196 ms 143268 KB Output is correct
34 Correct 182 ms 143052 KB Output is correct
35 Correct 155 ms 142856 KB Output is correct
36 Correct 164 ms 142932 KB Output is correct
37 Correct 180 ms 142932 KB Output is correct
38 Correct 177 ms 145492 KB Output is correct
39 Correct 183 ms 145432 KB Output is correct
40 Correct 182 ms 145492 KB Output is correct
41 Correct 182 ms 145604 KB Output is correct
42 Correct 162 ms 147024 KB Output is correct
43 Correct 178 ms 148816 KB Output is correct
44 Correct 175 ms 148552 KB Output is correct
45 Correct 141 ms 151460 KB Output is correct
46 Correct 186 ms 146432 KB Output is correct
47 Correct 2201 ms 600592 KB Output is correct
48 Correct 2295 ms 608960 KB Output is correct
49 Correct 2444 ms 625560 KB Output is correct
50 Correct 2469 ms 605376 KB Output is correct
51 Correct 2614 ms 605988 KB Output is correct
52 Correct 2823 ms 644428 KB Output is correct
53 Correct 2830 ms 648484 KB Output is correct
54 Correct 2845 ms 641620 KB Output is correct
55 Correct 2818 ms 646224 KB Output is correct
56 Correct 2911 ms 645256 KB Output is correct
57 Correct 2992 ms 644180 KB Output is correct
58 Correct 2983 ms 644228 KB Output is correct
59 Correct 2986 ms 649504 KB Output is correct
60 Correct 3063 ms 643420 KB Output is correct
61 Correct 3094 ms 639592 KB Output is correct
62 Correct 3057 ms 623404 KB Output is correct
63 Correct 2947 ms 605180 KB Output is correct
64 Correct 1926 ms 690772 KB Output is correct
65 Correct 1956 ms 691868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 113744 KB Output is correct
2 Correct 45 ms 113756 KB Output is correct
3 Correct 44 ms 113756 KB Output is correct
4 Correct 50 ms 113744 KB Output is correct
5 Correct 45 ms 113756 KB Output is correct
6 Correct 48 ms 113748 KB Output is correct
7 Correct 44 ms 113748 KB Output is correct
8 Correct 44 ms 113684 KB Output is correct
9 Correct 45 ms 113748 KB Output is correct
10 Correct 43 ms 113748 KB Output is correct
11 Correct 43 ms 113756 KB Output is correct
12 Correct 44 ms 113748 KB Output is correct
13 Correct 45 ms 113648 KB Output is correct
14 Correct 42 ms 114008 KB Output is correct
15 Correct 45 ms 113756 KB Output is correct
16 Correct 44 ms 113744 KB Output is correct
17 Correct 47 ms 113748 KB Output is correct
18 Correct 45 ms 113744 KB Output is correct
19 Correct 45 ms 113744 KB Output is correct
20 Correct 45 ms 113752 KB Output is correct
21 Correct 44 ms 113744 KB Output is correct
22 Correct 44 ms 113752 KB Output is correct
23 Correct 46 ms 113752 KB Output is correct
24 Correct 46 ms 113744 KB Output is correct
25 Correct 46 ms 113760 KB Output is correct
26 Correct 45 ms 113872 KB Output is correct
27 Correct 96 ms 141136 KB Output is correct
28 Correct 93 ms 141136 KB Output is correct
29 Correct 178 ms 141136 KB Output is correct
30 Correct 173 ms 141136 KB Output is correct
31 Correct 160 ms 141128 KB Output is correct
32 Correct 165 ms 141092 KB Output is correct
33 Correct 196 ms 143268 KB Output is correct
34 Correct 182 ms 143052 KB Output is correct
35 Correct 155 ms 142856 KB Output is correct
36 Correct 164 ms 142932 KB Output is correct
37 Correct 180 ms 142932 KB Output is correct
38 Correct 177 ms 145492 KB Output is correct
39 Correct 183 ms 145432 KB Output is correct
40 Correct 182 ms 145492 KB Output is correct
41 Correct 182 ms 145604 KB Output is correct
42 Correct 162 ms 147024 KB Output is correct
43 Correct 178 ms 148816 KB Output is correct
44 Correct 175 ms 148552 KB Output is correct
45 Correct 141 ms 151460 KB Output is correct
46 Correct 186 ms 146432 KB Output is correct
47 Correct 2201 ms 600592 KB Output is correct
48 Correct 2295 ms 608960 KB Output is correct
49 Correct 2444 ms 625560 KB Output is correct
50 Correct 2469 ms 605376 KB Output is correct
51 Correct 2614 ms 605988 KB Output is correct
52 Correct 2823 ms 644428 KB Output is correct
53 Correct 2830 ms 648484 KB Output is correct
54 Correct 2845 ms 641620 KB Output is correct
55 Correct 2818 ms 646224 KB Output is correct
56 Correct 2911 ms 645256 KB Output is correct
57 Correct 2992 ms 644180 KB Output is correct
58 Correct 2983 ms 644228 KB Output is correct
59 Correct 2986 ms 649504 KB Output is correct
60 Correct 3063 ms 643420 KB Output is correct
61 Correct 3094 ms 639592 KB Output is correct
62 Correct 3057 ms 623404 KB Output is correct
63 Correct 2947 ms 605180 KB Output is correct
64 Correct 1926 ms 690772 KB Output is correct
65 Correct 1956 ms 691868 KB Output is correct
66 Correct 133 ms 139860 KB Output is correct
67 Correct 640 ms 250704 KB Output is correct
68 Correct 1875 ms 660228 KB Output is correct
69 Correct 2021 ms 666064 KB Output is correct
70 Correct 2552 ms 685636 KB Output is correct
71 Correct 3269 ms 593376 KB Output is correct
72 Correct 3414 ms 603100 KB Output is correct
73 Correct 3936 ms 645984 KB Output is correct
74 Correct 4072 ms 649720 KB Output is correct
75 Correct 3848 ms 646096 KB Output is correct
76 Correct 3523 ms 622160 KB Output is correct
77 Correct 3420 ms 606380 KB Output is correct
78 Correct 3309 ms 603340 KB Output is correct
79 Correct 3911 ms 649900 KB Output is correct
80 Correct 4006 ms 649300 KB Output is correct
81 Correct 3600 ms 630912 KB Output is correct
82 Correct 4058 ms 649684 KB Output is correct
83 Correct 3910 ms 635224 KB Output is correct
84 Correct 3914 ms 609868 KB Output is correct
85 Correct 2742 ms 692008 KB Output is correct