好了对于这么水的题我表示只是来存代码的(逃

注意读入建图的时候的反向边标记然后大力Dinic就好了w

GitHub

 #include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> const int MAXV=;
const int MAXE=;
const int INF=0x7FFFFFFF; struct Edge{
int from;
int to;
int flow;
Edge* rev;
Edge* next;
};
Edge E[MAXE];
Edge* head[MAXV];
Edge* top=E; int v;
int depth[MAXV];
int flow[MAXV][MAXV]; int Dinic(int,int);
void Initialize();
void Insert(int,int);
int DFS(int,int,int);
bool BFS(int,int); int main(){
freopen("maxflowa.in","r",stdin);
freopen("maxflowa.out","w",stdout);
Initialize();
printf("%d\n",Dinic(,v));
return ;
} int Dinic(int s,int t){
int ans=;
while(BFS(s,t)){
ans+=DFS(s,INF,t);
}
return ans;
} int DFS(int s,int flow,int t){
if(s==t||flow==)
return flow;
int tmp=flow;
int k;
for(Edge* i=head[s];i!=NULL;i=i->next){
if(i->flow!=&&tmp!=&&depth[i->to]==depth[s]+){
k=DFS(i->to,std::min(tmp,i->flow),t);
if(k==){
depth[i->to]=;
continue;
}
tmp-=k;
i->flow-=k;
i->rev->flow+=k;
if(tmp==)
break;
}
}
return flow-tmp;
} bool BFS(int s,int t){
memset(depth,,sizeof(depth));
std::queue<int> q;
depth[s]=;
q.push(s);
while(!q.empty()){
s=q.front();
q.pop();
for(Edge* i=head[s];i!=NULL;i=i->next){
if(depth[i->to]==&&i->flow!=){
q.push(i->to);
depth[i->to]=depth[s]+;
if(i->to==t)
return true;
}
}
}
return false;
} void Initialize(){
scanf("%d",&v);
for(int i=;i<=v;i++){
for(int j=;j<=v;j++){
scanf("%d",&flow[i][j]);
}
}
for(int i=;i<=v;i++){
for(int j=;j<i;j++){
Insert(i,j);
}
}
} inline void Insert(int a,int b){
top->from=a;
top->to=b;
top->flow=flow[a][b];
top->next=head[a];
head[a]=top;
top->rev=top+;
top++;
top->from=b;
top->to=a;
top->flow=flow[b][a];
top->next=head[b];
head[b]=top;
top->rev=top-;
top++;
}

Backup

[COGS 0011] 运输问题1-LMLPHP

05-08 15:29