【BZOJ2625】[Neerc2009]Inspection

Description

You are in charge of a team that inspects a new ski resort. A ski resort is situated on several mountains and consists of a number of slopes. Slopes are connected with each other, forking and joining. A map of the ski resort is represented as an acyclic directed graph. Nodes of the graph represent different points in ski resort and edges of the graph represent slopes between the points, with the direction of edges going downwards. 
Your team has to inspect each slope of the ski resort. Ski lifts on this resort are not open yet, but you have a helicopter. In one fiight the helicopter can drop one person into any point of the resort. From the drop off point the person can ski down the slopes, inspecting each slope as they ski. It is fine to inspect the same slope multiple times, but you have to minimize the usage of the helicopter. So, you have to figure out how to inspect all the slopes with the fewest number of helicopter flights.
给张有向无环图,问至少多少条路径能够覆盖所有的边(可以重复

Input

The first line of the input file contains a single integer number n (2 <= n <= 100) - the number of points in the ski resort. The following n lines of the input file describe each point of the ski resort numbered from 1 to n. Each line starts with a single integer number mi (0 <= mi < n for i from 1 to n) and is followed by mi integer numbers aij separated by spaces. All aij are distinct for each i and each aij (1 <= aij <= n, aij  i) represents a slope going downwards from point i to point aij . Each point in the resort has at least one slope connected to it.

Output

On the first line of the output file write a single integer number k - the minimal number of helicopter flights that are needed to inspect all slopes. Then write k lines that describe inspection routes for each helicopter flight. Each route shall start with single integer number from 1 to n - the number of the drop off point for the helicopter flight, followed by the numbers of points that will be visited during inspection in the corresponding order as the slopes are inspected going downwards. Numbers on a line shall be separated by spaces. You can write routes in any order.

Sample Input

8
1 3
1 7
2 4 5
1 8
1 8
0
2 6 5
0

Sample Output

4

题解:经典的最小链覆盖问题。

采用有上下界的网络流的思路,将每个点拆成两个,从出点向入点连一条(0,inf)的边,对于每条边(a,b)从a的出点向b的入点连一条(1,inf)的边。然后先跑可行流再反着跑最大流。但是发现一个性质,第一遍跑可行流时一定能够满流,所以我们直接跑第二遍即可,具体连边方法:

1.S->每个点的出点,每个点的入点->T 容量inf
2.每个点的入点->出点 容量inf
3.对于(a,b),a的出点->b的入点 容量inf,a的出点->S,b的入点->T 容量1

ans=m-从T到S的最大流

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int inf=1<<30;
int n,m,S,T,cnt,ans;
int to[100000],next[100000],val[100000],head[1000],d[1000],m1[1000],m2[1000];
queue<int> q;
int rd()
{int ret=0,f=1;   char gc=getchar();while(gc<'0'||gc>'9')  {if(gc=='-')    f=-f;  gc=getchar();}while(gc>='0'&&gc<='9')  ret=ret*10+gc-'0',gc=getchar();return ret*f;
}
void add(int a,int b,int c,int d)
{to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;to[cnt]=a,val[cnt]=d,next[cnt]=head[b],head[b]=cnt++;
}
int dfs(int x,int mf)
{if(x==T) return mf;int i,k,temp=mf;for(i=head[x];i!=-1;i=next[i]){if(d[to[i]]==d[x]+1&&val[i]){k=dfs(to[i],min(temp,val[i]));if(!k)  d[to[i]]=0;val[i]-=k,val[i^1]+=k,temp-=k;if(!temp) break;}}return mf-temp;
}
int bfs()
{while(!q.empty())  q.pop();memset(d,0,sizeof(d));int i,u;q.push(S),d[S]=1;while(!q.empty()){u=q.front(),q.pop();for(i=head[u];i!=-1;i=next[i]){if(!d[to[i]]&&val[i]){d[to[i]]=d[u]+1;if(to[i]==T) return 1;q.push(to[i]);}}}return 0;
}
int main()
{n=rd(),S=0,T=2*n+1;int i,a,b;memset(head,-1,sizeof(head));for(i=1;i<=n;i++){a=rd(),m1[i]=a,ans+=a;while(a--)    b=rd(),add(i,b+n,inf,0),m2[b]++;}for(i=1;i<=n;i++)   add(S,i,inf,m1[i]),add(i+n,T,inf,m2[i]),add(i+n,i,inf,0);swap(S,T);while(bfs())   ans-=dfs(S,inf);printf("%d",ans);return 0;
}

转载于:https://www.cnblogs.com/CQzhangyu/p/7297651.html

【BZOJ2625】[Neerc2009]Inspection 最小流相关推荐

  1. 有上下界网络流 ---- P4843 清理雪道(DAG图上最小路径重复边覆盖)【模板】有源汇上下界最小流

    题目链接 题目大意: 解题思路: 首先我们发现对于每条边至少要覆盖一次,最多覆盖无数次 那么就有点像上下界网络流了[1,INF][1,INF][1,INF]的限制关系 跑一边最小流就可以了!! #in ...

  2. HDU 3157 Crazy Circuits(有源汇上下界最小流)

    HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...

  3. SGU-176 Flow construction 有上下界的最小流

    这里参看了大牛的解题思路,学习了很多.原来上下界流的求法是这么的灵活,尤其我是用的临界表存储的边,删除更新很不方便. http://www.shuizilong.com/house/archives/ ...

  4. BZOJ 2502: 清理雪道 [最小流]

    2502: 清理雪道 题意:任意点出发任意次每条边至少经过一次最小花费. 下界1,裸最小流.... #include <iostream> #include <cstdio> ...

  5. 【有源汇点上下界最小流】[SGU176]Flow construction

    题目大意 给出N个点,M条有向边 如果有向边的标号是1的话,就表示该边的上界下界都为容量 如果有向边的标号为0的哈,表示该边的下界为0,上界为容量 现在问,从1到N的最小流是多少,并输出每条边的流量 ...

  6. [BZOJ2502]清理雪道解题报告|带下界的最小流

    滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机,每次飞 ...

  7. LOJ - #117. 有源汇有上下界最小流(有源汇有上下界的最小流)

    题目链接:点击查看 题目大意:给出一个 n 个点和 m 条边的有向图,每条边都有一个流量限制 [ lower , upper ],给定源点 s 和汇点 t ,求出源点到汇点的最小流 题目分析:参考我的 ...

  8. POJ 3801/HDU 3157 Crazy Circuits | 有下界的最小流

    题目: POJ最近总是炸 所以还是用HDU吧http://acm.hdu.edu.cn/showproblem.php?pid=3157 题解: 题很长,但其实就是给个有源汇带下界网络流(+是源,-是 ...

  9. 图论:有源汇有上下界最小流

    这次就是最小流了 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const ll I ...

最新文章

  1. 手把手教你写一个生成对抗网络
  2. oracle导出超过100m的数据库,Oracle数据库的导出和导入
  3. 使用librosa计算pcen
  4. 自学python需要买书吗-Python真的适合每个人学习吗 学习Python需要多久
  5. 数组去重(JavaScript)先从网上整理一波,待验证
  6. 移动端真机测试怎么做
  7. 沈阳java基础培训,辽宁沈阳java培训学费大概多少
  8. popwindow+动画
  9. 目标规划第四章计算机求解,单纯形算法与目标规划地应用研究.doc
  10. 基于java在线问卷调查系统
  11. 海思Hi3559A Sample_comm_vdec模块解码 视频解码解析
  12. DTCloud编码规范
  13. 四六级、考研英语单词记忆---知米背单词APP推荐!
  14. vue 动态 Prop
  15. c语言中的百分数怎么求,如何计算具体百分比
  16. 奇奇怪怪的大佬:从职业赌徒到互联网大佬
  17. 组建项目团队-执行过程组
  18. 什么是欧拉角/姿态角?
  19. Did you install mysqlclient?
  20. 微信视频号怎么变现挣钱?六大变现操作方式。

热门文章

  1. Bayer Pattern to RGB
  2. 湖北宜昌:老太不慎落入江中 小伙奋勇救人
  3. 在Project 2010中添加自定义任务窗格
  4. 使用EasyUI的Datagrid的Editor进行行编辑,Enter回车结束编辑,并开启新的一行。
  5. IIS调用批处理权限的处理
  6. NodeJS、NPM安装配置与测试步骤(windows版本)
  7. Sql Server数据库数据导入到SQLite数据库中
  8. [Linux] ubuntu 格式化u盘
  9. Nagios安装与配置
  10. Oracle中判断空游标的方法