1 条题解

  • 0
    @ 2023-6-21 20:06:45

    C++ :

    #pragma GCC optimize ("O2")
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int MAXSIZE=10000020;
    const int INFint=0x3f3f3f3f;
    const ll INF=0x3f3f3f3f3f3f3f3fLL;
    int bufpos;
    char buf[MAXSIZE];
    #define NEG 0
    void init(){
    	#ifdef LOCAL
    		freopen("6079.txt","r",stdin);
    	#endif
    	buf[fread(buf,1,MAXSIZE,stdin)]='\0';
    	bufpos=0;
    }
    #if NEG
    int readint(){
    	bool isneg;
    	int val=0;
    	for(;!isdigit(buf[bufpos]) && buf[bufpos]!='-';bufpos++);
    	bufpos+=(isneg=buf[bufpos]=='-');
    	for(;isdigit(buf[bufpos]);bufpos++)
    		val=val*10+buf[bufpos]-'0';
    	return isneg?-val:val;
    }
    #else
    int readint(){
    	int val=0;
    	for(;!isdigit(buf[bufpos]);bufpos++);
    	for(;isdigit(buf[bufpos]);bufpos++)
    		val=val*10+buf[bufpos]-'0';
    	return val;
    }
    #endif
    char readchar(){
    	for(;isspace(buf[bufpos]);bufpos++);
    	return buf[bufpos++];
    }
    int readstr(char* s){
    	int cur=0;
    	for(;isspace(buf[bufpos]);bufpos++);
    	for(;!isspace(buf[bufpos]);bufpos++)
    		s[cur++]=buf[bufpos];
    	s[cur]='\0';
    	return cur;
    }
    const int maxn=10001;
    const int maxm=100001;
    struct graph{
    	int n,m;
    	struct edge{
    		int from,to,cap,cost,next;
    	}e[maxm];
    	void init(int n){
    		this->n=n;
    		m=1;
    	}
    	int first[maxn];
    	int addedge(int from,int to,int cap,int cost){
    		// printf("add %d %d %d %d\n",from,to,cap,cost);
    		e[++m]=(edge){from,to,cap,cost,first[from]};
    		first[from]=m;
    		e[++m]=(edge){to,from,0,-cost,first[to]};
    		first[to]=m;
    		return m-1;
    	}
    	int t;
    	int q[maxn],p[maxn];
    	bool inq[maxn];
    	ll dist[maxn];
    	bool spfa(int s){
    		int l=0,r=0;
    		q[(r++)%n]=s;
    		for(int i=1;i<=n;i++)
    			dist[i]=-INF;
    		dist[s]=0;
    		while(l<r){
    			int u=q[(l++)%n];
    			inq[u]=0;
    			// printf("out %d\n",u);
    			for(int i=first[u];i;i=e[i].next){
    				int v=e[i].to;
    				if (e[i].cap && dist[u]!=-INF && dist[v]<dist[u]+e[i].cost){
    					dist[v]=dist[u]+e[i].cost;
    					p[v]=i;
    					if (!inq[v]){
    						// p[v]=i;
    						q[(r++)%n]=v;
    						inq[v]=1;
    					}
    				}
    			}
    		}
    		return dist[t]!=-INF;
    	}
    	ll mcmf(int s,int t){
    		this->t=t;
    		ll ans=0;
    		while(spfa(s)){
    			int flow=INFint;
    			for(int i=t;i!=s;i=e[p[i]].from)
    				flow=min(flow,e[p[i]].cap);
    			for(int i=t;i!=s;i=e[p[i]].from){
    				e[p[i]].cap-=flow;
    				e[p[i]^1].cap+=flow;
    			}
    			ans+=flow*dist[t];
    		}
    		return ans;
    	}
    }g;
    int e[1002],id[1002];
    int main(){
    	init();
    	int n=readint(),k=readint(),ms=readint(),me=readint();
    	ll sum=0;
    	for(int i=1;i<=n;i++){
    		e[i]=readint();
    		sum+=e[i];
    	}
    	for(int i=1;i<=n;i++)
    		e[i]=readint()-e[i];
    	int s=n-k+3,t=s+1;
    	g.init(t);
    	for(int i=1;i<=n;i++)
    		id[i]=g.addedge(i<=n-k?i:n-k+1,i>k?i-k:n-k+2,1,e[i]);
    	for(int i=1;i<=n-k+1;i++)
    		g.addedge(i,i==1?n-k+2:i-1,k-ms-me,0);
    	g.addedge(n-k+2,t,k-ms,0);
    	g.addedge(s,n-k+1,k-ms,0);
    	printf("%lld\n",sum+g.mcmf(s,t));
    	for(int i=1;i<=n;i++)
    		putchar(g.e[id[i]].cap?'S':'E');
    }
    
    • 1

    #6079. 「2017 山东一轮集训 Day7」养猫

    信息

    ID
    1035
    时间
    1000ms
    内存
    512MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者