[网络流dinic]日常翻新

之前的排版简直辣眼睛,重写一遍好了

模板题是草地排水poj1273

网络流的基础思想就是瞎基本搜

但是搜要搜得有技巧,有特色

最简单的搜,无限深搜直到终点

稍微改进一下,宽搜先标号然后按层搜

再改进一下,把某些确定不再使用的点剔除

要点在于建立反向边给自己一个反悔的机会,用^1找到反向边

#include <stdio.h>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;
const int N=205;
const int M=405;
int m,n,s,last[N],level[N];
struct edge
{
    int v,w,n;
}e[M];
void add(int u,int v,int w)
{
    e[s]=(edge){v,w,last[u]},last[u]=s++;
    e[s]=(edge){u,0,last[v]},last[v]=s++;
}
int BFS()
{
    memset(level,-1,sizeof(level));
    queue<int>q;
    q.push(1);level[1]=0;
    while(!q.empty())
    {
        int t=q.front();q.pop();
        for (int i=last[t];~i;i=e[i].n)
        {
            if(e[i].w&&level[e[i].v]==-1)
                q.push(e[i].v),level[e[i].v]=level[t]+1;
        }
    }
    return level[n]!=-1;
}
int DFS(int a,int b)
{
    if (a==n) return b;
    int r=0;
    for (int i=last[a];~i&&b>r;i=e[i].n)
    {
        if (level[a]+1==level[e[i].v]&&e[i].w)
        {
            int t=DFS(e[i].v,min(b-r,e[i].w));
            e[i].w-=t;e[i^1].w+=t;r+=t;
        }
    }
    if (!r) level[a]=-1;
    return r;
}
int dinic()
{
    int ans=0,tmp;
    while(BFS())while(tmp=DFS(1,0x3fffffff))ans+=tmp;
    return ans;
}
int main()
{
    while(scanf ("%d %d",&m,&n)!=EOF)
{
    memset (last,-1,sizeof(last));
        s=0;
    for (int i=1;i<=m;i++)
    {
        int u,v,w;
        scanf ("%d %d %d",&u,&v,&w);
        add(u,v,w);
        //add(v,u,0);
    }
    printf ("%d\n",dinic());
}
}

就是这样,喵~



最后一行献给可爱的男孩子们



[网络流dinic]日常翻新

时间: 2024-08-13 16:13:12

[网络流dinic]日常翻新的相关文章

POJ 1273 Drainage Ditches (网络流Dinic模板)

Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage

hdu2732 Leapin&#39; Lizards (网络流dinic)

D - Leapin' Lizards Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasur

poj 1149 PIGS(网络流dinic)

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16054   Accepted: 7185 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

hdu 3572 Task Schedule(网络流 dinic算法)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1197 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

网络流Dinic(本篇介绍最大流)

前言:看到网上Dinic和ISAP的比较,多数人认为ISAP更快,不容易爆栈.当然,也有少数人认为,在多数情况下,Dinic比较稳定.我认为Dinic的思路比ISAP更简明,所以选择了Dinc算法 介绍:Dinic算法本身,自然是解决最大流(普通最大流,最大流最小割)的算法.通过处理,也可以解决二分图的最大匹配(下文介绍),最大权闭合图. 算法介绍:介绍Dinic之前,我们先介绍一下最大流.在最大流的题目中,图被称为"网络",每条边的边权被称作"流量",有一个起点(

网络流 dinic

1 struct Edge { 2 int v, w; 3 int next; 4 }edge[250*250]; 5 int head[50], tot; 6 void addedge(int u, int v, int w) { 7 edge[tot].v = v; 8 edge[tot].w = w; 9 edge[tot].next = head[u]; 10 head[u] = tot++; 11 } 12 int lvl[305]; 13 bool bfs(int s, int t)

POJ 3469(Dual Core CPU-最小割)[Template:网络流dinic V2]

Language: Default Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 19321   Accepted: 8372 Case Time Limit: 5000MS Description As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Offi

网络流dinic模板

#include <iostream> #include <cstdlib> #include <stdio.h> #include <map> #include <cstring> #include <math.h> #include <stdlib.h> #define ll long long #define INF 0x3f3f3f3f #define cle(a) memset(a,0,sizeof(a)) us

POJ 3281(Dining-网络流拆点)[Template:网络流dinic]

Language: Default Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9631   Accepted: 4446 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John