QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#745#487619#8824. Slay the Spireucup-team3091w4p3rFailed.2024-07-27 16:58:312024-07-27 16:58:31

Details

Extra Test:

Invalid Input

input:

1
5 3 0 2
1 6 2
2 2 1
3 6 1
1 1 3
3 2 2

output:


result:

FAIL Integer parameter [name=k] equals to 0, violates the range [1, 500] (test case 1, stdin, line 2)

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#487619#8824. Slay the Spirew4p3rAC ✓2ms3928kbC++201.6kb2024-07-23 02:08:402024-07-23 02:08:41

answer

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		int n,m,k,s;
		cin>>n>>m>>k>>s;
		vector<vector<pair<int,int>>> out(m+5);
		vector<int> in(m+5),pa(m+5),sumdeg(m+5),maxdel(m+5,0),minn(m+5,inf);
		function<int(int)> find=[&](int x){return pa[x]?pa[x]=find(pa[x]):x;};
		vector<tuple<int,int,int>> edges;
		long long ans=0;
		for(int i=1;i<=n;i++)
		{
			int u,v,w;
			cin>>u>>w>>v;
			ans+=w;
			edges.emplace_back(u,v,w);
			out[u].emplace_back(w,v);
			in[v]++;
		}
		for(int i=1;i<=k;i++)
		{
			int x;
			cin>>x;
			in[x]++;
		}
		in[s]++;
		for(int i=1;i<=m;i++)
		{
			sort(out[i].begin(),out[i].end(),greater<pair<int,int>>());
			while((int)out[i].size()>in[i])
			{
//				cerr<<"del "<<i<<' '<<out[i].back().second<<' '<<out[i].back().first<<endl;
				ans-=out[i].back().first;
				maxdel[i]=max(maxdel[i],out[i].back().first);
				out[i].pop_back();
			}
		}
		for(int u=1;u<=m;u++)
		{
			for(auto [w,v]:out[u])
			{
				int pu=find(u),pv=find(v);
				if(pu!=pv)
					pa[pv]=pu;
			}
		}
		for(int u=1;u<=m;u++)
		{
			int pu=find(u);
//			cerr<<u<<' '<<out[u].size()<<' '<<in[u]<<endl;
			sumdeg[pu]+=out[u].size();
			sumdeg[pu]-=in[u];
			if(not out[u].empty())
			{
				minn[pu]=min(minn[pu],out[u].back().first-maxdel[u]);
			}
		}
		for(int u=1;u<=m;u++)
		{
//			cerr<<u<<' '<<find(u)<<' '<<sumdeg[find(u)]<<endl;
			if(find(u)==u)
			{
				if(sumdeg[u]==0 and minn[u]!=inf)
					ans-=minn[u];
			}
		}
		cout<<ans<<"\n";
	}
	return 0;
}