hihoCoder 网络流四·最小路径覆盖
hihoCoder感觉很好。
网络流的精华就是建图
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int point;
int weight;
int nxt;
};
node line[5000000];
int head[50000],tail=-1;
void add(int x,int y,int z)
{
line[++tail].point=y;
line[tail].weight=z;
line[tail].nxt=head[x];
head[x]=tail;
line[++tail].point=x;
line[tail].weight=0;
line[tail].nxt=head[y];
head[y]=tail;
}
int cur[50000],dep[50000];
bool BFS(int begin,int end)
{
for(int i=begin;i<=end;i++)
{
cur[i]=head[i];
dep[i]=0;
}
dep[begin]=1;
queue<int>q;
q.push(begin);
while(!q.empty())
{
int pas=q.front();
q.pop();
for(int i=head[pas];i!=-1;i=line[i].nxt)
if(!dep[line[i].point]&&line[i].weight)
{
dep[line[i].point]=dep[pas]+1;
q.push(line[i].point);
}
}
if(dep[end])
return true;
return false;
}
int DFS(int now,int aim,int limte)
{
if(now==aim||!limte)
return limte;
int flow=0,f;
for(int i=cur[now];i!=-1;i=line[i].nxt)
{
cur[now]=i;
if(dep[line[i].point]==dep[now]+1&&(f=DFS(line[i].point,aim,min(limte,line[i].weight))))
{
limte-=f;
flow+=f;
line[i].weight-=f;
line[i^1].weight+=f;
if(!limte)
break;
}
}
return flow;
}
int dinic(int begin,int end)
{
int res=0;
while(BFS(begin,end))
res+=DFS(begin,end,0x7fffffff);
return res;
}//以上都是标准的dinic。ISAP就是个垃圾
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<=2*n+1;i++)//0为起点,2*n+1为终点
head[i]=-1;
int a,b;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
add(a,b+n,1);
}
for(int i=1;i<=n;i++)
add(0,i,1);
for(int i=1;i<=n;i++)
add(n+i,2*n+1,1);
int ans=dinic(0,2*n+1);
printf("%d",n-ans);
}
hihoCoder 网络流四·最小路径覆盖的更多相关文章
- hihocoder #1394 : 网络流四·最小路径覆盖(最小路径覆盖)
#1394 : 网络流四·最小路径覆盖 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 国庆期间正是旅游和游玩的高峰期. 小Hi和小Ho的学习小组为了研究课题,决定趁此机 ...
- hihoCoder 1394 : 网络流四·最小路径覆盖
题目链接:https://hihocoder.com/problemset/problem/1394 题目说是网络流,但是其实就是求有向无环图的最小路径覆盖. 不会网络流,只好用二分匹配了. 把每个点 ...
- ●hihocoder #1394 网络流四·最小路径覆盖
题链: http://hihocoder.com/problemset/problem/1394 题解: 有向图最小路径覆盖:最少的路径条数不重不漏的覆盖所有点. 注意到在任意一个最小路径覆盖的方案下 ...
- hiho 第118周 网络流四·最小路径覆盖
描述 国庆期间正是旅游和游玩的高峰期. 小Hi和小Ho的学习小组为了研究课题,决定趁此机会派出若干个调查团去沿途查看一下H市内各个景点的游客情况. H市一共有N个旅游景点(编号1..N),由M条单向游 ...
- [HihoCoder1394]网络流四·最小路径覆盖
题目大意:从有向无环图中选出若干点不想交的链,使得这些链覆盖所有的点,并且链的条数最小. 思路:设超级源点$S$.超级汇点$T$.将$N$个点复制一份,分为$A$部和$B$部.对于$A$部的所有点$A ...
- 网络流二十四题之P2764 最小路径覆盖问题
题目描述 给定有向图 G=(V,E)G=(V,E) .设 PP 是 GG 的一个简单路(顶点不相交)的集合.如果 VV 中每个定点恰好在PP的一条路上,则称 PP 是 GG 的一个路径覆盖.PP中路径 ...
- 【网络流24题】 No.3 最小路径覆盖问题 (网络流|匈牙利算法 ->最大二分匹配)
[题意] 给定有向图 G=(V,E).设 P 是 G 的一个简单路(顶点不相交) 的集合.如果 V 中每个顶点恰好在 P 的一条路上,则称 P 是 G 的一个路径覆盖. P 中路径可以从 V 的任何一 ...
- LOJ6002 - 「网络流 24 题」最小路径覆盖
原题链接 Description 求一个DAG的最小路径覆盖,并输出一种方案. Solution 模板题啦~ Code //「网络流 24 题」最小路径覆盖 #include <cstdio&g ...
- LibreOJ 6003. 「网络流 24 题」魔术球 贪心或者最小路径覆盖
6003. 「网络流 24 题」魔术球 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 ...
随机推荐
- SpringBoot---数据缓存(未完待续)
1.概述 1.1 在Spring中使用缓存的关键是配置CacheManager,而SpringBoot为我们自动配置了多个CacheManager的实现: 1.2 SpringBoot的CacheMa ...
- .NET控制台程序监听程序退出
There are mainly 2 types of Win32 applications, console application and window application. They hav ...
- DedeCms中Channel用typeid无效
DedeCms中channel 用typeid调用无法达目的吗?请换成type试试! {dede:channel type='son' typeid='19' row='1'} <a href= ...
- pat1091. Acute Stroke (30)
1091. Acute Stroke (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One impo ...
- HDU 4009——Transfer water——————【最小树形图、不定根】
Transfer water Time Limit:3000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u Subm ...
- UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】
Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently a ...
- Day3下
少女[问题描述]你是能看到第一题的 friends呢.—— hja少女在图上开车, 她们希望把每条边分配给与其相连的点中一个并且每个点最多被分配一条边,问可能的方案数.[输入格式]第一行两个整数
- 关于EasyUI datagrid 表头居中 数据列内容居右 或者居左
cell.css("text-align",(col.halign||col.align||"")); 这里有个属性挺眼熟 : col.align 前面还有一个 ...
- Java I/O模型
本文转发自技术世界,原文链接 http://www.jasongj.com/java/nio_reactor/ 同步 vs. 异步 同步I/O 每个请求必须逐个地被处理,一个请求的处理会导致整个流程的 ...
- 资源管理与调度系统-YARN的基本架构与原理
资源管理与调度系统-YARN的基本架构与原理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 为了能够对集群中的资源进行统一管理和调度,Hadoop2.0引入了数据操作系统YARN. ...