#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 11000
#define M 101000
#define INF 0x3f3f3f3f
using namespace std;
int map[65][65];
int n,m,cnt,S,T;
struct node
{
int from,to,val,cost,next;
}edge[M];
int head[N],dis[N],v[N],f[N];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void edgeadd(int from,int to,int val,int cost)
{
edge[cnt].to=to;
edge[cnt].val=val;
edge[cnt].from=from;
edge[cnt].cost=cost;
edge[cnt].next=head[from];
head[from]=cnt++;
}
int spfa(int s,int e)
{
memset(dis,0x3f,sizeof(dis));
memset(v,0,sizeof(v));
memset(f,-1,sizeof(f));
queue<int>q;
q.push(s);
dis[s]=0;
v[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
v[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(edge[i].val!=0&&dis[u]+edge[i].cost<dis[to])
{
dis[to]=dis[u]+edge[i].cost;
f[to]=i;
if(!v[to])
{
q.push(to);
v[to]=1;
}
}
}
}
if(dis[e]!=0x3f3f3f3f)return 1;
return 0;
}
void mcmf()
{
int ans=0;
while(spfa(S,T))
{
int flow=INF;
for(int i=f[T];i!=-1;i=f[edge[i].from])flow=min(flow,edge[i].val);
for(int i=f[T];i!=-1;i=f[edge[i].from])
{
edge[i].val-=flow;
edge[i^1].val+=flow;
}
ans+=flow*dis[T];
}
printf("%.2lf\n",(double)ans/m);
}
int main()
{
scanf("%d%d",&n,&m);
S=0,T=n*m+m+1;
init();
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&map[i][j]);
}
}
for(int i=1;i<=n*m;i++)
{
edgeadd(S,i,1,0);
edgeadd(i,S,0,0);
}
for(int i=n*m+1;i<=n*m+m;i++)
{
edgeadd(i,T,1,0);
edgeadd(T,i,0,0);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
for(int k=1;k<=m;k++)
{
edgeadd((i-1)*m+j,n*m+k,1,map[k][i]*j);
edgeadd(n*m+k,(i-1)*m+j,0,-map[k][i]*j);
}
}
}
mcmf();
}
原文地址:http://blog.csdn.net/wzq_qwq/article/details/46680593