题目链接:https://vjudge.net/contest/299467#problem/K
这个题目从数据范围来看可以发现是网络流,怎么建图呢?这个其实不是特别难,主要是读题难。
这个建图就是把源点和每一个蜥蜴存在的点相连,汇点和可以跑出去的相连,因为这个题目对于每一个点都有次数要求,所以就要拆点。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 5e5 + 10;
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m;
void init()
{
for (int i = 0; i <= maxn; i++)G[i].clear();
e.clear();
}
void add(int u, int v, int c)
{
e.push_back(edge(u, v, c, 0));
e.push_back(edge(v, u, 0, 0));
m = e.size();
G[u].push_back(m - 2);
G[v].push_back(m - 1);
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -1, sizeof(level));
queue<int>q;
level[s] = 0;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = 0; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < 0)
{
level[now.v] = level[u] + 1;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > 0 && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > 0)
{
now.f += d;//正向边流量加d
e[G[u][v] ^ 1].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return 0;
}
int Maxflow(int s, int t)
{
int flow = 0;
for (;;)
{
BFS(s);
if (level[t] < 0)return flow;//残余网络中到达不了t,增广路不存在
memset(iter, 0, sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > 0)
{
flow += f;
}
}
return flow;
}
struct node
{
int x, y, flow;
}exa[maxn];
int main()
{
int qaq, cas = 0;
scanf("%d", &qaq);
while(qaq--)
{
init();
int nn, dd, tot = 0, len;
char cs[110];
scanf("%d%d", &nn, &dd);
for(int i=0;i<nn;i++)
{
scanf("%s", cs);
len = strlen(cs);
for(int j=0;j<len;j++)
{
exa[++tot].x = i;
exa[tot].y = j;
exa[tot].flow = cs[j] - '0';
}
}
char mp[110][110];
int s = 0, t = tot * 2 + 1;
for(int i=0;i<nn;i++)
{
scanf("%s", mp[i]);
}
int sum = 0;
for(int i=1;i<=tot;i++)
{
if(exa[i].flow>0)
{
add(i, i + tot, exa[i].flow);
if (mp[exa[i].x][exa[i].y] == 'L')
{
sum++;
add(s, i, 1);
}
if (exa[i].x < dd || exa[i].y < dd || (nn - exa[i].x) <= dd || (len - exa[i].y) <= dd) add(i + tot, t, inf);
for(int j=1;j<=tot;j++)
{
if (i == j) continue;
int dis = (exa[i].x - exa[j].x)*(exa[i].x - exa[j].x) + (exa[i].y - exa[j].y)*(exa[i].y - exa[j].y);
if (exa[j].flow&&dis <= dd*dd)
{
add(i+tot, j, inf);
}
}
}
}
int ans = Maxflow(s, t);
sum = sum - ans;
if (sum == 0) printf("Case #%d: no lizard was left behind.\n",++cas);
else if (sum == 1) printf("Case #%d: 1 lizard was left behind.\n",++cas);
else printf("Case #%d: %d lizards were left behind.\n",++cas,sum);
}
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 5e5 + 10;
struct edge
{
int u, v, c, f;
edge(int u, int v, int c, int f) :u(u), v(v), c(c), f(f) {}
};
vector<edge>e;
vector<int>G[maxn];
int level[maxn];//BFS分层,表示每个点的层数
int iter[maxn];//当前弧优化
int m;
void init()
{
for (int i = 0; i <= maxn; i++)G[i].clear();
e.clear();
}
void add(int u, int v, int c)
{
e.push_back(edge(u, v, c, 0));
e.push_back(edge(v, u, 0, 0));
m = e.size();
G[u].push_back(m - 2);
G[v].push_back(m - 1);
}
void BFS(int s)//预处理出level数组
//直接BFS到每个点
{
memset(level, -1, sizeof(level));
queue<int>q;
level[s] = 0;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v = 0; v < G[u].size(); v++)
{
edge& now = e[G[u][v]];
if (now.c > now.f && level[now.v] < 0)
{
level[now.v] = level[u] + 1;
q.push(now.v);
}
}
}
}
int dfs(int u, int t, int f)//DFS寻找增广路
{
if (u == t)return f;//已经到达源点,返回流量f
for (int &v = iter[u]; v < G[u].size(); v++)
//这里用iter数组表示每个点目前的弧,这是为了防止在一次寻找增广路的时候,对一些边多次遍历
//在每次找增广路的时候,数组要清空
{
edge &now = e[G[u][v]];
if (now.c - now.f > 0 && level[u] < level[now.v])
//now.c - now.f > 0表示这条路还未满
//level[u] < level[now.v]表示这条路是最短路,一定到达下一层,这就是Dinic算法的思想
{
int d = dfs(now.v, t, min(f, now.c - now.f));
if (d > 0)
{
now.f += d;//正向边流量加d
e[G[u][v] ^ 1].f -= d;
//反向边减d,此处在存储边的时候两条反向边可以通过^操作直接找到
return d;
}
}
}
return 0;
}
int Maxflow(int s, int t)
{
int flow = 0;
for (;;)
{
BFS(s);
if (level[t] < 0)return flow;//残余网络中到达不了t,增广路不存在
memset(iter, 0, sizeof(iter));//清空当前弧数组
int f;//记录增广路的可增加的流量
while ((f = dfs(s, t, INF)) > 0)
{
flow += f;
}
}
return flow;
}
struct node
{
int x, y, flow;
}exa[maxn];
int main()
{
int qaq, cas = 0;
scanf("%d", &qaq);
while (qaq--)
{
init();
int nn, dd, tot = 0, len;
char cs[110];
scanf("%d%d", &nn, &dd);
for (int i = 1; i <= nn; i++)
{
scanf("%s", cs+1);
len = strlen(cs+1);//这个地方要注意一下,就是因为这个wa了几发,这个地方不可以写strlen(cs)-1
for (int j = 1; j <= len; j++)
{
exa[++tot].x = i;
exa[tot].y = j;
exa[tot].flow = cs[j] - '0';
}
}
char mp[110][110];
int s = 0, t = tot * 2 + 1;
for (int i = 1; i <= nn; i++)
{
scanf("%s", mp[i]+1);
}
int sum = 0;
for (int i = 1; i <= tot; i++)
{
if (exa[i].flow > 0)
{
add(i, i + tot, exa[i].flow);
if (mp[exa[i].x][exa[i].y] == 'L')
{
sum++;
add(s, i, 1);
}
if (exa[i].x <= dd || exa[i].y <= dd || (nn - exa[i].x) < dd || (len - exa[i].y) < dd) add(i + tot, t, inf);
for (int j = 1; j <= tot; j++)
{
if (i == j) continue;
int dis = (exa[i].x - exa[j].x)*(exa[i].x - exa[j].x) + (exa[i].y - exa[j].y)*(exa[i].y - exa[j].y);
if (exa[j].flow&&dis <= dd * dd)
{
add(i + tot, j, inf);
}
}
}
}
int ans = Maxflow(s, t);
sum = sum - ans;
if (sum == 0) printf("Case #%d: no lizard was left behind.\n", ++cas);
else if (sum == 1) printf("Case #%d: 1 lizard was left behind.\n", ++cas);
else printf("Case #%d: %d lizards were left behind.\n", ++cas, sum);
}
return 0;
}