#include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<queue> #include<algorithm> using namespace std; + ; const int INF = 0x7FFFFFFF; struct Edge { int from, to, cap, flow;…
题意: 第一行输入m和n,m是猪圈的数量,n是顾客的数量,下面n行 第 i+1行表示第i个顾客 , 输入第一个数字表示有几把猪圈的钥匙,后面输入对应的猪圈,最后一个数字输入顾客想买几头猪. 建图: 设一个源点 s 和一个汇点 t,s 连接猪圈被第一个打开的顾客,权值为猪圈的猪数量,t 与顾客相连,权值为顾客想要的猪的数量,因为题上说迈克会根据下一个顾客的需求来将猪转移到合适的猪圈,所以顾客与同一猪圈排队的后面紧邻的顾客相连,权值为正无穷. 想一下感觉挺对的,s点有无数头猪,但是边的权值为猪圈的数…
题目传送门 /* KM:裸题第一道,好像就是hungary的升级版,不好理解,写点注释 KM算法用来解决最大权匹配问题: 在一个二分图内,左顶点为X,右顶点为Y,现对于每组左右连接Xi,Yj有权w(i,j), 求一种匹配使得所有w(i,j)的和最大.也就是最大权匹配一定是完备匹配.如果两边的点数相等则是完美匹配. 如果点数不相等,其实可以虚拟一些点,使得点数相等,也成为了完美匹配.最大权匹配还可以用最大流去解决 */ #include <cstdio> #include <algorit…
参考: https://blog.csdn.net/txl199106/article/details/64441994 分析: 该算法是用bfs求出是否有路从s到t, 然后建立反向边(关于反向边), 最终求出答案, 复杂度(mn) #include<bits/stdc++.h> using namespace std; + ; + ; << ); struct Edge { int v, nxt, w; }; struct Node { int v, id; }; int n,…
题目传送门 /* 最小费用流:KM算法是求最大流,只要w = -w就可以了,很经典的方法 */ #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> using namespace std; ; const int INF = 0x3f3f3f3f; int x[MAXN], y[MAXN]; int w[MAXN][MAXN]; int visx[MAXN],…
题目传送门 /* KM: 相比HDOJ_1533,多了重边的处理,还有完美匹配的判定方法 */ #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> using namespace std; ; const int INF = 0x3f3f3f3f; int x[MAXN], y[MAXN]; int w[MAXN][MAXN]; int visx[MAXN],…
//最短增广路,Dinic算法 struct Edge { int from,to,cap,flow; };//弧度 void AddEdge(int from,int to,int cap) //增弧 { edges.push_back((Edge){}); edges.push_back((Edge){to,,}); m=edges.size(); G[); G[to].push_back(m-); } struct Dinic{ int n,m,s,t; vector<Edge> edg…
Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 24830   Accepted: 10756 Case Time Limit: 5000MS Description As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft C…
/* Time:2015-6-18 接触网络流好几天了 写的第一个模版————Ford-Fulkerson算法 作用:求解网络最大流 注意:源点是0 汇点是1 如果题目输入的是1到n 请预处理减1 */ #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> using namespace std; const int INF =…
题目链接 哈  学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能扩大现有流量,而且目前为止它是可以扩大流量的所有路径中单位花费最少的. 然后我们就把这条路填上.想想看当我们找不到这样一条路的时候会发生什么? 那就是没有增广路了.恭喜我们获得最小费用最大流.这是为什么呢? 首先没有任何一条增广路的时候我们肯定获得最大流没错 然后我们回顾我们扩展的方式.每次我们都选…