题目链接 3381 【模板】最小费用最大流

手写堆版本 dijkstra   400+ms 看来优先队列的常数好大

#include<bits/stdc++.h>
using namespace std;
#define maxn 100001
#define inf 0x3f3f3f3f
#define pii pair<int,int >
inline int read()
{
    ;
    char c=getchar();
    ;
    ;    c=getchar();}
    )+(x<<)+c-';c=getchar();}
    return flag?-x:x;
}
struct ac{
  int to,va,cos,nex;
}eg[maxn*];
struct wa{
   int x,y;
   wa(){}
   wa(int a,int b){
     x=a,y=b;
   }
}aa[maxn*];
int head[maxn],dis[maxn],pre[maxn],flow[maxn],h[maxn];
bool fa[maxn];
,maxcost=,tot=,len=;

void init(){
  tot=;
  memset(eg,,sizeof(eg));
  memset(head,-,sizeof(head));
  memset(h,,sizeof(h));
  memset(pre,,sizeof(pre));
}

bool dcmp(wa A,wa B){
    ;
    ;
}

void add(int v){
    )return;
    ]))swap(aa[v],aa[v/]),add(v/);
}

void updata(int v){
    >len)return;
    ==len)
    {
        ],aa[v]))swap(aa[v],aa[v*]);
        return;
    }
    ])&&dcmp(aa[v],aa[v*+]))return;
    ],aa[v*+]))swap(aa[v],aa[v*]),updata(v*);
    +]),updata(v*+);
}
void add_eg(int u,int to,int va,int cos){
   eg[tot].va=va;
   eg[tot].cos=cos;
   eg[tot].to=to;
   eg[tot].nex=head[u];
   head[u]=tot++;

   eg[tot].va=;
   eg[tot].cos=-cos;
   eg[tot].to=u;
   eg[tot].nex=head[to];
   head[to]=tot++;
}
bool dijstra(int s,int t){
    memset(dis,inf,sizeof(dis));
    dis[s]=;
    flow[s]=inf;
    aa[++len]=wa(,s);
    while(len){
      wa x=aa[];
      swap(aa[],aa[len--]);
      updata();
      int u=x.y;
      if(dis[x.y]<x.x) continue;
      ;j=eg[j].nex){
          int v=eg[j].to;
          int f=eg[j].cos;
          &&dis[v]>dis[u]+f+h[u]-h[v]){
             dis[v]=dis[u]+f+h[u]-h[v];
            flow[v]=min(flow[u],eg[j].va);
            pre[v]=j;
            aa[++len]=wa(dis[v],v);
            add(len);
          }
      }
    }
    ;
    ;
}
void work(){
   while(dijstra(s,t)){
      int z=t;
      while(z!=s){
         int i=pre[z];
         eg[i].va-=flow[t];
         eg[i^].va+=flow[t];
         z=eg[i^].to;
      }
      ;j<=n;j++){
         h[j]+=dis[j];
      }
      maxflow+=flow[t];
      maxcost+=flow[t]*h[t];
   }
}
int main(){
   cin>>n>>m>>s>>t;
   init();
   ;j<m;j++){
      int x,y,a,b;
      //scanf("%d%d%d%d",&x,&y,&a,&b);
      x=read(); y=read();a=read();b=read();
      add_eg(x,y,a,b);
   }
   work();
   printf("%d %d\n",maxflow,maxcost);
}

dijkstra 可能因为常数比较大 t了一个点但是 开o2之后可以轻松水过

dijkstra 版本
#include<bits/stdc++.h>
using namespace std;
#define maxn 100001
#define inf 0x3f3f3f3f
#define pii pair<int,int >
int head[maxn],dis[maxn],pre[maxn],flow[maxn],h[maxn];
struct ac{
  int to,va,cos,nex;
}eg[maxn];
,maxcost=,tot=;
void init(){
  tot=;
  memset(eg,,sizeof(eg));
  memset(head,-,sizeof(head));
  memset(h,,sizeof(h));
  memset(pre,,sizeof(pre));
}
void add_eg(int u,int to,int va,int cos){
   eg[tot].va=va;
   eg[tot].cos=cos;
   eg[tot].to=to;
   eg[tot].nex=head[u];
   head[u]=tot++;

   eg[tot].va=;
   eg[tot].cos=-cos;
   eg[tot].to=u;
   eg[tot].nex=head[to];
   head[to]=tot++;
}
bool dijstra(int s,int t){
   memset(dis,inf,sizeof(dis));
   memset(fa,,sizeof(fa));
   dis[s]=;
   flow[s]=inf;
   fa[s]=;
   priority_queue<pii,vector<pii>,greater<pii> >q;
   q.push(make_pair(,s));
   while(!q.empty()){
      pii x=q.top();
      q.pop();
      int u=x.second;
      if(dis[x.second]<x.first) continue;
      ;j=eg[j].nex){
         int v=eg[j].to;
         int f=eg[j].cos;
         &&dis[v]>dis[u]+f+h[u]-h[v]){
            dis[v]=dis[u]+f+h[u]-h[v];
            flow[v]=min(flow[u],eg[j].va);
            pre[v]=j;
            q.push(make_pair(dis[v],v));
         }
      }
   }
   ;
   ;
}
void work(){
   while(dijstra(s,t)){
      int z=t;
      while(z!=s){
         int i=pre[z];
         eg[i].va-=flow[t];
         eg[i^].va+=flow[t];
         z=eg[i^].to;
      }
      ;j<=n;j++){
         h[j]+=dis[j];
      }
      maxflow+=flow[t];
      maxcost+=flow[t]*h[t];
   }
}
int main(){
   cin>>n>>m>>s>>t;
   init();
   ;j<m;j++){
      int x,y,a,b;
      scanf("%d%d%d%d",&x,&y,&a,&b);
      add_eg(x,y,a,b);
   }
   work();
   cout<<maxflow<<" "<<maxcost<<endl;
}

SPFA版本

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
#define maxn 100001
#define inf 0x3f3f3f3f
int head[maxn],dis[maxn],pre[maxn],flow[maxn];
bool fa[maxn];
struct ac{
  int to,va,cos,nex;
}eg[maxn*];
,maxcost=,tot=;
void init(){
  tot=;
  memset(eg,,sizeof(eg));
  memset(head,-,sizeof(head));
}
void add_eg(int u,int to,int va,int cos){
   eg[tot].va=va;
   eg[tot].cos=cos;
   eg[tot].to=to;
   eg[tot].nex=head[u];
   head[u]=tot++;

   eg[tot].va=;
   eg[tot].cos=-cos;
   eg[tot].to=u;
   eg[tot].nex=head[to];
   head[to]=tot++;
}
bool spfa(int s,int t){
   memset(dis,inf,sizeof(dis));
   memset(fa,,sizeof(fa));
   dis[s]=;
   flow[s]=inf;
   fa[s]=;
   queue<int>q;
   q.push(s);
   while(!q.empty()){
      int u=q.front();
      q.pop();
      fa[u]=;
      ;j=eg[j].nex){
         int v=eg[j].to;
         int f=eg[j].cos;
         //cout<<u<<" "<<v<<endl;
         if(eg[j].va&&dis[u]+f<dis[v]){
            dis[v]=dis[u]+f;
            flow[v]=min(flow[u],eg[j].va);
            pre[v]=j;
            if(!fa[v]){
               fa[v]=;
               q.push(v);
            }
         }
      }
   }
   ;
   ;
}
void work(){
   while(spfa(s,t)){
      int z=t;
      while(z!=s){
         int i=pre[z];
         eg[i].va-=flow[t];
         eg[i^].va+=flow[t];
         z=eg[i^].to;
      }
      maxflow+=flow[t];
      maxcost+=flow[t]*dis[t];
   }
}
int main(){
   cin>>n>>m>>s>>t;
   init();
   ;j<m;j++){
      int x,y,a,b;
      cin>>x>>y>>a>>b;
      add_eg(x,y,a,b);
   }
   work();
   cout<<maxflow<<" "<<maxcost<<endl;
}

Luogu--3381 【模板】最小费用最大流的更多相关文章

  1. 洛谷.3381.[模板]最小费用最大流(zkw)

    题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...

  2. 【Luogu】P3381最小费用最大流模板(SPFA找增广路)

    题目链接 哈  学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ...

  3. 【洛谷 p3381】模板-最小费用最大流(图论)

    题目:给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 解法:在Dinic的基础下做spfa算法. 1 #include<cst ...

  4. P3381 [模板] 最小费用最大流

    EK  + dijkstra (2246ms) 开氧气(586ms) dijkstra的势 可以处理负权 https://www.luogu.org/blog/28007/solution-p3381 ...

  5. Luogu P3381 (模板题) 最小费用最大流

    <题目链接> 题目大意: 给定一张图,给定条边的容量和单位流量费用,并且给定源点和汇点.问你从源点到汇点的最带流和在流量最大的情况下的最小费用. 解题分析: 最小费用最大流果题. 下面的是 ...

  6. 费用流+SPFA ||Luogu P3381【模板】最小费用最大流

    题面:[模板]最小费用最大流 代码: #include<cstdio> #include<cstring> #include<iostream> #include& ...

  7. 最小费用最大流 学习笔记&&Luogu P3381 【模板】最小费用最大流

    题目描述 给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 题目链接 思路 最大流是没有问题的,关键是同时保证最小费用,因此,就可以把 ...

  8. luogu 3376 最小费用最大流 模板

    类似EK算法,只是将bfs改成spfa,求最小花费. 为什么可以呢,加入1-3-7是一条路,求出一个流量为40,那么40*f[1]+40*f[2]+40*f[3],f[1]是第一条路的单位费用,f[2 ...

  9. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

随机推荐

  1. WPF中定时器Timer与DispatcherTimer的用法

    最近的工作项目中需要定时更新UI控件中的数据,这时候第一反应肯定会想到去使用System.Timers.Timer定时更新UI控件,但是程序运行后,会发现程序崩溃了.报的异常为“调用线程无法访问此对象 ...

  2. Oracle Profile 配置文件

    Profile是用户的配置文件,它是密码限制,资源限制的命名集合.利用profile 可以对数据库用户进行基本的资源管理,密码管理. 1 创建profile 的语法 create profile pr ...

  3. java随笔3 spring 的注入执行逻辑顺序

  4. 补充:pyhton 2 和3中的beyts类型

    在python2里,bytes == str python2里还有个单独的类型是unicode , 把字符串解码后,就会变成unicode. 既然python2中byets == str,那为什么还要 ...

  5. 虚拟机的ip地址为什么会发生变化

    因为虚拟机在NAT模式下由Vmware8虚拟网卡提供虚拟机的IP分配,网桥模式下由Vmware1来提供IP分配.它们都相当于 一个小型的DHCP服务器,除非改动虚拟机的网络连接方式,或动了虚拟网卡服务 ...

  6. mac 电脑 打开隐藏文件

    command +shift +. 第一次是打开,第二次是关闭

  7. shit vue & shit iview

    shit vue & shit iview <Switch> !== <i-switch> https://www.iviewui.com/components/swi ...

  8. AI算法第一天【概述与数学初步】

    1. 机器学习的定义: 机器从数据中学习出规律和模式,以应用在新数据上作出预测的任务 2.学习现象: (1)语言文字的认知识别 (2)图像,场景,物体的认知和识别 (3)规则:下雨天要带雨伞 (4)复 ...

  9. 《Tensorflow从入门到精通》

    第一 开发环境搭建 1. tensorflow的环境搭建 windows下安装cpu版tensorflow: pip install tensorflow 在ubuntu上安装gpu版tensorfl ...

  10. Linux环境下安装NodeJS和mongoDB

    前面的话 本文将详细介绍如何下Linux环境下安装NodeJS和mongoDB NodeJS [1]使用二进制包安装 1.在官网下载Linux环境下的NodeJS安装包 2.通过xftp软件将安装包上 ...