BZOJ2007 [Noi2010]海拔 【平面图最小割转对偶图最短路】

时间:2021-03-27 07:00:43

题目链接

BZOJ2007

题解

这是裸题啊,,要是考试真的遇到就好了

明显是最小割,而且是有来回两个方向

那么原图所有向右的边转为对偶图向下的边

向左的边转为向上

向下转为向左

向上转为向右

然后跑一遍最短路即可

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<LL,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<LL,int>
#define LL long long int
#define id(x,y) (n * (x - 1) + y)
using namespace std;
const int maxn = 300005,maxm = 10000005;
const LL INF = 1000000000000000ll;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int h[maxn],ne;
struct EDGE{int to,nxt,w;}ed[maxm];
inline void build(int u,int v,int w){
ed[++ne] = (EDGE){v,h[u],w}; h[u] = ne;
}
int n,N,S,T,vis[maxn];
LL d[maxn];
priority_queue<cp,vector<cp>,greater<cp> > q;
void dijkstra(){
for (int i = 1; i <= N; i++) d[i] = INF; d[S] = 0;
q.push(mp(d[S],S));
int u;
while (!q.empty()){
u = q.top().second; q.pop();
if (vis[u]) continue;
vis[u] = true;
Redge(u) if (!vis[to = ed[k].to] && d[to] > d[u] + ed[k].w){
d[to] = d[u] + ed[k].w;
q.push(mp(d[to],to));
}
}
}
void readin(){
for (int i = 1; i <= n; i++) build(S,id(1,i),read());
for (int i = 1; i < n; i++){
for (int j = 1; j <= n; j++)
build(id(i,j),id(i + 1,j),read());
}
for (int i = 1; i <= n; i++) build(id(n,i),T,read()); for (int i = 1; i <= n; i++){
build(id(i,1),T,read());
for (int j = 1; j < n; j++)
build(id(i,j + 1),id(i,j),read());
build(S,id(i,n),read());
} for (int i = 1; i <= n; i++) build(id(1,i),S,read());
for (int i = 1; i < n; i++){
for (int j = 1; j <= n; j++)
build(id(i + 1,j),id(i,j),read());
}
for (int i = 1; i <= n; i++) build(T,id(n,i),read()); for (int i = 1; i <= n; i++){
build(T,id(i,1),read());
for (int j = 1; j < n; j++)
build(id(i,j),id(i,j + 1),read());
build(id(i,n),S,read());
}
}
int main(){
n = read(); N = n * n + 2; S = N - 1; T = N;
readin();
dijkstra();
printf("%lld\n",d[T]);
return 0;
}