Crime Wave – The Sequel
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
n banks have been robbed this fine day. m (greater than or equal to n) police cruisers are on duty at various locations in the city. n of the cruisers should be dispatched, one to each of the banks, so as to minimize the average time of arrival at the n banks.
Input
The input file contains several sets of inputs. The description of each set is given below:
The first line of input contains 0 < n <= m <= 20. n lines follow, each containing m positive real numbers: the travel time for cruiser mto reach bank n.
Input is terminated by a case where m=n=0. This case should not be processed.
Output
For each set of input output a single number: the minimum average travel time, accurate to 2 fractional digits.
Sample Input Output for Sample Input
3 4 10.0 23.0 30.0 40.0 5.0 20.0 10.0 60.0 18.0 20.0 20.0 30.0 0 0 |
13.33 |
题意:一艘船对应一个银行有一定的花费时间,求最小的平均时间。
思路:费用流模板题,输出需要加上eps才行。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
int v,flow,next;
double cost;
}edge[1010];
int n,N,m,tot,Head[110],F,pre[110],f[110],INF=1e9;
double C,dis[110],eps=1e-8;
bool vis[110];
queue<int> qu;
void add(int u,int v,double cost,int flow)
{
edge[tot].v=v;
edge[tot].cost=cost;
edge[tot].flow=flow;
edge[tot].next=Head[u];
Head[u]=tot++;
}
int spfa()
{
int i,j,k,u,v,p;
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
memset(f,0,sizeof(f));
for(i=0;i<=N;i++)
dis[i]=INF;
dis[0]=0;
vis[0]=1;
f[0]=INF;
qu.push(0);
while(!qu.empty())
{
u=qu.front();
qu.pop();
vis[u]=0;
for(p=Head[u];p>=0;p=edge[p].next)
{
v=edge[p].v;
if(edge[p].flow>0 && dis[v]>dis[u]+edge[p].cost)
{
dis[v]=dis[u]+edge[p].cost;
f[v]=min(f[u],edge[p].flow);
pre[v]=p;
if(!vis[v])
{
vis[v]=1;
qu.push(v);
}
}
}
}
//printf("%.2f\n",dis[N]);
if(dis[N]>INF-eps)
return 0;
F+=f[N];
C+=f[N]*dis[N];
if(F==n)
return 0;
for(p=pre[N];p>=0;p=pre[edge[p^1].v])
{
edge[p].flow-=f[N];
edge[p^1].flow+=f[N];
}
return 1;
}
int main()
{
int i,j,k;
double cost,t;
while(~scanf("%d%d",&n,&m) && n+m>0)
{
N=n+m+1;
tot=0;
memset(Head,-1,sizeof(Head));
for(i=1;i<=m;i++)
{
add(0,i,0,1);
add(i,0,0,0);
}
for(i=1;i<=n;i++)
{
add(m+i,N,0,1);
add(N,m+i,0,0);
}
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
scanf("%lf",&cost);
add(j,m+i,cost,1);
add(m+i,j,-cost,0);
}
F=0;
C=0;
while(spfa()){
//printf("%d %.2f %.2f %d %d\n",F,C,dis[N],n,m);
}
printf("%.2f\n",C/n+eps);
}
}