lightoj 1011 最大权重匹配或最大费用流
由于暂时不会KM算法,只能用最大费用流来做了。
题目链接:http://lightoj.com/volume_showproblem.php?problem=1011
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> #include <queue> #include <vector> using namespace std; const int maxe = 50000; const int maxn = 500; const int INF = 0x3f3f3f; struct Edge{ int u,v,flow,cap,cost; int next; Edge(int u=0,int v=0,int flow=0,int cap=0,int cost=0,int next=0): u(u), v(v), flow(flow), cap(cap), cost(cost), next(next) {} }; struct MCMF{ int d[maxn]; bool inq[maxn]; Edge edges[maxe]; int head[maxn],cnt; int pa[maxn]; //用于回溯找增广路。 int res[maxn]; void init(){ memset(head,-1,sizeof(head)); cnt = 0; } void addedge(int u,int v,int cap,int cost){ edges[cnt] = Edge(u,v,0,cap,cost,head[u]); head[u] = cnt++; edges[cnt] = Edge(v,u,0,0,-cost,head[v]); head[v] = cnt++; } bool SPFA(int s,int t,int& flow,int& cost){ memset(inq,0,sizeof(inq)); memset(d,-0x3f,sizeof(d)); queue<int> Q; Q.push(s); d[s] = 0; inq[s] = true; res[s] = INF; res[t] = 0; while(!Q.empty()){ int u = Q.front(); Q.pop(); inq[u] = false; for(int i=head[u];i!=-1;i=edges[i].next){ Edge& e = edges[i]; if(e.cap > e.flow && d[e.v] < d[u] + e.cost){ d[e.v] = d[u] + e.cost; pa[e.v] = i; res[e.v] = min(res[u],e.cap-e.flow); if(!inq[e.v]){ Q.push(e.v); inq[e.v] = true; } } } } if(res[t] == 0) return false; flow += res[t]; cost += res[t]*d[t]; for(int i=t;i!=s;i=edges[pa[i]].u){ edges[pa[i]].flow += res[t]; edges[pa[i]^1].flow -= res[t]; } return true; } void MaxCost(int s,int t){ int flow = 0,cost = 0; while(SPFA(s,t,flow,cost)) {} printf("%d\n",cost); } }solver; int main() { //freopen("E:\\acm\\input.txt","r",stdin); int T; cin>>T; for(int cas=1;cas<=T;cas++){ solver.init(); int N; cin>>N; int s = 0, t = 2*N + 1; for(int i=1;i<=N;i++){ solver.addedge(s,i,1,0); solver.addedge(i+N,t,1,0); } for(int i=1;i<=N;i++) for(int j=1;j<=N;j++){ int a; scanf("%d",&a); solver.addedge(i,j+N,1,a); } printf("Case %d: ",cas); solver.MaxCost(s,t); } }