传送门:QAQ
题意:就是给你一个二维平面图,然后图上有若干个整数点,问你是否可能每个点都到达边界并且图上任意一点只能被一个点经过且经过一次。
思路:因为每个点只能经过一次,所以我们将每个点拆成两个点,一个点是接受的,一个点是流出的,然后两个点之间的流量为1(注意边界点也要处理(这里错了好几次)),这样就能处理了。然后将源点与所有起点相连,然后就可以去跑最大流判断是否可以逃出。
附上代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long LL;
const int maxn = 6100;
struct pon {
int x;
int y;
};
pon point[3000];
int n, m, k;
struct Edge {
Edge() {}
Edge(int from, int to, int cap, int flow) :from(from), to(to), cap(cap), flow(flow) {}
int from, to, cap, flow;
};
struct Dinic {
int n, m, s, t;
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[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) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
bool BFS() {
memset(vis, 0, sizeof(vis));
queue<int>Q;
Q.push(s);
d[s] = 0;
vis[s] = true;
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
d[e.to] = d[x] + 1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a) {
if (x == t || a == 0) return a;
int flow = 0, f;
for (int& i = cur[x]; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int Maxflow(int s, int t) {
this->s = s, this->t = t;
int flow = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
flow += DFS(s, inf);
}
return flow;
}
}DC;
int s, tt,t;
void build() {
DC.init(6000);
s = 0;
for (int i = 0; i < k; i++) {
DC.AddEdge(s, (point[i].y-1)*n + point[i].x,1);
}
tt = s + 2*n*m + 1;
t = s + n*m + 1;
for (int i = 1; i <= n; i++) {
for (int z = 1; z <= m; z++) {
DC.AddEdge((z - 1)*n + i, t, 1);
if (z == 1 || z == m) {
DC.AddEdge(t, tt, 1);
}
else if (i == 1 || i == n) {
DC.AddEdge(t, tt, 1);
}
if (z - 1 >= 1) {
DC.AddEdge(t, (z - 2)*n + i, 1);
}
if (z + 1 <= m) {
DC.AddEdge(t, (z)*n + i, 1);
}
if (i - 1 >= 1) {
DC.AddEdge(t, (z - 1)*n + i-1, 1);
}
if (i + 1 <= n) {
DC.AddEdge(t, (z - 1)*n + i+1, 1);
}
t++;
}
}
}
int main(void) {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; i++) {
scanf("%d%d", &point[i].x, &point[i].y);
}
build();
if (DC.Maxflow(s, tt) == k) printf("possible\n");
else printf("not possible\n");
}
return 0;
}