【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;
}

【BZOJ2625】[Neerc2009]Inspection 最小流的更多相关文章

  1. 【bzoj2625】[Neerc2009]Inspection 有上下界最小流

    题目描述 You are in charge of a team that inspects a new ski resort. A ski resort is situated on several ...

  2. UVaLive 4597 Inspection (网络流,最小流)

    题意:给出一张有向图,每次你可以从图中的任意一点出发,经过若干条边后停止,然后问你最少走几次可以将图中的每条边都走过至少一次,并且要输出方案,这个转化为网络流的话,就相当于 求一个最小流,并且存在下界 ...

  3. UVa 1440:Inspection(带下界的最小流)***

    https://vjudge.net/problem/UVA-1440 题意:给出一个图,要求每条边都必须至少走一次,问最少需要一笔画多少次. 思路:看了好久才勉强看懂模板.良心推荐:学习地址. 看完 ...

  4. 【BZOJ-2502】清理雪道 有上下界的网络流(有下界的最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 594  Solved: 318[Submit][Status][Discuss] ...

  5. 【BZOJ-2893】征服王 最大费用最大流(带下界最小流)

    2893: 征服王 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 156  Solved: 48[Submit][Status][Discuss] D ...

  6. HDU3157 Crazy Circuits(有源汇流量有上下界网络的最小流)

    题目大概给一个电路,电路上有n+2个结点,其中有两个分别是电源和负载,结点们由m个单向的部件相连,每个部件都有最少需要的电流,求使整个电路运转需要的最少电流. 容量网络的构建很容易,建好后就是一个有源 ...

  7. POJ 3801 有上下界最小流

    1: /** 2: POJ 3801 有上下界的最小流 3: 4: 1.对supersrc到supersink 求一次最大流,记为f1.(在有源汇的情况下,先使整个网络趋向必须边尽量满足的情况) 5: ...

  8. bzoj 2502 清理雪道(有源汇的上下界最小流)

    [题意] 有一个DAG,要求每条边必须经过一次,求最少经过次数. [思路] 有上下界的最小流.  边的下界为1,上界为无穷.构造可行流模型,先不加ts边跑一遍最大流,然后加上t->s的inf边跑 ...

  9. sgu 176 Flow construction(有源汇的上下界最小流)

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11025 [模型] 有源汇点的上下界最小流.即既满足上下界又满足 ...

随机推荐

  1. Pebbles(hdu 2167)

    题意:给定一个N*N的方格,让你在里面取出一些数使其和最大,要求每一个数不能与其相邻的8个数同时取出. /* 和hdu1565差不多,只不过在更新的时候考虑斜上方的格子对该格子的影响. */ #inc ...

  2. Java--消除重复数字后的最大值

    描述: 一个长整型数字,消除重复的数字后,得到最大的一个数字. 如12341 ,消除重复的1,可得到1234或2341,取最大值2341. 42234,消除4 得到4223 或者 2234 ,再消除2 ...

  3. Oracle Dual 表详解

    1.DUAL表的用途Dual 是 Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中--查看当前连接用户SQL> select user from dua ...

  4. AC日记——小木棍【数据加强版】 洛谷 P1120

    题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段小木棍的长度,编 ...

  5. iOS 总结网页常用的东西

    webView与js交互常用JS语句::: 1. //禁用用户选择 [self.webView stringByEvaluatingJavaScriptFromString:@"docume ...

  6. Oracle服务扫描工具Oscanner

    Oracle服务扫描工具Oscanner   Oracle是甲骨文公司推出的关系型数据库,适用于中大规模数据存储,如大型企业.电信.银行等行业.Kali Linux集成了Oracle服务扫描专向工具O ...

  7. IE使用多彩文档上传数据库报错

    使用多彩文档,用IE浏览器提交表单,双引号里面包含单引号,导致数据库插入不了,而用chrome浏览器不会报错,自动过滤单引号, 解决:content.replace("'", &q ...

  8. 我是如何通过CSRF拿到Shell的

    织梦内容管理系统(DedeCms) 以简单.实用.开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户最多的PHP类CMS系统,在经历多年的发展,目前的版本无论在功能,还是在易用性方面,都有 ...

  9. jquery+css实现网页颜色主题变换(只改变已设置好的几种颜色主题)

    又遇到颜色主题变化,这次使用了jquery+css,使用了函数传值,而不是之前网站换肤改变link的方法. 首先是设置好颜色主题后,点击改变页面颜色主题.(需要自行导入jquery.js后查看效果) ...

  10. AVOS Cloud 技术支持系统开源了

    非常高兴跟大家说.工单系统(技术支持系统)开源了.代码托管在了Github上. 假设还未见识过工单系统,请移步于 https://ticket.avosapps.com/ 这个系统是用 AVOS Cl ...