bzoj 2502 清理雪道 (有源汇上下界最小流)
2502: 清理雪道
Time Limit: 10 Sec Memory Limit: 128 MB
Description
Input
输入文件的第一行包含一个整数n (2 <= n <= 100) – 代表滑雪场的地点的数量。接下来的n行,描述1~n号地点出发的斜坡,第i行的第一个数为mi (0 <= mi < n) ,后面共有mi个整数,由空格隔开,每个整数aij互不相同,代表从地点i下降到地点aij的斜坡。每个地点至少有一个斜坡与之相连。
Output
Sample Input
1 3
1 7
2 4 5
1 8
1 8
0
2 6 5
0
Sample Output
- #include<cstdio>
- #include<queue>
- #include<algorithm>
- #define N 105
- #define M 40001
- using namespace std;
- const int inf=2e9;
- int n,a[N],ans;
- int src,dec,S,T;
- int to[M],next[M],front[N],tot=,cap[M];
- int lev[N],cur[N];
- queue<int>q;
- void add(int u,int v,int w)
- {
- to[++tot]=v; next[tot]=front[u]; front[u]=tot; cap[tot]=w;
- to[++tot]=u; next[tot]=front[v]; front[v]=tot; cap[tot]=;
- }
- bool bfs(int s,int t)
- {
- for(int i=;i<=n+;i++) cur[i]=front[i],lev[i]=-;
- while(!q.empty()) q.pop();
- lev[s]=;
- q.push(s);
- int now;
- while(!q.empty())
- {
- now=q.front(); q.pop();
- for(int i=front[now];i;i=next[i])
- if(cap[i]>&&lev[to[i]]==-)
- {
- lev[to[i]]=lev[now]+;
- if(to[i]==t) return true;
- q.push(to[i]);
- }
- }
- return false;
- }
- int dfs(int now,int t,int flow)
- {
- if(now==t) return flow;
- int rest=,delta;
- for(int i=front[now];i;i=next[i])
- if(cap[i]>&&lev[to[i]]>lev[now])
- {
- delta=dfs(to[i],t,min(flow-rest,cap[i]));
- if(delta)
- {
- cap[i]-=delta; cap[i^]+=delta;
- rest+=delta; if(rest==flow) return rest;
- }
- }
- if(rest!=flow) lev[now]=-;
- return rest;
- }
- int dinic(int s,int t)
- {
- while(bfs(s,t)) dfs(s,t,inf);
- }
- int main()
- {
- scanf("%d",&n);
- dec=n+; S=n+;T=n+;
- for(int i=;i<=n;i++) add(i,dec,inf);
- for(int i=;i<=n;i++) add(src,i,inf);
- int x,y;
- for(int i=;i<=n;i++)
- {
- scanf("%d",&x);
- while(x--)
- {
- scanf("%d",&y);
- add(i,y,inf);
- a[y]++; a[i]--;
- }
- }
- for(int i=;i<=n;i++)
- if(a[i]>) add(S,i,a[i]);
- else if(a[i]<) add(i,T,-a[i]);
- dinic(S,T);
- add(dec,src,inf);
- dinic(S,T);
- printf("%d",cap[tot]);
- }
法二、先由普通汇点向普通源点连一条下界为0,上界为inf的边,再由超级源点向超级汇点跑一遍dinic,记那条边的流量为okflow
删去那条边,删去超级源点,删去超级汇点,由普通汇点向普通源点跑一遍dinic,得出最大流为a,
答案为okflow-a
- #include<cstdio>
- #include<queue>
- #include<algorithm>
- #define N 105
- #define M 40001
- using namespace std;
- const int inf=2e9;
- int n,a[N],ans;
- int src,dec,S,T;
- int to[M],next[M],front[N],tot=,cap[M];
- int lev[N],cur[N];
- queue<int>q;
- void add(int u,int v,int w)
- {
- to[++tot]=v; next[tot]=front[u]; front[u]=tot; cap[tot]=w;
- to[++tot]=u; next[tot]=front[v]; front[v]=tot; cap[tot]=;
- }
- bool bfs(int s,int t)
- {
- for(int i=;i<=n+;i++) cur[i]=front[i],lev[i]=-;
- while(!q.empty()) q.pop();
- lev[s]=;
- q.push(s);
- int now;
- while(!q.empty())
- {
- now=q.front(); q.pop();
- for(int i=front[now];i;i=next[i])
- if(cap[i]>&&lev[to[i]]==-)
- {
- lev[to[i]]=lev[now]+;
- if(to[i]==t) return true;
- q.push(to[i]);
- }
- }
- return false;
- }
- int dfs(int now,int t,int flow)
- {
- if(now==t) return flow;
- int rest=,delta;
- for(int i=front[now];i;i=next[i])
- if(cap[i]>&&lev[to[i]]>lev[now])
- {
- delta=dfs(to[i],t,min(flow-rest,cap[i]));
- if(delta)
- {
- cap[i]-=delta; cap[i^]+=delta;
- rest+=delta; if(rest==flow) return rest;
- }
- }
- if(rest!=flow) lev[now]=-;
- return rest;
- }
- int dinic(int s,int t)
- {
- int tmp=;
- while(bfs(s,t))
- tmp+=dfs(s,t,inf);
- return tmp;
- }
- void del(int x)
- {
- for(int i=front[x];i;i=next[i])
- cap[i]=cap[i^]=;
- }
- int main()
- {
- scanf("%d",&n);
- dec=n+; S=n+;T=n+;
- for(int i=;i<=n;i++) add(i,dec,inf);
- for(int i=;i<=n;i++) add(src,i,inf);
- int x,y;
- for(int i=;i<=n;i++)
- {
- scanf("%d",&x);
- while(x--)
- {
- scanf("%d",&y);
- add(i,y,inf);
- a[y]++; a[i]--;
- }
- }
- for(int i=;i<=n;i++)
- if(a[i]>) add(S,i,a[i]);
- else if(a[i]<) add(i,T,-a[i]);
- add(dec,src,inf);
- dinic(S,T);
- int okflow=cap[tot];
- del(T); del(S); cap[tot]=cap[tot-]=;
- printf("%d",okflow-dinic(dec,src));
- }
bzoj 2502 清理雪道 (有源汇上下界最小流)的更多相关文章
- BZOJ 2502 清理雪道/ Luogu P4843 清理雪道 (有源汇上下界最小流)
题意 有一个有向无环图,求最少的路径条数覆盖所有的边 分析 有源汇上下界最小流板题,直接放代码了,不会的看dalao博客:liu_runda 有点长,讲的很好,静心看一定能看懂 CODE #inclu ...
- BZOJ 2502 清理雪道(有源汇上下界最小流)
题面 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机, ...
- BZOJ_2502_清理雪道_有源汇上下界最小流
BZOJ_2502_清理雪道_有源汇上下界最小流 Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道), ...
- 【Loj117】有源汇上下界最小流(网络流)
[Loj117]有源汇上下界最小流(网络流) 题面 Loj 题解 还是模板题. #include<iostream> #include<cstdio> #include< ...
- hdu3157有源汇上下界最小流
题意:有源汇上下界最小流裸题,主要就是输入要用字符串的问题 #include<bits/stdc++.h> #define fi first #define se second #defi ...
- sgu176 有源汇上下界最小流
题意:有一堆点和边,1起点,n终点,某些边有可能必须满流,要求满足条件的最小流 解法:按原图建边,满流的即上下界都是容量,但是这样按有源汇上下界可行流求出来的可能不是最小流,那么我们需要开始建边的时候 ...
- HDU 3157 Crazy Circuits (有源汇上下界最小流)
题意:一个电路板,上面有N个接线柱(标号1~N) 还有两个电源接线柱 + - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流. 析:这是一个有源汇有上下界的 ...
- SGU 176 Flow construction(有源汇上下界最小流)
Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...
- HDU 3157 Crazy Circuits(有源汇上下界最小流)
HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...
随机推荐
- Swing State: Consistent Updates for Stateful and Programmable Data Planes
Swing State: Consistent Updates for Stateful and Programmable Data Planes 年份:2017 来源:ACM 本篇论文解决的问题 B ...
- C++对象模型 多重继承与虚函数表
一 多重继承 1) 代码: Code#include <iostream>using namespace std; class B1{public: int x; virtua ...
- Spring+Netty4实现的简单通信框架
参考:http://cpjsjxy.iteye.com/blog/1587601 Spring+Netty4实现的简单通信框架,支持Socket.HTTP.WebSocket_Text.WebSock ...
- 倒计时60s 代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【第二周】【作业五】Scrum 每日站会
1.首先来看一下什么是Scrum: Scrum是一种敏捷软件开发的方法学,用于迭代式增量软件开发过程.Scrum在英语是橄榄球运动中争球的意思. 虽然Scrum是为管理软件开发项目而开发的,它同样可以 ...
- IDEA配置Java Web项目
IDEA部署maven tomcat的java web项目的关键配置:
- HDU4802_GPA
水题. #include <iostream> #include <cstdio> #include <cstring> using namespace std; ...
- HDU4466_Triangle
今天比赛做的一个题目,不过今天终于感受到了复旦题目有多坑了. 题目的意思是给你一段长为n个单位长度的直线,你可以选择任意连续单位长度的线段组成三角形,可以组成任意你可以组成任意多个三角形,且要求其中所 ...
- 【JavaScript&jQuery】单选框radio,复选框checkbox,下拉选择框select
HTML: <!DOCTYPE html> <html> <head> <title></title> <meta charset=& ...
- C++解析(24):抽象类和接口、多重继承
0.目录 1.抽象类和接口 1.1 抽象类 1.2 纯虚函数 1.3 接口 2.被遗弃的多重继承 2.1 C++中的多重继承 2.2 多重继承的问题一 2.3 多重继承的问题二 2.4 多重继承的问题 ...