本文主要是介绍codevs 1028 花店橱窗布置 最小费用最大流,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
花与花瓶连边,容量为1,费用为对应费用,s向花连边,容量为1,费用为0,花瓶向t连边,容量为1,费用为0。这里要求最大费用,把费用设为相反数,结果也取相反数。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 1000000000
using namespace std;
int S,T;
int f,v;
struct Edge
{int from,to,cap,cost;int next;
} edges[50000];
int ne=0;
int head[250];void addEdge(int from,int to,int cap,int cost)
{edges[ne].from=from;edges[ne].to=to;edges[ne].cap=cap;edges[ne].cost=cost;edges[ne].next=head[from];head[from]=ne++;edges[ne].from=to;edges[ne].to=from;edges[ne].cap=0;edges[ne].cost=-cost;edges[ne].next=head[to];head[to]=ne++;
}bool vis[250];
int pre[250];
int d[250];bool spfa(int s,int t,int n)
{queue<int>que;memset(pre,-1,sizeof(pre));memset(vis,0,sizeof(vis));for(int i=s; i<=t; i++){d[i]=inf;}d[s]=0;vis[s]=1;que.push(s);while(!que.empty()){int u=que.front();//cout<<u<<endl;que.pop();vis[u]=0;for(int i=head[u]; i!=-1; i=edges[i].next){int v=edges[i].to;if(edges[i].cap>0&&d[v]>d[u]+edges[i].cost){d[v]=d[u]+edges[i].cost;pre[v]=i;if(vis[v]==0){que.push(v);vis[v]=1;}}}}if(d[t]==inf)return false;return true;
}int MCMF(int s,int t,int n)
{int flow=0,mincost=0;int minflow;//cout<<2<<endl;while(spfa(s,t,n)){//cout<<1<<endl;minflow=inf;for(int i=pre[t]; i!=-1; i=pre[edges[i].from]){minflow=min(minflow,edges[i].cap);}for(int i=pre[t]; i!=-1; i=pre[edges[i].from]){edges[i].cap-=minflow;edges[i^1].cap+=minflow;}flow+=minflow;mincost+=minflow*d[t];}return mincost;
}int main()
{memset(head,-1,sizeof(head));cin>>f>>v;int a;for(int i=1; i<=f; i++){for(int j=1; j<=v; j++){scanf("%d",&a);addEdge(i,f+j,1,-a);}}S=0,T=f+v+1;for(int i=1; i<=f; i++){addEdge(S,i,1,0);}for(int i=1; i<=v; i++){addEdge(f+i,T,1,0);}int ans=MCMF(S,T,T+1);cout<<-ans;return 0;
}
这篇关于codevs 1028 花店橱窗布置 最小费用最大流的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!