zoukankan      html  css  js  c++  java
  • 「LuoguP3381」【模板】最小费用最大流

    Description


    如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用。

    Input


    第一行包含四个正整数N、M、S、T,分别表示点的个数、有向边的个数、源点序号、汇点序号。

    接下来M行每行包含四个正整数ui、vi、wi、fi,表示第i条有向边从ui出发,到达vi,边权为wi(即该边最大流量为wi),单位流量的费用为fi。

    Output


    一行,包含两个整数,依次为最大流量和在最大流量情况下的最小费用。

    Sample Input


    4 5 4 3
    4 2 30 2
    4 3 20 3
    2 3 20 1
    2 1 30 9
    1 3 40 5

    Sample Output


    50 280

    说明


    时空限制:1000ms,128M

    (BYX:最后两个点改成了1200ms)

    数据规模:

    对于30%的数据:N<=10,M<=10

    对于70%的数据:N<=1000,M<=1000

    对于100%的数据:N<=5000,M<=50000

    样例说明:

    如图,最优方案如下:

    第一条流为4–>3,流量为20,费用为3*20=60。

    第二条流为4–>2–>3,流量为20,费用为(2+1)*20=60。

    第三条流为4–>2–>1–>3,流量为10,费用为(2+9+5)*10=160。

    故最大流量为50,在此状况下最小费用为60+60+160=280。

    故输出50 280。

    题解


    学费用流的模板题 选了zkw

    对着hzwer的模板敲了一遍网络流&费用流模板by hzwer

    其实跟dinic差不多 就是把bfs换成spfa 以单位费用为dis从源点跑 返回dis[t]是否能到达

    以及不知道为什么spfa要反着搜 正搜也A了2333

      1 #include<queue>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<iostream>
      5 using namespace std;
      6 #define R register
      7 const int INF=99999999;
      8 inline int read()
      9 {
     10     char ch=getchar();
     11     int s=1,x=0;
     12     while(ch<'0'||ch>'9'){if(ch=='-')s=-1;ch=getchar();}
     13     while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
     14     return s*x;
     15 }
     16 int n,m,s,t;
     17 struct emm{
     18     int e,f,v,c;
     19 }a[100007];
     20 int h[5007];
     21 int tot=1;
     22 void con(int x,int y,int vv,int cc)
     23 {
     24     a[++tot].f=h[x];
     25     h[x]=tot;
     26     a[tot].e=y;
     27     a[tot].v=vv;
     28     a[tot].c=cc;
     29     a[++tot].f=h[y];
     30     h[y]=tot;
     31     a[tot].e=x;
     32     a[tot].c=-cc;
     33     return;
     34 }
     35 void scan()
     36 {
     37     n=read(),m=read(),s=read(),t=read();
     38     for(R int i=1;i<=m;++i)
     39     {
     40         int x=read(),y=read(),v=read(),c=read();
     41         con(x,y,v,c);
     42     }
     43     return;
     44 }
     45 queue<int>q;
     46 bool sf[5007];
     47 int d[5007];
     48 bool spfa()
     49 {
     50     memset(sf,0,sizeof(sf));
     51     //memset(d,127,sizeof(d));
     52     for(int i=1;i<=n;++i)d[i]=INF;
     53     sf[s]=1;d[s]=0;q.push(s);
     54     while(!q.empty())
     55     {
     56         int x=q.front();q.pop();
     57         for(int i=h[x];i;i=a[i].f)
     58         if(d[x]+a[i].c<d[a[i].e]&&a[i].v)
     59         {
     60             d[a[i].e]=d[x]+a[i].c;
     61             if(!sf[a[i].e])
     62             {
     63                 sf[a[i].e]=1;
     64                 q.push(a[i].e);
     65             }
     66         }
     67         sf[x]=0;
     68     }
     69     //for(int i=1;i<=4;++i)
     70     //cout<<d[i]<<" ";
     71     //cout<<endl;
     72     return d[t]<INF;
     73 }
     74 int ans=0;
     75 int dfs(int x,int al)
     76 {
     77     sf[x]=1;
     78     if(x==t||!al)return al;
     79     int fl=0;
     80     for(int i=h[x];i;i=a[i].f)
     81     if(d[a[i].e]==d[x]+a[i].c&&a[i].v&&!sf[a[i].e])
     82     {
     83         int f=dfs(a[i].e,min(al,a[i].v));
     84         fl+=f;
     85         al-=f;
     86         ans+=f*a[i].c;
     87         a[i].v-=f;
     88         a[i^1].v+=f;
     89         if(!al)break;
     90     }
     91     if(!fl)d[x]=-1;
     92     return fl;
     93 }
     94 int now=0;
     95 void zkw()
     96 {
     97     while(spfa())
     98     {
     99         sf[t]=1;
    100         while(sf[t])
    101         {
    102             memset(sf,0,sizeof(sf));
    103             now+=dfs(s,INF);
    104         }
    105     }
    106     return;
    107 }
    108 int main()
    109 {
    110     scan();
    111     zkw();
    112     cout<<now<<" "<<ans;
    113     return 0;
    114 }

    csdn好丑啊 可是好看的lofter根本不是用来写博客的吧?!

    UPD

    现在是十一月了 我写了这么多次费用流都是正向跑的spfa 没出过一点问题qwq

  • 相关阅读:
    牛客题霸NC119题解
    牛客题霸NC105题解
    牛客题霸NC93题解
    牛客题霸NC88题解
    牛客题霸NC68题解
    牛客题霸NC45题解
    牛客题霸NC33题解
    牛客题霸NC15题解
    牛客题霸NC04题解
    牛客题霸反转链表题解
  • 原文地址:https://www.cnblogs.com/qwerta/p/9379761.html
Copyright © 2011-2022 走看看