网络流

网络流24题 太空飞行计划问题

社会主义新天地 提交于 2019-12-04 10:39:19
题目传送门 这道题不是好久之前做的了 填一下网络流24题的坑 本质上是个最大权闭合图问题的模板 (话说这么多问题,我怎么记得住) 在源点 \(S\) 和每个实验之间连一条边权为实验利益的边 在每个实验和它需要的仪器之间连一条边权为 \(+\infty\) 的边 在仪器和汇点 \(t\) 之间连一条边权为仪器花费的边 然后跑最小割就好了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define LL long long #define inf 0x7fffffff using namespace std; LL read() { LL k = 0, f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') k = k * 10 + c - 48, c = getchar(); return k * f; } struct zzz { int t, nex, len; }e[100010 << 1]; int head[1010], tot = 1; void

网络流24题 最长不下降子序列问题

心已入冬 提交于 2019-12-04 10:28:16
题目传送门 这个建图啊,十分巧妙,我是真没看出来(我太蒟了) 首先要先跑一遍最长不下降子序列的 \(O(n^2)\) 解法,预处理出以 \(i\) 结尾的最长不下降子序列的长度 \(dp[i]\) ,然后找出最大的 \(dp[i]\) ,记为 \(cnt\) ,输出 \(cnt\) ,第一问就结束了。 从源点向长度为 \(1\) 的点连一条容量为 \(1\) 的边,长度为 \(cnt\) 的点向汇点连一条容量为 \(1\) 的边。因为每个点只能用一次,所以我们还要把一个点拆成两个,在它们之间连一条容量为 \(1\) 的边。之后 \(n^2\) 枚举每两个点,若两个点 \(i,j\) 满足 \(j{<}i\) 且 \(dp[i]=dp[j]+1\) ,则在 \(i,j\) 之间连一条容量为 \(1\) 的边。建好图后跑一边最大流,就是第二问的答案。 第三问就好办了,将源点向 \(1\) 连的边的容量改为 \(\rm{INF}\) , \(1\) 和 \(1\) 被拆之后的点 \(1'\) 之间的容量也改为 \(\rm{INF}\) 。同理, \(n\) 和 \(n'\) 之间的边、 \(n'\) 和汇点之间的边(如果存在的话)容量也要改为 \(\rm{INF}\) #include<iostream> #include<cstdio> #include<cstring>

网络流24题 魔术球问题

浪子不回头ぞ 提交于 2019-12-04 10:28:10
题目传送门 又是一个神奇的建图题,建图 \(Van\) ♂全不会啊 大体就是我们一个一个的把球放进来,每放进来一个,我们就求出当前的最小路径覆盖数(当前顶点数-最大流),直到最小路径覆盖数 \({>}\) 柱子数。此时的球的编号 \(-1\) 就是第一问的答案。第二问就是求每一条路径,顺着推下来就好了 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<cmath> using namespace std; struct zzz{ int t,len,nex; }e[100010<<1]; int head[20010],tot=1; void add(int x,int y,int z){ e[++tot].t=y; e[tot].len=z; e[tot].nex=head[x]; head[x]=tot; } int s=0,t=20000,vis[20010],pre[20010]; bool bfs(){ memset(vis,0,sizeof(vis)); queue <int> q; q.push(s); vis[s]=1; while(!q.empty()){ int k=q.front(); q.pop(); for

网络流24题 飞行员配对方案问题

和自甴很熟 提交于 2019-12-04 10:27:47
题目传送门 虽然这题是“网络流24题”,但我匈牙利 Van♂ 完全不虚,还比 \(dinic\) 好写不少(不过就是慢一些) #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int read(){ int k=0,f=1; char c=getchar(); for(;c<'0'||c>'9';c=getchar()) if(c=='-') f=-1; for(;c>='0'&&c<='9';c=getchar()) k=(k<<3)+(k<<1)+c-48; return k*f; } struct zzz{ int t,nex; }e[10001*2]; int head[101],tot; inline void add(int x,int y){ e[++tot].t=y; e[tot].nex=head[x]; head[x]=tot; } bool vis[101];int lin[101]; bool find(int x){ for(int i=head[x];i;i=e[i].nex) if(!vis[e[i].t]){ vis[e[i].t]=1; if(!lin[e[i].t]||find(lin[e[i].t])){

网络流24题

南楼画角 提交于 2019-12-04 08:47:16
1.飞行员配对方案问题 题目链接 二分图匹配模板题 2.负载平衡问题 题目链接 题意: 公司有 n个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等。如何用最少搬运量可以使 n个仓库的库存数量相同。搬运货物时,只能在相邻的仓库之间搬运。输出最少搬运量。 思路: 求出平均数,显然最后所有仓库的货物数量都是平均数。如果点u的货物数比平均数大w,则源点到u有一条容量为w;费用为0的边,如果点u的货物数比平均数小w,则u到汇点有一条容量为w,费用为0的边。相邻两点之间有容量无穷大,费用为1的边,跑费用流即可。 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxm=1e3+5; const int maxn=105; const int INF=1e9; struct edge { int v,next,w,f; } E[maxm]; int tot=1,head[maxn];//tot必须设为1才能使反向边用^得到 void addedge(int u,int v,int w,int f) { E[++tot].v=v; E[tot].w=w; E[tot].f=f; E[tot].next=head[u]; head[u]=tot; } int n,m,s,t; int

Kindergarten(网络流解法)

Deadly 提交于 2019-12-03 20:54:14
题意: http://acm.hdu.edu.cn/showproblem.php?pid=2458 问你二分图的最大团是多大。 1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15

下载网络文件,网络流转换为内存流

狂风中的少年 提交于 2019-12-03 17:08:00
string fileext = CSA_BLL.Common.Utils.GetFileExt(filePath); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(filePath); request.Method = "GET"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.OK) { Stream rs = response.GetResponseStream(); //网络流转换为内存流 var ms = StreamToMemoryStream(rs); ms.Seek(0, SeekOrigin.Begin); int buffsize = (int)ms.Length; //rs.Length 此流不支持查找,先转为MemoryStream byte[] bytes = new byte[buffsize]; ms.Read(bytes, 0, buffsize); ms.Flush(); ms.Close(); rs.Flush(); rs.Close(); //以文件流的方式下载 Response

P4014 分配问题 网络流

烂漫一生 提交于 2019-12-03 15:07:40
P4014 分配问题 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 305, inf = 0x3f3f3f3f; 4 struct Edge { 5 int from, to, cap, flow, cost; 6 }; 7 8 struct MCMF { 9 int n, m, s, t; 10 vector<Edge> edges; 11 vector<int> G[maxn]; 12 int inq[maxn]; 13 int d[maxn]; 14 int p[maxn]; 15 int a[maxn]; 16 17 void init(int n) { 18 this->n = n; 19 for (int i = 1; i <= n; ++i) G[i].clear(); 20 edges.clear(); 21 } 22 23 void AddEdge(int from, int to, int cap, int cost) { 24 edges.push_back((Edge){from, to, cap, 0, cost}); 25 edges.push_back((Edge){to, from, 0, 0, -cost}); 26 m = edges.size(); 27

P4016 负载平衡问题 网络流

别等时光非礼了梦想. 提交于 2019-12-03 15:04:58
P4016 负载平衡问题 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1005, inf = 0x3f3f3f3f; 4 struct Edge { 5 int from, to, cap, flow, cost; 6 }; 7 8 struct MCMF { 9 int n, m, s, t; 10 vector<Edge> edges; 11 vector<int> G[maxn]; 12 int inq[maxn]; 13 int d[maxn]; 14 int p[maxn]; 15 int a[maxn]; 16 17 void init(int n) { 18 this->n = n; 19 for (int i = 1; i <= n; ++i) G[i].clear(); 20 edges.clear(); 21 } 22 23 void AddEdge(int from, int to, int cap, int cost) { 24 edges.push_back((Edge){from, to, cap, 0, cost}); 25 edges.push_back((Edge){to, from, 0, 0, -cost}); 26 m = edges.size();

P1251 餐巾计划问题 网络流

南楼画角 提交于 2019-12-03 14:45:42
P1251 餐巾计划问题 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 5005, inf = 0x3f3f3f3f; 5 struct Edge { 6 int from, to; 7 ll cap, flow, cost; 8 }; 9 10 struct MCMF { 11 int n, m, s, t; 12 vector<Edge> edges; 13 vector<int> G[maxn]; 14 int inq[maxn]; 15 ll d[maxn]; 16 int p[maxn]; 17 ll a[maxn]; 18 19 void init(int n) { 20 this->n = n; 21 for (int i = 1; i <= n; ++i) G[i].clear(); 22 edges.clear(); 23 } 24 25 void AddEdge(int from, int to, ll cap, ll cost) { 26 edges.push_back((Edge){from, to, cap, 0, cost}); 27 edges.push_back((Edge){to, from, 0, 0,