题意:还记得上次那些抢银行的强盗吗?这次为了银行的安全,要派一些警察去守护这些银行。现在有n个银行和m警察,给出每个警察到达各个银行的时间,问说怎么样安排人手,可以使的警察到达银行的平均时间最小。
解题思路:建图是关键。设置一个超级源点连向所有的警察,容量为1,费用为0;设置一个超级汇点,使所有的银行连向超级汇点,容量为1, 费用为零。然后,连接所有警察和银行,费用为题目给出时间,容量为1。然后就是求最小费给最大流。
Wrong answer:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <climits>
#include <string>
using namespace std;
const double eps=1e-6;
const int INF = 0x3f3f3f3f;
const int maxn=20+20+10;
struct Edge
{
int from,to,cap,flow;
double cost;
};
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
double d[maxn];
int p[maxn];
int a[maxn];
void init()
{
for(int i=0;i<=n+m+1;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back((Edge){from,to,cap,0,cost});
edges.push_back((Edge){to,from,0,0,-cost});
int v=edges.size();
G[from].push_back(v-2);
G[to].push_back(v-1);
}
bool Bellman_Ford(int s, int t, int& flow, double& cost)
{
queue<int> q;
memset(inq, 0, sizeof(inq));
memset(a, 0, sizeof(a));
memset(p, 0, sizeof(p));
for (int i = 0; i <= n+m+1; i++) d[i] = INF;
d[s] = 0;
a[s] = INF;
inq[s] = 1;
p[s] = 0;
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 > 1e-6)
{
d[e.to] = d[u] + e.cost;
a[e.to] = min(a[u], e.cap - e.flow);
p[e.to] = G[u][i];
if (!inq[e.to])
{
inq[e.to] = 1;
q.push(e.to);
}
}
}
}
if (INF - d[t] < 1e-6) return false;
flow += a[t];
cost += (double)d[t] * (double)a[t];
for (int u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
}
return true;
}
double Mincost(int s,int t)
{
int flow=0;
double cost=0;
while(Bellman_Ford(s,t,flow,cost));
return cost;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0)break;
s=0;
t=n+m+1;
init();
for(int i=1;i<=m;i++)AddEdge(0,i,1,0);
for(int i=m+1;i<=m+n;i++)AddEdge(i,n+m+1,1,0);
for(int i=m+1;i<=m+n;i++)
{
for(int j=1;j<=m;j++)
{
double times;
scanf("%lf",×);
AddEdge(j,i,1,times);
}
}
double ans=Mincost(s,t);
printf("%.2lf\n",ans/n+0.001);
}
return 0;
}
一模一样,还是错,无解。
Accepted:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <climits>
#include <string>
using namespace std;
const int maxn = 20+20+10;
const int INF = 0x3f3f3f3f;
int n, m, s, t;
int a[maxn], p[maxn], inq[maxn];
double d[maxn];
struct Edge
{
int from, to, cap, flow;
double cost;
};
vector<Edge> edges;
vector<int> G[maxn];
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});
int m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
void init()
{
for (int i = 0; i <=m+n+1; i++) G[i].clear();
edges.clear();
}
bool Bellmaxn_Ford(int s, int t, int& flow, double& cost)
{
queue<int> q;
memset(inq, 0, sizeof(inq));
memset(a, 0, sizeof(a));
memset(p, 0, sizeof(p));
for (int i = 0; i <= n+m+1; i++) d[i] = INF;
d[s] = 0;
a[s] = INF;
inq[s] = 1;
p[s] = 0;
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 > 1e-6)
{
d[e.to] = d[u] + e.cost;
a[e.to] = min(a[u], e.cap - e.flow);
p[e.to] = G[u][i];
if (!inq[e.to])
{
inq[e.to] = 1;
q.push(e.to);
}
}
}
}
if (INF - d[t] < 1e-6) return false;
flow += a[t];
cost += (double)d[t] * (double)a[t];
for (int u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
}
return true;
}
double Mincost(int s, int t)
{
int flow = 0;
double cost = 0;
while (Bellmaxn_Ford(s, t, flow, cost));
return cost;
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
if (n == 0 && m == 0) break;
s = 0, t = n+m+1;
init();
for(int i=1;i<=m;i++)AddEdge(0,i,1,0);
for(int i=m+1;i<=m+n;i++)AddEdge(i,n+m+1,1,0);
for(int i=m+1;i<=m+n;i++)
{
for(int j=1;j<=m;j++)
{
double times;
scanf("%lf",×);
AddEdge(j,i,1,times);
}
}
double ans=Mincost(s, t);
printf("%.2lf\n", ans / n + 0.001);
}
return 0;
}