Luogu4474 [Excalibur] - Network Flow

Title Description

https://cdn.luogu.com.cn/upload/pic/17920.png
This is Al-Astoria Pendragon before things become souls, she was going to pull out the sword in the stone to become King Arthur, before that she was going to collect some gems.
Gem n * m are arranged in a grid, each grid has a value of gem v (i, j), and Al · Anatolia Pendragon can choose their starting point.
Start time is 0 seconds. Following operations, performed in sequence per second

  1. When i first started second, Al Anatolia · Pendragon on the grid (x, y), she can take away (x, y) in the gem.
  2. In even second, Anatolia around Al-Pendragon four-frame gems will disappear
  3. If Al Astoria Pande Aragon beginning on the i-th second grid (x, y), i + 1 at the first seconds can be immediately moved to (x + 1, y), (x, y + 1 ), (x-1, y) or (x, y-1), can remain in the (x, y) on.

Al-seeking Anatolia Pendragon up how much the value of gems

Input Format

The first line gives the number N, M represents the number of ranks .N, M is less than or equal to 100, the value does not exceed 10000. The following gem N rows and M columns for describing the digital matrix

Output Format

How much output can get the most value gems

Sample input and output

Input # 1
2 2
1 2
2 1
Output # 1
4

Description / Tips

Yao Jinyu original title.

Code:

include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define R register
#define inf 1e9+7

using namespace std;
const int Excalibur = 10005;//N或MAXN
int n, m, S, T, cur[Excalibur], dep[Excalibur];
struct saber {//edge,我的王
    int nxt, to, v;
}rin[Excalibur<<3];//是凛哦
int Lancer[Excalibur], Fate = 1, Archar;//emmm,head和tot以及sum
int x[5] = {0, -1, 0, 1, 0}, y[5] = {0, 0, -1, 0, 1};

inline void add(int from, int to, int v) {
    rin[++Fate].v = v;
    rin[Fate].to = to;
    rin[Fate].nxt = Lancer[from];
    Lancer[from] = Fate;
}

inline bool bfs(int s, int t) {
    queue<int> Rider;//q
    for(R int i = 0;i <= T;++ i) cur[i] = Lancer[i], dep[i] = 0;
    Rider.push(s); dep[s] = 1;
    while(!Rider.empty()) {
        R int vi = Rider.front();
        Rider.pop();
        for(R int i = Lancer[vi]; i ;i = rin[i].nxt) {
            R int vc = rin[i].to;
            if(!dep[vc] && rin[i].v) {
                dep[vc] = dep[vi] + 1;
                Rider.push(vc);
            }
        }
    }
    return dep[t];
}

int dfs(int s, int t, int flow) {
    if(!flow || s == t) return flow;
    int Caster = 0, Assassin;//增量
    for(R int i = cur[s]; i ;i = rin[i].nxt) {
        R int vc = rin[i].to;
        cur[s] = i;
        if(dep[vc] == dep[s] + 1 && rin[i].v) {
            Assassin = dfs(vc, t, min(flow, rin[i].v));
            if(!Assassin) continue;
            Caster += Assassin; flow -= Assassin;
            rin[i].v -= Assassin; rin[i ^ 1].v += Assassin;
            if(!flow) return Caster;
        }
    }
    return Caster;
}

int Dinic() {
    int res = 0;
    while(bfs(S, T)) res += dfs(S, T, inf);
    return Archar - res;
}

int main() {
    scanf("%d%d",&n, &m);
    //以下是建图,中二到此结束
    S = 0, T = n * m + 1;
    for(R int i = 1;i <= n;++ i)
        for(R int j = 1;j <= m;++ j) {
            R int v; scanf("%d",&v);
            Archar += v;
            R int p = (i - 1) * m + j;
            if((i + j) & 1) { add(S, p, v);add(p, S, 0); }
            else { add(p, T, v);add(T, p, 0); }
        }
    for(R int i = 1;i <= n;++ i) for(R int j = 1;j <= m;++ j)
        if((i + j) & 1)
            for(R int k = 1;k <= 4;++ k) {
                R int f = i + x[k], g = j + y[k];
                if(f < 1 || f > n || g < 1 || g > m) continue;
                R int u = (i - 1) * m + j, v = (f - 1) * m + g;
                add(u, v, inf); add(v, u, 0);
            }
    printf("%d",Dinic());
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/yelir/p/11600231.html