CF190 DIV1 B Ciel and Duel maximum charge flow

Source: Internet
Author: User

This CF was so happy to go!

The Charge flow solution for this question is ......

Two cases are discussed: Ciel selects the intermediate end, and Ciel finishes all tasks.

[I always wanted to solve QAQ in two cases at the beginning]

In the first case, Ciel is on the left, Jiro is on the right, traffic 1 on the left of the source node is 0, and traffic 1 on the right is 0. For each ATK of Jiro, if Ciel card> = Jiro card then edge, traffic 1 cost C-J, run the maximum charge flow until the end of a certain increase in the cost of the road is negative.

In the second case, the number of Ciel cards must be greater than that of Jiro cards, the number of Ciel cards on the left is Ciel, the number of Jiro cards on the right is Jiro, the cost of Traffic 1 on the left is 0, and the cost of Traffic 1 on the right is 0, for every ATK of Jiro, if Ciel card> = Jiro card is connected edge, traffic 1 cost C-J, for every DEF of Jiro, if Ciel card> Jiro card is connected edge, traffic 1 cost 0, and then pull out one more point EX, the point on the left is connected to the EX traffic 1 cost is the strength of this card, the EX connection point traffic is the number of Ciel card-the number of Jiro cards, the cost is 0, and the maximum charge flow must be full.

Output the maximum values of the two methods.

#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<string>#include<map>#include<set>#include<queue>#include<stack>#include<ctime>#include<vector>#include<utility>using namespace std;#define N 1010#define M 1000010#define INF (1<<30)int n,m,s,t,ans;int head[N],d[N],pre[N],cnt;bool vis[N];struct edge{int v,w,c,next;}e[M];void addedge(int u,int v,int w,int c){e[cnt]=(edge){v,w,c,head[u]}; head[u]=cnt++;e[cnt]=(edge){u,0,-c,head[v]}; head[v]=cnt++;}int spfa(){queue<int> q;memset(pre,-1,sizeof(pre));for(int i=0;i<N;++i) d[i]=-INF;d[s]=0;q.push(s);while (! q.empty()){int u=q.front();q.pop();vis[u]=0;for(int i=head[u];i!=-1;i=e[i].next)if(e[i].w)if(d[e[i].v]<d[u]+e[i].c){d[e[i].v]=d[u]+e[i].c;pre[e[i].v]=i;if(! vis[e[i].v]) q.push(e[i].v),vis[e[i].v]=1;}}return d[t]!=-INF;}int flow;void mcmf1(){ans=flow=0;while (spfa()){int u,mn=INF;for(u=t;u!=s;u=e[pre[u]^1].v)mn=min(mn,e[pre[u]].w);if(d[t]<0) break;flow+=mn;ans+=mn*d[t];for(u=t;u!=s;u=e[pre[u]^1].v)e[pre[u]].w-=mn,e[pre[u]^1].w+=mn;}}void mcmf2(){ans=flow=0;while (spfa()){int u,mn=INF;for(u=t;u!=s;u=e[pre[u]^1].v)mn=min(mn,e[pre[u]].w);flow+=mn;ans+=mn*d[t];for(u=t;u!=s;u=e[pre[u]^1].v)e[pre[u]].w-=mn,e[pre[u]^1].w+=mn;}}int n1,n2,ans1;char ss[N][6];int J[N],C[N];int main (){freopen("1.in","r",stdin);scanf("%d%d",&n1,&n2);for(int i=1;i<=n1;++i) scanf("%s %d",ss[i],&J[i]);for(int i=1;i<=n2;++i) scanf("%d",&C[i]);n=n1+n2;s=n+1;t=s+1;cnt=0;memset(head,-1,sizeof(head));for(int i=1;i<=n1;++i)addedge(n2+i,t,1,0);for(int i=1;i<=n2;++i){addedge(s,i,1,0);for(int j=1;j<=n1;++j)if(strcmp(ss[j],"ATK")==0 && C[i]>=J[j]) addedge(i,j+n2,1,C[i]-J[j]);}mcmf1();ans1=ans;if(n2<=n1){printf("%d\n",ans1);return 0;}cnt=0;memset(head,-1,sizeof(head));int ex=t+1;addedge(ex,t,n2-n1,0);for(int i=1;i<=n1;++i)addedge(n2+i,t,1,0);for(int i=1;i<=n2;++i){addedge(s,i,1,0);addedge(i,ex,1,C[i]);for(int j=1;j<=n1;++j){if(strcmp(ss[j],"ATK")==0){if(C[i]>=J[j]) addedge(i,j+n2,1,C[i]-J[j]);}else{if(C[i]>J[j]) addedge(i,j+n2,1,0);}}}mcmf2();if(flow==n2) printf("%d\n",max(ans,ans1));else printf("%d\n",ans1);return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.