Crime Wave – The Sequel - UVa 10746 费用流

本文介绍了一个关于CrimeWave游戏中的算法问题,通过派遣警车到被抢银行来最小化平均到达时间。使用费用流算法解决该问题,并给出了详细的AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Crime Wave – The Sequel

Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

n banks have been robbed this fine day. (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 <= 20n 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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值