[網路流 24 題] 方格取數問題 騎士共存問題
阿新 • • 發佈:2019-02-10
這兩道題都差不多,就放在一起寫了。兩道題的話原題就不貼過來了,只放一個題目連結。
方格取數問題的題意很明顯,互相連著的兩個數不能一起取走。這樣的話,我們將圖染成黑白兩種顏色,並建立一個二分圖,將一種顏色的點放在左側,另一種顏色的點放在右側。並各與源點、匯點連一條容量為該點的數值的邊。
將一個點與其相鄰的點用一條容量為INF的邊連線起來。
這樣我們就建立了一個二分圖,之後跑一邊最大流,之後怎麼辦呢,我們先看一下騎士共存問題。
騎士共存問題上來一看很難,騎士只要畫一下圖就可以發現,對對角線上的騎士是永遠都不會互相攻擊到的,所以我們只要像方格取數問題一樣,將所有點染色,然後連一下圖就可以了。這時所有的邊的容量都是1。
連圖具體的方案是列舉每個點,將該點和該店可以攻擊到的點連一條容量為1的邊。
之後求出該圖的最大流,我們知道對於一個二分圖,最大流就是該圖的最大匹配,也就是說我們求出了該圖的最大匹配。而對於每個匹配的兩個點,它們都是可以互相攻擊到的,所以我們只能取其中的一個,也就是說,用騎士的總數減去最大匹配的數量就可以得到答案了,其實這也就是二分圖的最大獨立集。
還要注意的就是,要把有障礙的點排除一下。
我們再回頭來看方格取數問題,這一題每個格子上都有自己的權值,而我們用了容量來代表這個權值。容易發現,對於一個“匹配”, 通過他的流量是兩個點中容量的最小的值,這樣的話,我們就可以用所有數字的和減去最大流,就可以得出解了。
這兩道題還要注意的是空間是否開夠。
程式碼:
方格取數問題
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000;
const int INF = 1<<29;
int n, m, s, t, sum=0;
int tot=1, front[MAXN];
int cur[MAXN], layer[MAXN];
struct tEdge
{
int v, next, c, f;
inline void addEdge(int tmpu, int tmpv, int tmpc)
{
c = tmpc; v = tmpv; f = 0;
next = front[tmpu];
front[tmpu] = tot;
//printf("u:%d, v:%d, c:%d\n", tmpu, v, c);
}
} e[MAXN*MAXN*2];
bool bfs()
{
queue <int> q;
memset(layer, 0, sizeof(layer));
layer[s] = 1; q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i=front[u]; i>0; i=e[i].next)
{
int v = e[i].v, maxflow = e[i].c - e[i].f;
if(maxflow <= 0) continue;
if(layer[v] != 0) continue;
layer[v] = layer[u] + 1;
q.push(v);
if(v == t) return true;
}
}
//printf("layer[t]:%d\n", layer[t]);
return false;
}
int dfs(int u, int curflow)
{
if(u == t || curflow == 0) return curflow;
int flow = 0;
for(int &i=cur[u]; i>0; i=e[i].next)
{
int v = e[i].v, maxflow = e[i].c - e[i].f;
if(maxflow <= 0) continue;
if(layer[v] != layer[u] + 1) continue;
int nowflow = dfs(v, min(curflow, maxflow));
flow += nowflow;
curflow -= nowflow;
e[i].f += nowflow;
e[i^1].f -= nowflow;
if(curflow == 0) break;
}
return flow;
}
int dinic()
{
int flow = 0;
while(bfs() == true)
{
//printf("ok");
for(int i=1; i<=m*n+2; i++) cur[i] = front[i];
flow += dfs(s, INF);
//printf("flow:%d\n", flow);
}
return flow;
}
int main()
{
scanf("%d%d", &m, &n);
s = m*n+1, t = m*n+2;
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
int tmp, now=(i-1)*n+j;
scanf("%d", &tmp); sum += tmp;
if((i+j)%2 == 0)
{
e[++tot].addEdge(s, now, tmp);
e[++tot].addEdge(now, s, 0);
if(i-1 >= 1)
{
e[++tot].addEdge(now, (i-2)*n+j, INF);
e[++tot].addEdge((i-2)*n+j, now, 0);
}
if(i+1 <= m)
{
e[++tot].addEdge(now, i*n+j, INF);
e[++tot].addEdge(i*n+j, now, 0);
}
if(j-1 >= 1)
{
e[++tot].addEdge(now, now-1, INF);
e[++tot].addEdge(now-1, now, 0);
}
if(j+1 <= n)
{
e[++tot].addEdge(now, now+1, INF);
e[++tot].addEdge(now+1, now, 0);
}
}
else
{
e[++tot].addEdge(now, t, tmp);
e[++tot].addEdge(t, now, 0);
}
}
}
int flow = dinic();
printf("%d\n", sum - flow);
return 0;
}
騎士共存問題
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50000;
const int INF = 1<<30;
int n, m, s, t, sum=0;
int tot=1, front[MAXN];
int cur[MAXN], layer[MAXN];
bool block[500][500];
int dx[8] = {1, 2, 1, 2, -1, -2, -1, -2};
int dy[8] = {2, 1, -2, -1, 2, 1, -2, -1};
struct tEdge
{
int v, next, c, f;
inline void addEdge(int tmpu, int tmpv, int tmpc)
{
c = tmpc; f = 0;
next = front[tmpu];
front[tmpu] = tot;
v = tmpv;
}
} e[5000000];
void add(int tmpu, int tmpv, int tmpc)
{
e[++tot].addEdge(tmpu, tmpv, tmpc);
e[++tot].addEdge(tmpv, tmpu, 0);
//printf("u:%d, v:%d, c:%d\n", tmpu, tmpv, tmpc);
}
bool bfs()
{
queue <int> q;
memset(layer, 0, sizeof(layer));
layer[s] = 1; q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i=front[u]; i>0; i=e[i].next)
{
int v = e[i].v, maxflow = e[i].c - e[i].f;
if(maxflow <= 0) continue;
if(layer[v] != 0) continue;
layer[v] = layer[u] + 1;
q.push(v);
if(v == t) return true;
}
}
return false;
}
int dfs(int u, int curflow)
{
if(u == t || curflow == 0) return curflow;
int flow = 0;
for(int &i=cur[u]; i>0; i=e[i].next)
{
int v = e[i].v, maxflow = e[i].c - e[i].f;
if(maxflow <= 0) continue;
if(layer[v] != layer[u] + 1) continue;
int nowflow = dfs(v, min(curflow, maxflow));
curflow -= nowflow;
e[i].f += nowflow;
e[i^1].f -= nowflow;
flow += nowflow;
if(curflow == 0) break;
}
return flow;
}
int dinic()
{
int flow = 0;
while(bfs() == true)
{
for(int i=1; i<=n*n+2; i++) cur[i] = front[i];
flow += dfs(s, INF);
//printf("flow:%d\n", flow);
}
return flow;
}
int main()
{
scanf("%d%d", &n, &m);
s = n*n +1; t = n*n +2;
for(int i=1; i<=m; i++)
{
int tmp1, tmp2;
scanf("%d%d", &tmp1, &tmp2);
block[tmp1][tmp2] = true;
}
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(block[i][j] == true) continue;
int now = (i-1)*n+j; sum ++;
if((i+j) %2 == 0)
{
add(s, now, 1);
for(int k=0; k<8; k++)
{
int x = i+dx[k], y = j+dy[k];
if(x<1 || y<1 || x>n || y>n) continue;
if(block[x][y] == true) continue;
int tmp = (x-1)*n+y;
add(now, tmp, 1);
}
}
else add(now, t, 1);
}
}
int flow = dinic();
printf("%d\n", sum - flow);
return 0;
}