平面图,求从(0,0)到(m-1,n-1)的最大流/最小割
本模型关键在于建图,不同题目的建图方法不同,建图原则如下:
每个平面图G都有一个与它对偶的平面图G',满足以下性质:
1.G'中的每个顶点对应于G中的每个面
2.对于G中的每条边e
1)e在G中属于两个面f1,f2,在G'中加入边f1'-f2'
2)e在G中属于一个面f,在G’中加入自环边f1'-f1'
G与G'的关系:
1.G'中的每个面对应于G中的每个点
2.G'中的每条边对应于G中的每条边
3.G'中的每个环对应于G中的每个割
由关系3可以得到以下方法求原图s->t的最大流:
1.在G中连接s-t,无界面被分成了有界面s'和无界面t'
2.求改造后G图的对偶图G',删除G'中的边s'-t'
3.G’中求s'->t'的最短路,最短路的长度就是G'中s->t的最小割,即最大流
注:本题数据范围很大,数组开到了千万,=.=,不然会RE。。。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#define ll long long
#define sf scanf
#define pf printf
#define INF 1<<29
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
#define maxn 20000100
const ll mol=1000000007;
using namespace std;
int n,m,st,ed,base;
int head[maxn],vis[maxn],d[maxn],tot;
struct Edge{
int to,next,w;
}edge[maxn];
struct Heapnode{
int u,d;
bool operator<(const Heapnode &rhs)const{
return d>rhs.d;
}
};
int down(int i,int j){ return (i-1)*(m-1)+base+j; }
int up(int i,int j) { return (i-1)*(m-1)+j; }
void add(int u,int v,int w){
edge[tot].to=v,edge[tot].next=head[u],edge[tot].w=w,head[u]=tot++;
edge[tot].to=u,edge[tot].next=head[v],edge[tot].w=w,head[v]=tot++;
}
void build(){
int tmp;
for(int i=0;i<=ed;i++) head[i]=-1;
tot=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m-1;j++){
scanf("%d",&tmp);
if(i==1) add(up(1,j),ed,tmp);
else if(i==n) add(down(n-1,j),st,tmp);
else add(down(i-1,j),up(i,j),tmp);
}
}
for(int i=1;i<=n-1;i++){
for(int j=1;j<=m;j++){
scanf("%d",&tmp);
if(j==1) add(down(i,1),st,tmp);
else if(j==m) add(up(i,m-1),ed,tmp);
else add(up(i,j-1),down(i,j),tmp);
}
}
for(int i=1;i<=n-1;i++){
for(int j=1;j<=m-1;j++){
scanf("%d",&tmp);
add(up(i,j),down(i,j),tmp);
}
}
}
void dij(){
priority_queue<Heapnode> que;
for(int i=0;i<=ed;i++) { d[i]=(i==0)?0:INF; vis[i]=0; }
que.push((Heapnode){0,0});
while(!que.empty()){
Heapnode x=que.top();que.pop();
int u=x.u;
if(vis[u]) continue;
vis[u]=1;
if(u==ed) break;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to,w=edge[i].w;
if(d[v]>d[u]+w) { d[v]=d[u]+w;que.push((Heapnode){v,d[v]}); }
}
}
}
int main(){
//freopen("a.txt","r",stdin);
int T=0;
while(scanf("%d%d",&n,&m)!=EOF,(n&&m)){
st=0,ed=2*(n-1)*(m-1)+1,base=(n-1)*(m-1);
build();
dij();
printf("Case %d: Minimum = %d\n",++T,d[ed]);
}
}
本文探讨了如何求解平面图从(0,0)到(m-1,n-1)的最大流/最小割问题。通过建立平面图及其对偶图G',并利用对偶图的性质,可以将原图的最大流转化为求对偶图中s'到t'的最短路。此方法适用于数据范围大的情况,避免数组溢出导致错误。"
111897131,10541082,Python数据处理:MOOC课程答案解析,"['Python编程', '数据科学', '摄影技术', '图像处理']

1312

被折叠的 条评论
为什么被折叠?



