哪里错了
查看原帖
哪里错了
723383
dhgzh楼主2024/8/6 16:12

题目

#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
const int maxn=5e5;
struct node{
	int v,w,c,nxt;
}edge[maxn*2];
int ans,s,t,N,p,a,fast,b,slow,num,cnt=1,n;
int head[maxn],dep[maxn],r[maxn],h[maxn];
bool v[maxn];
queue<int> q;
void add(int from,int to,int cc,int cc2){
	edge[++cnt]={to,cc,cc2,head[from]};
	head[from]=cnt;
	edge[++cnt]={from,0,-cc2,head[to]};
	head[to]=cnt;
	return;
}
bool bfs(){
	for(int i=0;i<=maxn;i++) dep[i]=inf,v[i]=0,h[i]=head[i];
	while(!q.empty()) q.pop();
	q.push(s);
	dep[s]=0;
	v[s]=1;
	while(!q.empty()){
		int tmp=q.front();
		q.pop();
		v[tmp]=0;
		for(int i=head[tmp];i;i=edge[i].nxt){
			if(dep[edge[i].v]>dep[tmp]+edge[i].c&&edge[i].w){
				dep[edge[i].v]=dep[tmp]+edge[i].c;
				if(!v[edge[i].v]) v[edge[i].v]=1,q.push(edge[i].v);
			}
		}
	}
	if(dep[t]!=inf) return 1;
	return 0;
}
int dfs(int x,int sum){
	memset(v,0,sizeof(v));
	if(x==t){
		v[t]=1;
		return sum;
	}
	int k=0,res=0;
	v[x]=1;
	for(int i=h[x];i&&sum;i=edge[i].nxt){
		h[x]=1;
		if((!v[edge[i].v]||edge[i].v==t)&&edge[i].w&&dep[edge[i].v]==dep[x]+edge[i].c){
			k=dfs(edge[i].v,min(sum,edge[i].w));
			if(k==0) dep[edge[i].v]=inf;
			edge[i].w-=k;
			edge[i^1].w+=k;
			res+=k;
			sum-=k;
			ans+=edge[i].c*k;
		}
	}
	return res;
}
int main(){
	cin>>n;
	s=0,t=2*n+1;
	for(int i=1;i<=n;i++){
		cin>>r[i];
		add(s,i,r[i],0); 
		add(i+n,t,r[i],0);
	}
	cin>>p>>a>>fast>>b>>slow;
	for(int i=1;i<=n;i++){
		add(s,i+n,inf,p);
		if(i+1<=n) add(i,i+1,inf,0); 
		if(i+a<=n) add(i,i+n+a,inf,fast);
		if(i+b<=n) add(i,i+n+b,inf,slow);
	}
	while(bfs()){
		v[t]=1;
		while(v[t]) dfs(s,inf);
		cout<<ans<<endl;
	}
	cout<<ans;
	return 0;
}

感谢!调了一天了

2024/8/6 16:12
加载中...