QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#502362 | #125. Wild Boar | Rafi22 | 12 | 1ms | 3956kb | C++20 | 4.7kb | 2024-08-03 04:47:39 | 2024-08-03 04:47:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
int inf=1000000007;
ll infl=1000000000000000007;
ll mod=1000000007;
const int N=107,pot=1<<10;
vector<pair<pair<int,int>,ll>>seg[2*pot+7];
bool is[2*pot+7];
void upd(int v)
{
seg[v].clear();
if(!is[2*v+1])
{
seg[v]=seg[2*v];
return ;
}
int a,b;
ll mn=infl;
vector<pair<int,int>>xd;
for(auto [pl,cl]:seg[2*v])
{
for(auto [pr,cr]:seg[2*v+1])
{
if(pl.nd!=pr.st&&cl+cr<mn)
{
mn=cl+cr;
a=pl.st;
b=pr.nd;
}
}
}
if(mn!=infl)
{
seg[v].pb({{a,b},mn});
xd.pb({a,b});
}
auto add=[&](int xa,int ya)
{
int x,y;
mn=infl;
for(auto [pl,cl]:seg[2*v])
{
for(auto [pr,cr]:seg[2*v+1])
{
bool nie=0;
for(auto [xx,yy]:xd) if(pl.st==xx&&pr.nd==yy) nie=1;
if(nie) continue;
if(pl.st!=xa&&pr.nd!=ya&&pl.nd!=pr.st&&cl+cr<mn)
{
mn=cl+cr;
x=pl.st;
y=pr.nd;
}
}
}
if(mn!=infl)
{
seg[v].pb({{x,y},mn});
xd.pb({x,y});
}
};
add(a,b);
add(a,-1);
add(-1,b);
}
vector<pair<pair<int,int>,ll>>T[N][N];
ll g[N][N];
vector<pair<int,ll>>G[N];
int U[2*N],V[2*N],C[2*N];
int X[pot];
ll dis[N][N];
ll D[2*N];
vector<pair<ll,int>>W[N][N];
bool odw[2*N];
void ins(int v)
{
v+=pot-1;
while(v>1)
{
v/=2;
upd(v);
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,m,q,L;
cin>>n>>m>>q>>L;
FOR(i,1,m)
{
cin>>V[i]>>U[i]>>C[i];
V[i+m]=U[i];
U[i+m]=V[i];
C[i+m]=C[i];
g[U[i]][V[i]]=C[i];
g[V[i]][U[i]]=C[i];
G[V[i]].pb({U[i],i});
G[V[i+m]].pb({U[i+m],i+m});
}
FOR(i,1,n)
{
for(auto [j,t]:G[i])
{
FOR(l,1,2*m) D[l]=infl;
memset(odw,0,sizeof odw);
D[t]=0;
priority_queue<pair<ll,int>>Q;
Q.push({0,t});
while(sz(Q)>0)
{
int v=Q.top().nd;
Q.pop();
if(odw[v]) continue;
odw[v]=1;
W[j][U[v]].pb({D[v],V[v]});
for(auto [u,tu]:G[U[v]])
{
if(abs(v-tu)==m) continue;
if(D[v]+C[tu]<D[tu])
{
D[tu]=D[v]+C[tu];
Q.push({-D[tu],tu});
}
}
}
FOR(l,1,n) sort(all(W[j][l]));
}
FOR(j,1,n)
{
if(i==j) continue;
int a,b;
ll mn=infl;
if(g[i][j])
{
a=j,b=i;
mn=g[i][j];
}
vector<pair<int,int>>xd;
for(auto [A,ta]:G[i])
{
for(auto [B,tb]:G[j])
{
dis[A][B]=infl;
if(sz(W[A][B])>0&&W[A][B][0].nd!=j) dis[A][B]=W[A][B][0].st;
else if(sz(W[A][B])>1) dis[A][B]=W[A][B][1].st;
debug(i,j,A,B,dis[A][B]);
if(C[ta]+dis[A][B]+C[tb]<mn)
{
mn=C[ta]+dis[A][B]+C[tb];
a=A;
b=B;
}
}
}
if(mn!=infl)
{
debug(i,j,mn);
T[i][j].pb({{a,b},mn});
xd.pb({a,b});
}
auto add = [&](int xa,int ya)
{
mn=infl;
int x,y;
if(g[i][j]&&j!=xa&&i!=ya)
{
bool nie=0;
for(auto [xx,yy]:xd) if(j==xx&&i==yy) nie=1;
if(!nie)
{
mn=g[i][j];
x=j;
y=i;
}
}
for(auto [A,ta]:G[i])
{
if(A==xa) continue;
for(auto [B,tb]:G[j])
{
if(B==ya) continue;
bool nie=0;
for(auto [xx,yy]:xd) if(A==xx&&B==yy) nie=1;
if(nie) continue;
if(C[ta]+dis[A][B]+C[tb]<mn)
{
mn=C[ta]+dis[A][B]+C[tb];
x=A;
y=B;
}
}
}
if(mn!=infl)
{
T[i][j].pb({{x,y},mn});
xd.pb({x,y});
}
};
add(a,b);
add(a,-1);
add(-1,b);
}
for(auto [j,t]:G[i]) FOR(l,1,n) W[j][l].clear();
}
FOR(i,1,L) cin>>X[i];
FOR(i,1,L-1)
{
is[i+pot-1]=1;
seg[i+pot-1]=T[X[i]][X[i+1]];
}
ROF(i,pot-1,1)
{
is[i]=is[2*i];
upd(i);
}
while(q--)
{
int i;
cin>>i>>X[i];
if(i>1)
{
seg[i-1+pot-1]=T[X[i-1]][X[i]];
ins(i-1);
}
if(i<L)
{
seg[i+pot-1]=T[X[i]][X[i+1]];
ins(i);
}
FOR(i,1,L-1) debug(X[i],X[i+1],seg[i+pot-1]);
ll ans=infl;
for(auto [p,x]:seg[1]) ans=min(ans,x);
if(ans==infl) cout<<-1<<endl;
else cout<<ans<<endl;
}
return 0;
}
详细
Subtask #1:
score: 12
Accepted
Test #1:
score: 12
Accepted
time: 1ms
memory: 3704kb
input:
5 5 1 10 1 4 10 2 4 2 2 5 4 3 5 4 4 5 7 2 4 5 3 5 3 5 4 2 1 10 1
output:
-1
result:
ok single line: '-1'
Test #2:
score: 12
Accepted
time: 1ms
memory: 3620kb
input:
5 10 1 10 1 2 8 1 3 5 1 4 2 1 5 1 2 3 7 2 4 9 2 5 6 3 4 10 3 5 10 4 5 6 4 2 4 3 5 3 1 4 3 5 3 5
output:
64
result:
ok single line: '64'
Test #3:
score: 12
Accepted
time: 1ms
memory: 3688kb
input:
5 10 1 10 1 2 8 1 3 4 1 4 2 1 5 10 2 3 10 2 4 10 2 5 6 3 4 10 3 5 2 4 5 10 5 1 2 1 4 5 1 5 3 1 8 4
output:
60
result:
ok single line: '60'
Test #4:
score: 12
Accepted
time: 1ms
memory: 3920kb
input:
6 10 1 10 1 3 3 1 4 5 1 5 9 1 6 10 2 3 4 2 5 5 2 6 1 3 6 4 4 6 6 5 6 8 4 3 6 4 1 3 6 4 3 5 1 1
output:
48
result:
ok single line: '48'
Test #5:
score: 12
Accepted
time: 1ms
memory: 3636kb
input:
6 10 1 10 1 2 10 1 3 9 1 5 6 1 6 9 2 4 3 2 5 4 2 6 9 3 5 2 4 5 6 4 6 8 4 6 2 3 2 3 1 3 5 4 9 1
output:
86
result:
ok single line: '86'
Test #6:
score: 12
Accepted
time: 1ms
memory: 3732kb
input:
6 10 1 10 1 2 7 1 4 10 1 5 9 1 6 9 2 3 7 2 4 3 3 4 1 3 5 4 4 6 2 5 6 3 6 3 1 3 6 4 6 4 3 1 5 2
output:
61
result:
ok single line: '61'
Test #7:
score: 12
Accepted
time: 1ms
memory: 3720kb
input:
7 10 1 10 1 2 4 1 3 8 1 5 10 1 7 5 2 5 5 2 7 4 3 4 8 3 6 9 4 7 6 5 6 9 2 1 2 1 2 1 2 1 2 1 9 2
output:
56
result:
ok single line: '56'
Test #8:
score: 12
Accepted
time: 1ms
memory: 3856kb
input:
7 10 1 10 1 4 8 1 5 9 1 6 5 1 7 5 2 4 1 2 6 9 2 7 6 3 5 5 3 7 2 5 6 7 6 5 3 4 6 3 6 4 7 4 4 4
output:
88
result:
ok single line: '88'
Test #9:
score: 12
Accepted
time: 0ms
memory: 3708kb
input:
7 10 1 10 1 3 1 1 4 5 1 5 8 2 3 4 2 7 9 3 4 8 3 7 5 4 5 1 5 6 8 6 7 2 6 5 2 5 6 5 6 5 7 6 4 3
output:
84
result:
ok single line: '84'
Test #10:
score: 12
Accepted
time: 1ms
memory: 3700kb
input:
7 10 1 10 1 3 4 1 4 3 1 5 7 1 6 5 1 7 2 2 3 8 2 4 6 4 7 7 5 6 10 6 7 4 3 2 3 1 3 1 3 1 3 2 9 3
output:
90
result:
ok single line: '90'
Test #11:
score: 12
Accepted
time: 1ms
memory: 3708kb
input:
8 10 1 10 1 2 4 1 7 4 1 8 7 2 3 7 2 4 2 3 7 5 3 8 8 4 5 5 5 6 5 6 8 3 3 5 7 5 6 3 5 3 6 4 9 2
output:
105
result:
ok single line: '105'
Test #12:
score: 12
Accepted
time: 1ms
memory: 3652kb
input:
8 10 1 10 1 3 6 1 7 3 1 8 1 2 3 10 2 4 6 2 8 9 3 5 3 4 6 5 5 7 7 6 8 7 5 2 6 8 4 5 2 5 3 6 10 7
output:
114
result:
ok single line: '114'
Test #13:
score: 12
Accepted
time: 1ms
memory: 3652kb
input:
8 10 1 10 1 2 6 1 3 9 1 6 7 2 8 6 3 5 10 4 6 4 4 7 1 4 8 8 5 7 7 6 8 9 2 8 5 6 2 3 5 6 4 8 3 5
output:
125
result:
ok single line: '125'
Test #14:
score: 12
Accepted
time: 1ms
memory: 3932kb
input:
8 10 1 10 1 2 5 1 5 8 1 8 3 2 6 5 2 7 8 3 4 5 3 8 4 4 6 4 5 6 1 5 7 5 7 4 5 1 6 4 7 4 8 6 10 1
output:
99
result:
ok single line: '99'
Test #15:
score: 12
Accepted
time: 1ms
memory: 3956kb
input:
9 10 1 10 1 3 1 1 6 6 1 9 6 2 4 5 2 9 3 3 5 4 4 5 10 6 7 6 7 8 8 8 9 6 3 5 2 6 2 8 5 1 3 7 2 8
output:
189
result:
ok single line: '189'
Test #16:
score: 12
Accepted
time: 0ms
memory: 3716kb
input:
9 10 1 10 1 8 9 1 9 5 2 6 8 2 7 10 3 5 8 3 7 5 4 5 3 4 8 4 5 9 7 6 9 9 4 7 5 2 7 5 8 4 6 1 7 2
output:
217
result:
ok single line: '217'
Test #17:
score: 12
Accepted
time: 1ms
memory: 3940kb
input:
9 10 1 10 1 4 8 1 9 10 2 3 3 2 5 10 3 9 1 4 8 5 5 7 6 6 7 2 6 8 7 8 9 10 2 9 6 4 5 8 6 3 9 3 4 7
output:
116
result:
ok single line: '116'
Test #18:
score: 12
Accepted
time: 1ms
memory: 3604kb
input:
9 10 1 10 1 3 6 1 4 1 2 4 1 2 6 7 3 4 6 3 9 1 5 6 2 5 8 6 7 8 8 7 9 9 1 6 7 6 9 7 1 5 9 3 7 6
output:
155
result:
ok single line: '155'
Test #19:
score: 12
Accepted
time: 1ms
memory: 3956kb
input:
10 10 1 10 1 3 6 1 9 5 2 4 4 2 6 2 3 4 10 5 8 1 5 10 7 6 7 8 7 10 2 8 9 8 10 3 10 9 1 2 8 4 7 4 2 3
output:
196
result:
ok single line: '196'
Test #20:
score: 12
Accepted
time: 1ms
memory: 3656kb
input:
10 10 1 10 1 4 5 1 8 6 2 8 9 2 9 7 3 9 4 3 10 3 4 7 5 5 6 10 5 10 10 6 7 8 9 5 4 6 5 8 2 8 9 8 3 9
output:
284
result:
ok single line: '284'
Test #21:
score: 12
Accepted
time: 1ms
memory: 3704kb
input:
5 10 1 10 1 2 1 1 3 7 1 4 6 1 5 10 2 3 2 2 4 4 2 5 5 3 4 1 3 5 10 4 5 3 2 1 5 1 4 2 5 4 1 5 7 5
output:
45
result:
ok single line: '45'
Test #22:
score: 12
Accepted
time: 1ms
memory: 3916kb
input:
5 10 1 10 1 2 4 1 3 10 1 4 5 1 5 6 2 3 2 2 4 1 2 5 1 3 4 9 3 5 10 4 5 10 4 2 3 1 4 3 1 3 2 3 10 1
output:
49
result:
ok single line: '49'
Test #23:
score: 12
Accepted
time: 1ms
memory: 3692kb
input:
5 10 1 10 1 2 10 1 3 8 1 4 2 1 5 10 2 3 7 2 4 2 2 5 4 3 4 3 3 5 7 4 5 1 3 1 2 4 5 4 3 2 5 2 10 2
output:
41
result:
ok single line: '41'
Test #24:
score: 12
Accepted
time: 1ms
memory: 3904kb
input:
5 10 1 10 1 2 7 1 3 7 1 4 4 1 5 8 2 3 6 2 4 10 2 5 1 3 4 9 3 5 4 4 5 6 1 4 3 1 2 4 2 1 2 4 1 2
output:
70
result:
ok single line: '70'
Test #25:
score: 12
Accepted
time: 1ms
memory: 3696kb
input:
5 10 1 10 1 2 10 1 3 8 1 4 9 1 5 3 2 3 3 2 4 3 2 5 10 3 4 1 3 5 1 4 5 7 4 3 1 3 4 2 4 5 2 3 9 2
output:
36
result:
ok single line: '36'
Test #26:
score: 12
Accepted
time: 1ms
memory: 3620kb
input:
5 10 1 10 1 2 5 1 3 7 1 4 2 1 5 6 2 3 5 2 4 2 2 5 8 3 4 8 3 5 3 4 5 1 3 2 5 4 1 2 4 3 1 2 2 2
output:
38
result:
ok single line: '38'
Subtask #2:
score: 0
Runtime Error
Dependency #1:
100%
Accepted
Test #27:
score: 35
Accepted
time: 1ms
memory: 3876kb
input:
20 40 1 100 1 9 61 1 17 2 1 20 2 2 8 35 2 16 11 2 17 4 3 5 19 3 18 52 4 5 4 4 6 89 4 11 52 5 6 59 5 10 77 5 12 24 5 13 82 5 16 55 5 18 25 6 7 98 6 9 59 6 15 37 7 10 42 7 17 71 8 10 37 8 19 31 9 10 74 9 14 25 9 15 70 9 18 39 10 14 1 11 20 46 12 17 48 12 20 36 13 17 32 13 20 77 14 18 73 15 16 50 15 17...
output:
8152
result:
ok single line: '8152'
Test #28:
score: 0
Runtime Error
input:
100 100 1 100000 1 8 471117256 1 62 130027795 1 73 656848900 2 37 768343489 2 79 60152578 3 39 730756132 3 91 742436424 4 14 127206074 4 26 49752186 4 37 277130591 5 39 267794148 5 67 795814790 6 26 322818374 7 9 528203057 7 22 321028016 7 74 603723430 8 48 173910965 9 56 146180110 9 84 468273541 9 ...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%