您的位置:首页 > 产品设计 > UI/UE

Crime Wave – The Sequel - UVa 10746 费用流

2015-02-09 17:46 751 查看

CrimeWave–TheSequel

Input:StandardInput

Output:StandardOutput
TimeLimit:2Seconds


nbankshavebeenrobbedthisfineday.m(greaterthanorequalton)policecruisersareondutyatvariouslocationsinthecity.nofthecruisersshouldbedispatched,onetoeachof
thebanks,soastominimizetheaveragetimeofarrivalatthenbanks.



Input

Theinputfilecontainsseveralsetsofinputs.Thedescriptionofeachsetisgivenbelow:

Thefirstlineofinputcontains0<n<=m<=20.nlinesfollow,eachcontainingmpositiverealnumbers:thetraveltimeforcruisermtoreachbankn.

Inputisterminatedbyacasewherem=n=0.Thiscaseshouldnotbeprocessed.

Output

Foreachsetofinputoutputasinglenumber:theminimumaveragetraveltime,accurateto2fractionaldigits.


SampleInputOutputforSampleInput

34

10.023.030.040.0

5.020.010.060.0

18.020.020.030.0

00

13.33

题意:一艘船对应一个银行有一定的花费时间,求最小的平均时间。

思路:费用流模板题,输出需要加上eps才行。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
usingnamespacestd;
structnode
{
intv,flow,next;
doublecost;
}edge[1010];
intn,N,m,tot,Head[110],F,pre[110],f[110],INF=1e9;
doubleC,dis[110],eps=1e-8;
boolvis[110];
queue<int>qu;
voidadd(intu,intv,doublecost,intflow)
{
edge[tot].v=v;
edge[tot].cost=cost;
edge[tot].flow=flow;
edge[tot].next=Head[u];
Head[u]=tot++;
}
intspfa()
{
inti,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
);
if(dis
>INF-eps)
return0;
F+=f
;
C+=f
*dis
;
if(F==n)
return0;
for(p=pre
;p>=0;p=pre[edge[p^1].v])
{
edge[p].flow-=f
;
edge[p^1].flow+=f
;
}
return1;
}
intmain()
{
inti,j,k;
doublecost,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,m);
}
printf("%.2f\n",C/n+eps);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: