题目链接:
https://www.lydsy.com/JudgeOnline/problem.php?id=1001
题目大意:
见链接
思路:
求最小割,平面图的最小割等价于对偶图的最短路
直接建图求最短路即可,只是图比较难建。
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
#define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Mem(a) memset(a, 0, sizeof(a))
#define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
#define MID(l, r) ((l) + ((r) - (l)) / 2)
#define lson ((o)<<1)
#define rson ((o)<<1|1)
#pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} typedef long long ll;
const int maxn = + ;
const int mod = ;//const引用更快,宏定义也更快
const int INF = 1e9; struct edge
{
int next;//指向下一个节点
int u, v, w;
};
edge a[maxn];
int head[maxn], node;//node记录节点的数目,head[i]记录连接着i的第一条边
void addedge(int u, int v, int w)
{
a[node].u = u;
a[node].v = v;
a[node].w = w;
a[node].next = head[u];
head[u] = node++;
a[node].u = v;
a[node].v = u;
a[node].w = w;
a[node].next = head[v];
head[v] = node++;
}
struct Heapnode
{
int d, u;//d为距离,u为起点
Heapnode(){}
Heapnode(int d, int u):d(d), u(u){}
bool operator <(const Heapnode & a)const
{
return d > a.d;//这样优先队列先取出d小的
}
};
ll n, m;
bool v[maxn];//标记点是否加入集合
int d[maxn];//起点s到各个点的最短路
void init(int n)
{
node = ;
memset(head, -, sizeof(head));
}
priority_queue<Heapnode>q;
void dijkstra(ll s, ll n)//以s为起点
{
while(!q.empty())q.pop();
for(int i = ; i <= n; i++)d[i] = INF;
d[s] = ;
memset(v, , sizeof(v));
q.push(Heapnode(, s));
while(!q.empty())
{
Heapnode now = q.top();
q.pop();
ll u = now.u;//当前起点
if(v[u])continue;//如果已经加入集合,continue
v[u] = ;
for(int i = head[u]; i != -; i = a[i].next)
{
edge& e = a[i];//引用节省代码
if(d[e.v] > d[u] + e.w)
{
d[e.v] = d[u] + e.w;
q.push(Heapnode(d[e.v], e.v));
}
}
}
} int main()
{
ll n, m;
while(scanf("%lld%lld", &n, &m) != EOF)
{
if(n == || m == )
{
ll ans = INF, x;
for(int i = ; i < max(n, m); i++)
scanf("%lld", &x), ans = min(ans, x);
printf("%lld\n", ans);
continue;
}
ll x;
ll t = , s = (n - ) * (m - ) * + ;
init(s);
ll tmp = (m - ) * ;//每一行的数目
for(int i = ; i <= n; i++)
for(int j = ; j < m; j++)
{
scanf("%lld", &x);
if(i == )addedge(j * , t, x);
else if(i == n)addedge(s, (n - ) * tmp + * j - , x);
else addedge((i - ) * tmp + * j, (i - ) * tmp + * j - , x);
}
for(int i = ; i < n; i++)
for(int j = ; j <= m; j++)
{
scanf("%lld", &x);
if(j == )addedge(s, (i - ) * tmp + , x);
else if(j == m)addedge(i * tmp, t, x);
else addedge((i - ) * tmp + * j - , (i - ) * tmp + * j - , x);
}
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
{
scanf("%lld", &x);
addedge((i - ) * tmp + * j - , (i - ) * tmp + * j, x);
}
dijkstra(s, s);
printf("%d\n", d[t]);
}
return ;
}