题目描述

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.
给张有向无环图,问至少多少条路径能够覆盖所有的边(可以重复

输入

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.

输出

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.

样例输入

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

样例输出

4


题解

有上下界最小流

题目要求可以从任意点出发到任意点停止,每条边至少经过一次。这显然是最小流问题。

连边S->每个点、每个点->T、T->S、原图中的边,容量为inf;对于每个点x如果入度大于0则连SS->x,容量为ind[x],否则连x->TT,容量为-ind[x]。

跑SS->TT最大流,然后把T->S、SS->x、x->TT边删除,跑T->S最大流,两次答案相减即为答案。

#include <cstdio>
#include <cstring>
#include <queue>
#define N 110
#define M 20010
using namespace std;
const int inf = 1 << 30;
queue<int> q;
int head[N] , to[M] , val[M] , next[M] , cnt = 1 , s , t , dis[N] , ind[N];
void add(int x , int y , int z)
{
to[++cnt] = y , val[cnt] = z , next[cnt] = head[x] , head[x] = cnt;
to[++cnt] = x , val[cnt] = 0 , next[cnt] = head[y] , head[y] = cnt;
}
bool bfs()
{
int x , i;
memset(dis , 0 , sizeof(dis));
while(!q.empty()) q.pop();
dis[s] = 1 , q.push(s);
while(!q.empty())
{
x = q.front() , q.pop();
for(i = head[x] ; i ; i = next[i])
{
if(val[i] && !dis[to[i]])
{
dis[to[i]] = dis[x] + 1;
if(to[i] == t) return 1;
q.push(to[i]);
}
}
}
return 0;
}
int dinic(int x , int low)
{
if(x == t) return low;
int temp = low , i , k;
for(i = head[x] ; i ; i = next[i])
{
if(val[i] && dis[to[i]] == dis[x] + 1)
{
k = dinic(to[i] , min(temp , val[i]));
if(!k) dis[to[i]] = 0;
val[i] -= k , val[i ^ 1] += k;
if(!(temp -= k)) break;
}
}
return low - temp;
}
int main()
{
int n , i , b , e , x , y , ans = 0;
scanf("%d" , &n) , b = 0 , e = n + 1 , s = n + 2 , t = n + 3;
add(e , b , inf);
for(i = 1 ; i <= n ; i ++ )
{
scanf("%d" , &y);
while(y -- ) scanf("%d" , &x) , add(i , x , inf) , ind[i] -- , ind[x] ++ ;
}
for(i = 1 ; i <= n ; i ++ )
{
add(b , i , inf) , add(i , e , inf);
if(ind[i] > 0) add(s , i , ind[i]);
else add(i , t , -ind[i]);
}
while(bfs()) dinic(s , inf);
ans = val[3] , val[2] = val[3] = 0;
for(i = head[s] ; i ; i = next[i]) val[i] = val[i ^ 1] = 0;
for(i = head[t] ; i ; i = next[i]) val[i] = val[i ^ 1] = 0;
s = e , t = b;
while(bfs()) ans -= dinic(s , inf);
printf("%d\n" , ans);
return 0;
}

【bzoj2625】[Neerc2009]Inspection 有上下界最小流的更多相关文章

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

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

  2. BZOJ_2502_清理雪道_有源汇上下界最小流

    BZOJ_2502_清理雪道_有源汇上下界最小流 Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道), ...

  3. 【Loj117】有源汇上下界最小流(网络流)

    [Loj117]有源汇上下界最小流(网络流) 题面 Loj 题解 还是模板题. #include<iostream> #include<cstdio> #include< ...

  4. SGU 176 Flow construction (有源有汇有上下界最小流)

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

  5. loj #117. 有源汇有上下界最小流

    题目链接 有源汇有上下界最小流,->上下界网络流 注意细节,边数组也要算上后加到SS,TT边. #include<cstdio> #include<algorithm> ...

  6. LOJ.117.[模板]有源汇有上下界最小流(Dinic)

    题目链接 有源汇有上下界最小流 Sol1. 首先和无源汇网络流一样建图,求SS->TT最大流: 然后连边(T->S,[0,INF]),再求一遍SS->TT最大流,答案为新添加边的流量 ...

  7. BZOJ1458:士兵占领(有上下界最小流)

    Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放 ...

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

    题意:一个电路板,上面有N个接线柱(标号1~N)   还有两个电源接线柱  +  - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流. 析:这是一个有源汇有上下界的 ...

  9. hdu3157有源汇上下界最小流

    题意:有源汇上下界最小流裸题,主要就是输入要用字符串的问题 #include<bits/stdc++.h> #define fi first #define se second #defi ...

随机推荐

  1. Windows 7, Visual Studio 2015下编译Webkit

    因工作需要,需要编译Windows版本的Webkit,中间走了不少弯路,都记录下来,供大家参考!也随时欢迎大家讨论(QQ群:345802342) 整个编译工作参考的是官方文档:https://webk ...

  2. BZOJ 2539: [Ctsc2000]丘比特的烦恼

    Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 695  Solved: 260[Submit][Status][Discuss] Description ...

  3. Spoj REPEATS 后缀自动机+set

    REPEATS - Repeats 链接:http://www.spoj.com/problems/REPEATS 题意:求S串中某个子串连续循环次数最多的次数. 想法: 从暴力开始,枚举所有串,求出 ...

  4. 字符编码:WideCharToMultiByte

    WideCharToMultiByte 编辑   目录 1基本介绍及功能 2相关变量     1基本介绍及功能编辑 WideCharToMultiByte 函数功能:该函数映射一个unicode字符串 ...

  5. RuntimeError: cryptography is required for sha256_password or caching_sha2_p

    报错原因:mysql版本身份验证出现问题引起的 我这里报错的地方是在Django里,pycharm连接数据库时出现的 解决办法,安装安装cryptography即可:pip install crypt ...

  6. 约束Constraints

    1.setNeedsUpdateConstraints:当想要调整子视图布局时,在主线程调用该方法标记constraint需要在未来的某个点更新(该方法不会立刻强制刷新constraint,而是等待下 ...

  7. Create & use FTP service on Ubuntu(在Ubuntu上搭建并使用FTP服务)

    Check if the FTP service has been installed.(检查是否已安装)   Vsftpd --version  If it has not install,Pres ...

  8. Linux菜鸟起飞之路【八】文本编辑器

    在Linux中,文本编辑器有两个,VI和VIM.这两个编辑器用法差不多,但vim是vi的升级版,所以功能更强大一些. vim编辑器一共有三种模式,命令行模式.编辑模式和扩展模式. 进入vim界面,首先 ...

  9. 控制nginx并发链接数量和客户端请求nginx的速率

    一.控制nginx并发链接数 ngx_http_limit_conn_module这个模块用于限制每个定义的key值的链接数,特别是单IP的链接数. 不是所有的链接数都会被计数,一个符合计数要求的连接 ...

  10. DeepFaceLab小白入门(2):软件安装!

    严格上来说这个软件本身并不需要安装,他唯一需要的就是对应版本的显卡驱动,CUDA和CuDNN都非必须.下面我说一下如何安装正确的驱动版本.我尽量写得简洁清晰,希望大家都能看懂,但是,如果你连基本的电脑 ...