链接:
https://vjudge.net/problem/HDU-2732
题意:
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room‘s floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below... Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there‘s a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.
思路:
依然是拆点建图.每个柱子拆成入口和出口,权值为可用次数,出口连到别的点和边缘都为INF.
对每个有人的柱子,源点连一个权值为1的边.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 20+10;
const int INF = 1e9;
struct Edge
{
int from, to, cap;
};
vector<Edge> edges;
vector<int> G[MAXN*MAXN*4];
int Dis[MAXN*MAXN*4];
char Map1[MAXN][MAXN], Map2[MAXN][MAXN];
int n, m, s, t, d;
void AddEdge(int from, int to, int cap)
{
edges.push_back(Edge{from, to, cap});
edges.push_back(Edge{to, from, 0});
G[from].push_back(edges.size()-2);
G[to].push_back(edges.size()-1);
}
bool Bfs()
{
memset(Dis, -1, sizeof(Dis));
queue<int> que;
que.push(s);
Dis[s] = 0;
while (!que.empty())
{
int u = que.front();
que.pop();
// cout << u << endl;
for (int i = 0;i < G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (e.cap > 0 && Dis[e.to] == -1)
{
Dis[e.to] = Dis[u]+1;
que.push(e.to);
}
}
}
return Dis[t] != -1;
}
int Dfs(int u, int flow)
{
if (u == t)
return flow;
int res = 0;
for (int i = 0;i < G[u].size();i++)
{
Edge &e = edges[G[u][i]];
if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
{
int tmp = Dfs(e.to, min(flow, e.cap));
// cout << "flow:" << e.from << ' ' << e.to << ' ' << tmp << endl;
e.cap -= tmp;
flow -= tmp;
edges[G[u][i]^1].cap += tmp;
res += tmp;
if (flow == 0)
break;
}
}
if (res == 0)
Dis[u] = -1;
return res;
}
int MaxFlow()
{
int res = 0;
while (Bfs())
{
res += Dfs(s, INF);
}
return res;
}
int main()
{
// freopen("test.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
int T, cnt = 0;
cin >> T;
while (T--)
{
cin >> n >> d;
for (int i = 1;i <= n;i++)
cin >> (Map1[i]+1);
for (int i = 1;i <= n;i++)
cin >> (Map2[i]+1);
m = strlen(Map1[1]+1);
s = 0, t = n*m*2+1;
for (int i = s;i <= t;i++)
G[i].clear();
edges.clear();
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
//(i-1)*m+j
if (Map1[i][j] == 0)
continue;
int node = (i-1)*m+j;
AddEdge(node*2-1, node*2, Map1[i][j]-'0');
if (i <= d || i > n-d || j <= d || j > m-d)
{
// cout << i << ' ' << j << endl;
AddEdge(node*2, t, INF);
}
for (int z = 1;z <= n;z++)
{
for (int k = 1;k <= m;k++)
{
if (abs(z-i)+abs(k-j) > d)
continue;
if (i == z && j == k)
continue;
// cout << i << ' ' << j << ' ' << ' ' << z << ' ' << k << endl;
int nodeto = (z-1)*m+k;
AddEdge(node*2, nodeto*2-1, INF);
// cout << Map1[i][j]-'0' << endl;
}
}
}
}
int res = 0;
for (int i = 1;i <= n;i++)
{
for (int j = 1;j <= m;j++)
{
if (Map2[i][j] == '.')
continue;
res++;
int node = (i-1)*m+j;
AddEdge(s, node*2-1, 1);
}
}
res -= MaxFlow();
if (res == 0)
cout << "Case #" << ++cnt << ": no lizard was left behind." << endl;
else if (res == 1)
cout << "Case #" << ++cnt << ": " << res << " lizard was left behind." << endl;
else
cout << "Case #" << ++cnt << ": " << res << " lizards were left behind." << endl;
}
return 0;
}
/*
10
3 1
111
111
111
LLL
LLL
LLL
*/