# 洛谷 P2053 [SCOI2007]修车 费用流

2 2
3 2
1 4

1.50

(2<=M<=9,1<=N<=60), (1<=T<=1000)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>

const int maxn=1007;
const int maxe=100006;
const int inf=0x3f3f3f3f;

using namespace std;

int n,m,s,t,flow,cost,cnt,x;
int ls[maxn],dis[maxn],vis[maxn],pre[maxn];

queue <int> q;

struct edge{
int x,y,w,c,op,next;
}g[maxe];

void add(int x,int y,int w,int c)
{
g[++cnt]=(edge){x,y,w,c,cnt+1,ls[x]};
ls[x]=cnt;
g[++cnt]=(edge){y,x,0,-c,cnt-1,ls[y]};
ls[y]=cnt;
}

bool spfa()
{
while (!q.empty()) q.pop();
for (int i=s;i<=t;i++)
{
dis[i]=inf;
vis[i]=0;
}
dis[s]=0;
vis[s]=1;
q.push(s);
while (!q.empty())
{
int x=q.front();
q.pop();
for (int i=ls[x];i>0;i=g[i].next)
{
int y=g[i].y;
if (!g[i].w) continue;
if (dis[x]+g[i].c<dis[y])
{
dis[y]=dis[x]+g[i].c;
pre[y]=i;
if (!vis[y])
{
vis[y]=1;
q.push(y);
}
}
}
vis[x]=0;
}
return (dis[t]!=inf);
}

void msf()
{
int x=inf,i=t;
while (pre[i])
{
x=min(x,g[pre[i]].w);
i=g[pre[i]].x;
}
flow+=x;
i=t;
while (pre[i])
{
g[pre[i]].w-=x;
g[g[pre[i]].op].w+=x;
cost+=x*g[pre[i]].c;
i=g[pre[i]].x;
}
}
int main()
{
scanf("%d%d",&m,&n);
s=0; t=n+n*m+1;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
scanf("%d",&x);
}
}
while (spfa()) msf();
double ans=(double)cost/n;
printf("%.2lf",ans);
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120