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

uva 10746 Crime Wave - The Sequel

2017-02-18 22:58 411 查看
原题:

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 m to 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

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

Sample Output

13.33

中文:

有n个银行被抢劫,有m个警察。给你每个警察到这n银行的时间,现在问你每个银行派一个警察,使得使得总消耗时间的平均和最小是多少?

#include<bits/stdc++.h>
using namespace std;
const int maxn=101;
const double inf=100000000.000;
const double eps = 1e-6;
struct Edge//边
{
int from,to,cap,flow;//出点,入点,容量,当前流量,费用(也就是权值)
double cost;
Edge(int u,int v,int c,int f,double w):from(u),to(v),cap(c),flow(f),cost(w){}
};

struct MCMF
{
int n,m;
vector<Edge> edges;//保存表
vector<int> G[maxn];//保存邻接关系
int inq[maxn];//判断一个点是否在队列当中(SPFA算法当中要用)
double d[maxn];//起点到d[i]的最短路径保存值
int p[maxn];//用来记录路径,保存上一条弧
int a[maxn];//找到增广路径后的改进量

void init(int n)//初始化
{
this->n=n;
for(int i=0;i<=n;i++)
G[i].clear();
edges.clear();
}

void AddEdge(int from,int to,int cap,double cost)//添加边
{
edges.push_back(Edge(from,to,cap,0,cost));//正向
edges.push_back(Edge(to,from,0,0,-cost));//反向
m=edges.size();
G[from].push_back(m-2);//按照边的编号保存邻接关系
G[to].push_back(m-1);
}

bool BellmanFord(int s,int t,int& flow,double& cost)//最短路径算法
{
for(int i=0;i<=n;i++)
d[i]=inf*1.0;
memset(inq,0,sizeof(inq));
d[s]=0;
inq[s]=1;
p[s]=0;
a[s]=inf;

queue<int> Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]=0;
for(int i=0;i<G[u].size();i++)
{
Edge& e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost+eps)//寻找满足容量大于流量的可松弛边
{
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to])//是否在队列当中
{
Q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(d[t]>=inf)//如果d[t]没有被更新,相当于没找到增广路径,则没有最大流也没有最小费用
return false;
flow+=a[t];//更新最大流
cost+=(double )d[t]*(double)a[t];//单位流量乘以单位路径长度用来计算消耗
for(int u=t;u!=s;u=edges[p[u]].from)//通过使用p[]保存的上一个边的值来对刚刚找到的增广路径上面的流量进行更新
{
edges[p[u]].flow+=a[t];//正向变更新
edges[p[u]^1].flow-=a[t];//反向变更新(用位运算实现的)
}
return true;
}

int MincostMaxflow(int s,int t,double& cost)//计算从s到t的最小消耗cost,返回最大流
{
int flow = 0;
cost=0;
while(BellmanFord(s,t,flow,cost));//不断寻找最短增广路径,直到找不到为止
return flow;
}
};
MCMF mcmf;
double tmp[21][21];
int main()
{
int n,m;
ios::sync_with_stdio(false);
while(cin>>n>>m,n+m)
{
mcmf.init(100);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>tmp[i][j];
}
}
mcmf.AddEdge(0,1,n,0);
for(int i=1;i<=m;i++)
mcmf.AddEdge(1,i+1,1,0);
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
mcmf.AddEdge(1+i,1+m+j,1,tmp[j][i]);
for(int i=1;i<=n;i++)
mcmf.AddEdge(m+1+i,m+n+2,1,0);

double ans;
mcmf.MincostMaxflow(0,m+n+2,ans);
cout<<fixed<<setprecision(2)<<(ans/n+eps)<<endl;
}
return 0;
}


思路:

裸的最小费用最大流问题,给每个警察到每个银行建立一条边,容量都是1,费用用给定的时间表示。

设置超级源点s’到源点s,容量设置成银行的数量,用来控制出警的数量,然后把s点和所有警察连接上,容量设置为1,费用设置为0,最后把所有银行连接到一个汇点上,每个边的容量都是1,费用都设置为0即可。

此题是小数,所以一定要注意精度转换!用eps最小精度来调节寻找最短增光路的松弛过程!结果也要加上eps
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: