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

解法:在Dinic的基础下做spfa算法。

 1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<iostream>
5 #include<queue>
6 using namespace std;
7
8 const int N=5010,M=50010,INF=(int)1e9;
9 int n,m,st,ed,len=1;
10 int last[N],vis[N],id[N],pre[N],flow[N],d[N];
11 struct node{int y,fl,co,next;}a[2*M];
12 queue<int> q;
13
14 int mmin(int x,int y) {return x<y?x:y;}
15 void ins(int x,int y,int fl,int co)
16 {
17 a[++len].y=y,a[len].fl=fl,a[len].co=co;
18 a[len].next=last[x],last[x]=len;
19 a[++len].y=x,a[len].fl=0,a[len].co=-co;
20 a[len].next=last[y],last[y]=len;
21 }
22 bool spfa()
23 {
24 while (!q.empty()) q.pop();
25 for (int i=1;i<=n;i++) d[i]=INF;
26 memset(vis,0,sizeof(vis));
27 q.push(st);
28 d[st]=0,vis[st]=1,flow[st]=INF;
29 while (!q.empty())
30 {
31 int x=q.front();
32 q.pop(); vis[x]=0;
33 for (int i=last[x];i!=-1;i=a[i].next)
34 {
35 int y=a[i].y;
36 if (!a[i].fl) continue;
37 if (d[x]+a[i].co<d[y])
38 {
39 d[y]=d[x]+a[i].co;
40 flow[y]=mmin(flow[x],a[i].fl);
41 id[y]=i, pre[y]=x;
42 if (!vis[y]) q.push(y), vis[y]=1;
43 }
44 }
45 }
46 return (d[ed]!=INF);
47 }
48 void Max_flow()
49 {
50 int sum=0,cost=0;;
51 while (spfa())
52 {
53 sum+=flow[ed],cost+=d[ed]*flow[ed];
54 for (int i=ed;i!=st;i=pre[i])
55 {
56 a[id[i]].fl-=flow[ed];
57 a[id[i]^1].fl+=flow[ed];
58 }
59 }
60 printf("%d %d\n",sum,cost);
61 }
62 int main()
63 {
64 scanf("%d%d%d%d",&n,&m,&st,&ed);
65 int x,y,fl,co;
66 memset(last,-1,sizeof(last));
67 for (int i=1;i<=m;i++)
68 {
69 scanf("%d%d%d%d",&x,&y,&fl,&co);
70 ins(x,y,fl,co);
71 }
72 Max_flow();
73 return 0;
74 }

最新文章

  1. MS - 1 - 把二元查找树转变成排序的双向链表
  2. 国家电力项目SSH搭建
  3. WPS文字在表格中打字自动跳动
  4. js函数和jquery函数详解
  5. Unity3D手游开发日记(9) - 互动草的效果
  6. [转载]SharePoint 2013搜索学习笔记之搜索构架简单概述
  7. linux之od命令
  8. HIbernate学习笔记(七) hibernate中的集合映射和继承映射
  9. ios 获取字符串所需要占用的label的高度
  10. leetcode修炼之路——13. Roman to Integer
  11. 使用top工具,找出消耗CPU 较多的进程
  12. js脚本同步、异步与延迟
  13. C#中大List的内存分配
  14. python3 time模块与datetime模块
  15. Python之matplotlib学习(一)
  16. Download all Apple open source OS X files at once
  17. 【Android 应用开发】 ActionBar 基础
  18. NSURLSession 所有的都在这里(二)
  19. 盒子模型、IFC、BFC和Collapsing margins
  20. [转]Qt 之 QFileSystemWatcher

热门文章

  1. ssh升级以及ssh: symbol lookup error: ssh: undefined symbol: EVP_aes_128_ctr错误处理
  2. 记一次使用Asp.Net Core WebApi 5.0+Dapper+Mysql+Redis+Docker的开发过程
  3. 阿里云OSS整合
  4. 你必须要懂的 Github 开源协议
  5. 编译安装PHP - 7.3.16
  6. docker 容器和镜像的常用命令
  7. 【Linux】iptables的内核模块问题大坑!
  8. 【Oracle】查看表或视图的创建语句
  9. Java反射全解析(使用、原理、问题、在Android中的应用)
  10. Unsafe Fileupload - Pikachu