QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#502352#125. Wild BoarRafi220 1ms3556kbC++204.5kb2024-08-03 04:36:202024-08-03 04:36:20

Judging History

你现在查看的是最新测评结果

  • [2024-08-03 04:36:20]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3556kb
  • [2024-08-03 04:36:20]
  • 提交

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=37,pot=1<<6;
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,b);
	add(a,-1);
	add(a,-1);
	add(-1,b);
	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) 
			{
				T[i][j].pb({{a,b},mn});
				xd.pb({a,b});
			}
			auto add = [&](int xa,int ya) 
			{
				mn=infl;
				int x,y;
				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,b);
			add(a,-1);
			add(a,-1);
			add(-1,b);
			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;
}



Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 12
Accepted
time: 1ms
memory: 3556kb

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: 0
Wrong Answer
time: 1ms
memory: 3520kb

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:

75

result:

wrong answer 1st lines differ - expected: '64', found: '75'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%