LUOGU 4012 network flow problem deep-sea robot 24 questions

Copyright: https: //blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/91357969

title

LUOGU 4012
Title Description

Deep Ocean Resources Research Expedition of submarines will reach the deep seabed of scientific investigation.
More than in the deep-sea robot submarine. After reaching the deep seabed submarine, deep sea submarine robot will move away from the intended target.
Deep-sea robot on the move must also collect specimens of marine life along the way. Along the way biological specimens is done by first experiencing its deep-sea robot collection.
Value of biological specimens on each predetermined route is known, but can only be acquired once biological specimens.
This question can only be moved along a defined deepwater or north easterly direction from its starting position, and a plurality of deepwater may occupy the same position at the same time.
With a P × Q P\times Q mesh representation deepwater movable position. Southwest corner coordinates (0, 0), the northeast corner of coordinates (Q, P).
Here Insert Picture Description
Given the deep sea robot for each departure and destination locations, as well as the value of biological specimens, each of the grid edge.
After the optimal mobile computing solutions deep-sea robot, the deep-sea robot reaches its destination, the highest total value of collected biological specimens.

Input and output format
input format:

The first deep-sea robot behavior starting position of the file number a, and the destination number b.
P and Q values of the second row.
The next P + 1 lines, each Q has a positive integer representing the value of the biological specimen movement path on east, south adherence to the data line arranged to north.
The next row of Q + 1, each row of P has a positive integer representing the value of the biological specimen on a moving path of the north, the east to the west line data compliant to the arrangement.
A next row, each row having three positive integer k, x, y, k expressed from a deepwater (x, y) position coordinates.
B then the next row, each row having three positive integers r, x, y, r represents a deepwater has selectable (x, y) coordinates of the location as the destination.
a horizontal and vertical lines and the input lines b to turn the coordinates lie

Output formats:

The total value of the maximum output collected biological specimens.

Input Output Sample
Input Sample # 1:

1 1
2 2
1 2
3 4
5 6
7 2
8 10
9 3
2 0 0
2 2 2

Output Sample # 1:

42

Explanation

1 P , Q 15 1\leq P,Q\leq15
1 a 4 1 \ leq a \ leq 4
1 b 6 1 \ leq b \ leq 6

analysis

Some people say that this question is BZOJ 1001 wolf caught rabbits upgraded version, in my opinion, really, difficulty thinking is definitely up some, but its level of difficulty of a provincial election because it is estimated that more than half of language to describe.

We even between any two lattice two sides: one side capacity 1 1 , the price for the Weight , another capacity I N F INF , The cost of 0 0

Then for each starting point, from s Source s and even a capacity k i k_i Side. End empathy.

Oh, no, it somewhat routine, but because of his language to describe, and made me question this coordinate description of an ignorant, half a day to read really is disgusting, or else do not want to be so Tucao .

code

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10,inf=0xcfcfcfcf,INF=0x3f3f3f3f;

char buf[1<<15],*fs,*ft;
inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
template<typename T>inline void read(T &x)
{
    x=0;
    T f=1, ch=getchar();
    while (!isdigit(ch) && ch^'-') ch=getchar();
    if (ch=='-') f=-1, ch=getchar();
    while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
    x*=f;
}

template<typename T>inline void write(T x)
{
    if (!x) { putchar('0'); return ; }
    if (x<0) putchar('-'), x=-x;
    T num=0, ch[20];
    while (x) ch[++num]=x%10+48, x/=10;
    while (num) putchar(ch[num--]);
}

int ver[maxn<<1],edge[maxn<<1],Next[maxn<<1],cost[maxn<<1],head[maxn],len=1;
inline void add(int x,int y,int z,int c)
{
    ver[++len]=y,edge[len]=z,cost[len]=c,Next[len]=head[x],head[x]=len;
    ver[++len]=x,edge[len]=0,cost[len]=-c,Next[len]=head[y],head[y]=len;
}

int s,t;
int dist[maxn],incf[maxn],pre[maxn];
bool vis[maxn];
inline bool spfa()
{
    memset(dist,0xcf,sizeof(dist));
    memset(vis,0,sizeof(vis));
    queue<int>q;q.push(s);
    dist[s]=0,vis[s]=1,incf[s]=1<<30;
    while (!q.empty())
    {
        int x=q.front();
        q.pop();
        vis[x]=0;
        for (int i=head[x]; i; i=Next[i])
        {
            if (!edge[i]) continue;
            int y=ver[i];
            if (dist[y]<dist[x]+cost[i])
            {
                dist[y]=dist[x]+cost[i];
                incf[y]=min(incf[x],edge[i]);
                pre[y]=i;
                if (!vis[y]) q.push(y),vis[y]=1;
            }
        }
    }
    if (dist[t]==inf) return false;
    else return true;
}

long long maxflow,ans;
inline void update()
{
    int x=t;
    while (x!=s)
    {
        int i=pre[x];
        edge[i]-=incf[t];
        edge[i^1]+=incf[t];
        x=ver[i^1];
    }
    maxflow+=incf[t];
    ans+=dist[t]*incf[t];
}

int main()
{
	int a,b,P,Q,id[16][16];
	read(a);read(b);read(P);read(Q);
    for (int cnt=0,i=0; i<=P; ++i)
        for (int j=0; j<=Q; ++j) id[i][j]=++cnt;
    s=(P+1)*(Q+1)+1,t=s+1;
    for (int i=0; i<=P; ++i)
        for (int j=0,x; j<Q; ++j) read(x),add(id[i][j],id[i][j+1],1,x),add(id[i][j],id[i][j+1],INF,0);

    for (int j=0; j<=Q; ++j)
        for (int i=0,x; i<P; ++i) read(x),add(id[i][j],id[i+1][j],1,x),add(id[i][j],id[i+1][j],INF,0);

    for (int k,x,y; a; --a) read(k),read(x),read(y),add(s,id[x][y],k,0);
    for (int r,x,y; b; --b) read(r),read(x),read(y),add(id[x][y],t,r,0);

    while (spfa()) update();
    write(ans),puts("");
	return 0;
}

Guess you like

Origin blog.csdn.net/huashuimu2003/article/details/91357969