洛谷P3381——费用流模板题

嗯。。随便刷了一道费用流的模板题。。。。来练练手。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int h[5210],d[5210],used[5210],que[100010],last[5210];
int k=1,INF=0x7fffffff,ans1=0,ans2=0;
inline int read(){
    int t=1,num=0;
    char c=getchar();
    while(c>‘9‘||c<‘0‘){if(c==‘-‘)t=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){num=num*10+c-‘0‘;c=getchar();}
    return num*t;
}
struct edge{
    int to,cap,cost,next;
}g[120010];
void add(int f,int t,int c1,int c2){
    g[++k].next=h[f];h[f]=k;g[k].to=t;g[k].cap=c1;g[k].cost=c2;
    g[++k].next=h[t];h[t]=k;g[k].to=f;g[k].cap=0;g[k].cost=-c2;
}
bool spfa(int s,int t){
    memset(last,0,sizeof(last));
    memset(d,127/3,sizeof(d));INF=d[0];
    memset(used,0,sizeof(used));
    int tail,head;
    head=tail=50002;
    que[head]=s;used[s]=1;d[s]=0;
    while(head>=tail){
        int x=que[tail++];
        for(int i=h[x];i;i=g[i].next){
            if(g[i].cap&&d[x]+g[i].cost<d[g[i].to]){
                d[g[i].to]=d[x]+g[i].cost;
                last[g[i].to]=i;
                if(!used[g[i].to]){
                    if(d[g[i].to]<d[que[tail]])que[--tail]=g[i].to;
                    else que[++head]=g[i].to;
                    used[g[i].to]=1;
                }
            }
        }
        used[x]=0;
    }
    return d[t]!=INF;
}
void mcf(int t){
    int minn=INF;
    for(int i=last[t];i;i=last[g[i^1].to])minn=min(minn,g[i].cap);
    ans1+=minn;
    for(int i=last[t];i;i=last[g[i^1].to]){
        ans2+=g[i].cost*minn;
        g[i].cap-=minn;
        g[i^1].cap+=minn;
    }
}
int main()
{
    int n,m,s,t;
    n=read();m=read();s=read();t=read();
    for(int i=1;i<=m;i++){
        int x,y,w,f;
        x=read();y=read();w=read();f=read();
        add(x,y,w,f);
    }
    while(spfa(s,t))mcf(t);
    printf("%d %d",ans1,ans2);
    return 0;
}

本文由Yzyet编写,网址为www.cnblogs.com/Yzyet。非Yzyet同意,禁止转载,侵权者必究。

时间: 2024-08-03 07:10:01

洛谷P3381——费用流模板题的相关文章

POJ2135 最小费用最大流模板题

练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include <cstring> #define MAXN 1005 #define MAXM 10005 #define INF 0x3f3f3f3f struct Edge { int y,c,w,ne;//c容量 w费用 }e[MAXM*4]; int n,m,x,y,w; int s,t,Maxflow

UVa 12534 Binary Matrix 2 zkw费用流模版题

题目链接:点击打开链接 思路: 我们首先假设这个图都是全0的 用n个点代表行,m个点代表列 用源点向行连一个值x 表示每行1的个数,向列连一个y表示每列y个1 则若行i和列j之间流过一个流量就表示 (i,j) 点填了1 那么若原来图中(i,j)点为0 则花费就是1 若原图中(i,j)点是1,则花费是-1 如此枚举x跑个费用流就好了 ==居然把我多年的白书费用流坑掉了... zkw走起啊 #include <stdio.h> #include <string.h> #include

hdu 3549 Flow Problem(最大流模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input

POJ 3686 The Windy&#39;s(思维+费用流好题)

The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5362   Accepted: 2249 Description The Windy's is a world famous toy factory that owns M top-class workshop to make toys. This year the manager receives N orders for toys. The ma

HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.只能走最短路 3.走过的点不能再走 问最大和. 对每个点拆点限流为1即可满足3. 费用流流量为2满足1 最大费用流,先给图取负,结果再取负,满足2 #include <stdio.h> #include <string.h> #include <iostream> #include <math.h> #include <queue> #include <set&

[ACM] hdu 3549 Flow Problem (最大流模板题)

Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input contains an integer T, denoting the nu

图论模板——最大流及费用流模板

图论模板--最大流及费用流模板 最大流--SAP 时间复杂度:O(v^2*e) const int MAXN=1010;//点数的最大值 const int MAXM=1010;//边数的最大值 const int INF=0x3f3f3f3f; struct Node { int from,to,next; int cap; }edge[MAXM]; int tol; int head[MAXN]; int dep[MAXN]; int gap[MAXN];//gap[x]=y :说明残留网络

[洛谷P3381]【模板】最小费用最大流

题目大意:给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 解题思路:最小费用最大流模板.虽说此题最后两个点的时限是1200ms,但我觉得耗时在1000ms以上很不爽,于是对代码进行全面优化.然而最后一个点仍然有1050+ms.最后逼我开大招(代码第一行),瞬间只有不到900ms,大爽. C++ Code: %:pragma GCC optimize("Ofast") #include<cstdio> #in

【网络流#2】hdu 1533 最小费用最大流模板题

嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(House)作为汇点,各个源点与汇点分别连一条边,这条边的流量是1(因为每个源点只能走一条边到汇点),费用是 从源点走到汇点的步数,因为有多个源点与汇点,要建一个超级源点与超级汇点,超级源点与各个源点连一条流量为1,费用为0(要避免产生多余的费用)的边 按照这个图跑一发费用流即可 把代码挂上去,用的是前向星写的 1 #include<cstdio> 2 #include<cstr