Luogu 3376 网络最大流

栏目: 数据库 · 发布时间: 7年前

内容简介:题目链接:思路:这道题数据范围较大,按照邻接矩阵的方法存边只能得70分。

题目链接: https://www.luogu.org/problemnew/show/P3376

思路:

这道题数据范围较大,按照邻接矩阵的方法存边只能得70分。

还是先考虑求解,只是更换存边方法。

由于本人对掌握不是很好,于是就没敢用存边,听说很方便?

其实邻接矩阵比较好写,在存反向边和增广方面。

用邻接表其实也不难。

对于反向边,我们边号需要从0开始,也就是与平常不同,。

然后就可以用异或来做,因为0^1=1,1^1=0,2^1=3,3^1=2,......

然后增光的时候循环稍有不同,看代码吧。

#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
static const int MAXN=20050;
static const int MAXM=200050;
static const int INF=1<<30;
using namespace std;
struct Edge{
    int from,to,val,nextt;
}edge[MAXM];
int n,m,s,t,u,v,w,num,maxflow,head[MAXN],res[MAXN],father[MAXN];
queue<int> q;
inline int read(){
    int x=0;bool sign=false;
    char alpha=0;
    while(!isdigit(alpha)) sign|=alpha=='-',alpha=getchar();
    while(isdigit(alpha)) x=(x<<1)+(x<<3)+(alpha^48),alpha=getchar();
    return sign?-x:x;
}
inline void addedge(int u,int v,int w){
    edge[num].from=u;
    edge[num].to=v;
    edge[num].val=w;
    edge[num].nextt=head[u];
    head[u]=num++;
}
inline bool bfs(int s,int t){
    while(!q.empty()) q.pop();
    memset(res,0,sizeof(res));
    q.push(s);
    res[s]=INF;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=head[x];i;i=edge[i].nextt){
            int to=edge[i].to;
            if(res[to]==0&&edge[i].val){
                father[to]=i;
                res[to]=min(res[x],edge[i].val);
                q.push(to); 
            }
        }
        if(res[t]) return true;
    }
    return false;
}
inline int EK(int s,int t){
    while(1){
        if(!bfs(s,t)) break;
        for(int i=t;i!=s;i=edge[father[i]].from){
            edge[father[i]].val-=res[t];
            edge[father[i]^1].val+=res[t];
        }
        maxflow+=res[t];
    }
    return maxflow;
}
int main(){
    n=read();m=read();s=read();t=read();
    for(int i=1;i<=m;i++){
        u=read();v=read();w=read();
        addedge(u,v,w);
        addedge(v,u,0);
    }
    printf("%d\n",EK(s,t));
    return 0;
}

之后会更新算法。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

反欺骗的艺术

反欺骗的艺术

(美) 米特尼克(Mitnick, K. D.) / 潘爱民 / 清华大学出版社 / 2014-7-1 / 49.80元

凯文•米特尼克(Kevin D. Mitnick)曾经是历史上最令FBI头痛的计算机顽徒之一,现在他已经完成了大量的文章、图书、影片和记录文件。自从2000年从联邦监狱中获释以来,米特尼克改变了他的生活方式,成了全球广受欢迎的计算机安全专家之一。在他的首部将功补过的作品中,这位全世界最著名的黑客为“放下屠刀,立地成佛”这句佛语赋予了新的含义。 在《反欺骗的艺术——世界传奇黑客的经历分享》中,......一起来看看 《反欺骗的艺术》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试