P4015 运输问题 最大/最小费用最大流

P4015 运输问题

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int maxn = 205, inf = 0x3f3f3f3f;
  4 struct Edge {
  5     int from, to, cap, flow, cost;
  6 };
  7 struct MCMF {
  8     int n, m, s, t;
  9     vector<Edge> edges;
 10     vector<int> G[maxn];
 11     int inq[maxn];
 12     int d[maxn];
 13     int p[maxn];
 14     int a[maxn];
 15
 16     void init(int n) {
 17         this->n = n;
 18         for (int i = 1; i <= n; ++i) G[i].clear();
 19         edges.clear();
 20     }
 21
 22     void AddEdge(int from, int to, int cap, int cost) {
 23         edges.push_back((Edge){from, to, cap, 0, cost});
 24         edges.push_back((Edge){to, from, 0, 0, -cost});
 25         m = edges.size();
 26         G[from].push_back(m-2);
 27         G[to].push_back(m-1);
 28     }
 29     bool BellmanFord(int s, int t, int& flow, int& cost) {
 30         for (int i = 1; i <= n; ++i) d[i] = inf;
 31         memset(inq, 0, sizeof(inq));
 32         d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = inf;
 33
 34         queue<int> que;
 35         que.push(s);
 36         while (!que.empty()) {
 37             int u = que.front(); que.pop();
 38             inq[u] = 0;
 39             for (int i = 0; i < G[u].size(); ++i) {
 40                 Edge& e = edges[G[u][i]];
 41                 if (e.cap > e.flow && d[e.to] > d[u] + e.cost) {
 42                     d[e.to] = d[u] + e.cost;
 43                     p[e.to] = G[u][i];
 44                     a[e.to] = min(a[u], e.cap-e.flow);
 45                     if (!inq[e.to]) { que.push(e.to); inq[e.to] = 1; }
 46                 }
 47             }
 48         }
 49         if (d[t] == inf) return false;
 50         flow += a[t];
 51         cost += d[t] * a[t];
 52         int u = t;
 53         while (u != s) {
 54             edges[p[u]].flow += a[t];
 55             edges[p[u]^1].flow -= a[t];
 56             u = edges[p[u]].from;
 57         }
 58         return true;
 59     }
 60     int mincost(int s, int t) {
 61         int flow = 0, cost = 0;
 62         while (BellmanFord(s, t, flow, cost));
 63         return cost;
 64     }
 65 }mcmf;
 66 int a[maxn], b[maxn], c[maxn][maxn];
 67 int main() {
 68     int m, n; scanf("%d%d",&m,&n);
 69     int s = m+n+1, t = m+n+2;
 70
 71     for (int i = 1; i <= m; ++i) scanf("%d",&a[i]);
 72     for (int j = 1; j <= n; ++j) scanf("%d",&b[j]);
 73     for (int i = 1; i <= m; ++i) {
 74         for (int j = 1; j <= n; ++j) {
 75             scanf("%d",&c[i][j]);
 76         }
 77     }
 78     // 最小费用最大流
 79     mcmf.init(n+m+2);
 80     for (int i = 1; i <= m; ++i) {
 81         mcmf.AddEdge(s,i,a[i],0);
 82     }
 83     for (int i = 1; i <= m; ++i) {
 84         for (int j = 1; j <= n; ++j) {
 85             mcmf.AddEdge(i,j+m,inf,c[i][j]);
 86         }
 87     }
 88     for (int j = 1; j <= n; ++j) {
 89         mcmf.AddEdge(j+m,t,b[j],0);
 90     }
 91     printf("%d\n",mcmf.mincost(s,t));
 92
 93     // 最大费用最大流
 94     mcmf.init(n+m+2);
 95     for (int i = 1; i <= m; ++i) {
 96         mcmf.AddEdge(s,i,a[i],0);
 97     }
 98     for (int i = 1; i <= m; ++i) {
 99         for (int j = 1; j <= n; ++j) {
100             mcmf.AddEdge(i,j+m,inf,-c[i][j]);
101         }
102     }
103     for (int j = 1; j <= n; ++j) {
104         mcmf.AddEdge(j+m,t,b[j],0);
105     }
106     printf("%d\n",-mcmf.mincost(s,t));
107     return 0;
108 }

原文地址:https://www.cnblogs.com/wstong/p/11785927.html

时间: 2024-07-31 01:01:12

P4015 运输问题 最大/最小费用最大流的相关文章

洛谷 P4015 运输问题 【最小费用最大流+最大费用最大流】

s向仓库i连ins(s,i,a[i],0),商店向t连ins(i+m,t,b[i],0),商店和仓库之间连ins(i,j+m,inf,c[i][j]).建两次图分别跑最小费用最大流和最大费用最大流即可. #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; const int N=1000005,inf=1e9; int n,m,h

luogu p4015(最小费用最大流)

传送门 题意: 有\(m\)个仓库和\(n\)个零售商,第\(i\)个仓库送到第\(j\)个零售商需要花费\(v[i][j]\)元.现在需要让仓库的供给量以及零售商的收获量相同,问最小花费以及最大花费. 分析: 相当经典的最小费用最大流的模型.因为要保证供给以及收获相同,即代表着流量平衡,因此我们可以让超级源点\(sp\)跟对应的仓库连一条流量为\(a_i\),费用为\(0\)的边,同时让对应的零售商跟超级汇点\(ep\)连一条流量为\(b_i\),费用为\(0\)的边.而对于仓库与零售商,我们

[网络流24题(5/24)] 分配问题(最小费用最大流)

传送门 分析: 非常经典的费用流的模型吧,也可以通过二分图最大匹配去做,但是鉴于二分图最大匹配的算法存在一定的局限性,故还是学一学较为通用的费用流的做法. 这道题目中本质上要讨论的问题跟运输问题,运输问题是一致的. 因为考虑到每个人只能被分配到一种货物,每种货物只能被一个人所分配,因此,我们不妨用流量将他们限流. 我们创建一个超级源地\(sp\),将\(sp\)跟每个人连一条流量为\(1\),费用为\(0\)的边. 同时我们创建一个超级汇点\(ep\),将每一种货物跟\(ep\)都连一条流量为\

【BZOJ3876】【Ahoi2014】支线剧情 有下界的最小费用最大流

#include <stdio.h> int main() { puts("转载请注明出处谢谢"); puts("http://blog.csdn.net/vmurder/article/details/43025375"); } [BZOJ2324]营救皮卡丘 这道题也是一道有下界的最小费用最大流. 我的题解地址:http://blog.csdn.net/vmurder/article/details/41378979 这道题其实就是模板题. 我的处理

POJ 3686.The Windy&#39;s 最小费用最大流

The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5477   Accepted: 2285 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

P3381 【模板】最小费用最大流

P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行包含四个正整数ui.vi.wi.fi,表示第i条有向边从ui出发,到达vi,边权为wi(即该边最大流量为wi),单位流量的费用为fi. 输出格式: 一行,包含两个整数,依次为最大流量和在最大流量情况下的

C++之路进阶——最小费用最大流(支线剧情)

F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  hyxzc Logout 捐赠本站 Notice:由于本OJ建立在Linux平台下,而许多题的数据在Windows下制作,请注意输入.输出语句及数据类型及范围,避免无谓的RE出现. 3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 542  Solved: 332[Submit

hdu 4494 Teamwork 最小费用最大流

Teamwork Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4494 Description Some locations in city A has been destroyed in the fierce battle. So the government decides to send some workers to repair these location

POJ - 2195 Going Home(最小费用最大流)

1.N*M的矩阵中,有k个人和k个房子,每个人分别进入一个房子中,求所有人移动的最小距离. 2.人看成源点,房子看成汇点,求最小费用最大流. 建图-- 人指向房子,容量为1,费用为人到房子的曼哈顿距离. 建立超级源点和超级汇点:超级源点指向人,容量为1,费用为0:超级汇点指向房子,容量为1,费用为0. 求超级源点到超级汇点的最小费用最大流即可. ps:容量为什么都设为1?---有待研究.. 3. 1.Bellman-Ford: #include<iostream> #include<st