玄2关求条ABC G
  • 板块学术版
  • 楼主harmis_yz
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/5/18 22:08
  • 上次更新2024/5/19 09:38:28
查看原帖
玄2关求条ABC G
993404
harmis_yz楼主2024/5/18 22:08

01_random_05.txt 这个点 WA 了。不知道为什么pwp。

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define re register
#define il inline
#define pii pair<int,int>
#define x first
#define y second
#define gc getchar()
#define rd read()
#define debug() puts("------------")

namespace yzqwq{
	il int read(){
		int x=0,f=1;char ch=gc;
		while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc;}
		while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=gc;
		return x*f;
	}
	il int qmi(int a,int b,int p){
		int ans=1;
		while(b){
			if(b&1) ans=ans*a%p;
			a=a*a%p,b>>=1;
		}
		return ans;
	}
	il auto max(auto a,auto b){return (a>b?a:b);}
	il auto min(auto a,auto b){return (a<b?a:b);}
	il int gcd(int a,int b){
		if(!b) return a;
		return gcd(b,a%b);
	}
	il int lcm(int a,int b){
		return a/gcd(a,b)*b;
	}
	il void exgcd(int a,int b,int &x,int &y){
		if(!b) return x=1,y=0,void(0);
		exgcd(b,a%b,x,y);
		int t=x;
		x=y,y=t-a/b*x;
		return ;
	}
	mt19937 rnd(time(0));
}
using namespace yzqwq;

const int N=8e6+10;
int a[N],n,s,t; 
string str[N];
int ne[N<<1],e[N<<1],w[N<<1],h[N],h2[N],idx=1;
int dep[N],vis[N];
int sum,ans;

il void add(int a,int b,int c){
//	cout<<a<<" "<<b<<" "<<c<<"\n";
	ne[++idx]=h[a],e[idx]=b,w[idx]=c,h[a]=idx;
	ne[++idx]=h[b],e[idx]=a,w[idx]=0,h[b]=idx;
}
il bool bfs(){
	for(re int i=s;i<=t;++i)
		h2[i]=h[i],
		dep[i]=1e18,vis[i]=0;
	queue<int> qu;
	qu.push(s),dep[s]=0,vis[s]=1;
	while(!qu.empty()){
		int now=qu.front();qu.pop();
		for(re int i=h[now];i;i=ne[i]){
			int j=e[i];
			if(dep[j]>dep[now]+1&&w[i]){
				dep[j]=dep[now]+1;
				if(!vis[j]) vis[j]=1,qu.push(j);
			}
		}
	}
	if(dep[t]<1e18) return 1;
	return 0;
}
il int dfs(int now,int Min){
	if(now==t) return ans+=Min,Min;
	int use=0;
	for(re int i=h2[now];i;i=ne[i]){
		int j=e[i];h2[now]=i;
		if(dep[j]==dep[now]+1&&w[i]){
			int use_=dfs(j,min(Min-use,w[i]));
			if(use_<=0) continue;
			use+=use_;
			w[i]-=use_,w[i^1]+=use_;
			if(use==Min) return use;
		}
	}
	return use;
}
il void dinic(){
	while(bfs()) dfs(s,1e18);
	return ;
}

il void solve(){
	n=rd,s=0,t=1e6+1;
	for(re int i=1;i<=n;++i) cin>>str[i];
	for(re int i=1;i<=n;++i) a[i]=rd,sum+=a[i];
	for(re int i=1;i<=n;++i)
	for(re int j=1;j<=n;++j){
		if(i==j) continue;
		int idx=str[j].find(str[i]);
		if(idx!=string::npos) add(i,n+j,1e18);
	}
	for(re int i=1;i<=n;++i)
		add(s,i,a[i]),add(i+n,t,a[i]);//debug();
	dinic();
	cout<<sum-ans;
	return ;
}

signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int t=1;while(t--)
	solve();
	return 0;
}
2024/5/18 22:08
加载中...