POJ-2594
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 7035 | Accepted: 2860 |
Description
Recently, a company named EUC (Exploring the Unknown Company) plan
to explore an unknown place on Mars, which is considered full of
treasure. For fast development of technology and bad environment for
human beings, EUC sends some robots to explore the treasure.
To make it easy, we use a graph, which is formed by N points (these N
points are numbered from 1 to N), to represent the places to be
explored. And some points are connected by one-way road, which means
that, through the road, a robot can only move from one end to the other
end, but cannot move back. For some unknown reasons, there is no circle
in this graph. The robots can be sent to any point from Earth by
rockets. After landing, the robot can visit some points through the
roads, and it can choose some points, which are on its roads, to
explore. You should notice that the roads of two different robots may
contain some same point.
For financial reason, EUC wants to use minimal number of robots to explore all the points on Mars.
As an ICPCer, who has excellent programming skill, can your help EUC?
Input
input will consist of several test cases. For each test case, two
integers N (1 <= N <= 500) and M (0 <= M <= 5000) are given
in the first line, indicating the number of points and the number of
one-way roads in the graph respectively. Each of the following M lines
contains two different integers A and B, indicating there is a one-way
from A to B (0 < A, B <= N). The input is terminated by a single
line with two zeros.
Output
Sample Input
1 0
2 1
1 2
2 0
0 0
Sample Output
1
1
2
Source
/**
题意:最小路径覆盖
做法:二分图最大匹配 有向无环图的最小路径覆盖 = 该图的顶点数-该图的最大匹配。
**/
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 510
int g[maxn][maxn];
int linker[maxn];
int used[maxn];
int n,m;
bool dfs(int u)
{
for(int v = ; v<n; v++)
{
if(g[u][v] && used[v] == )
{
used[v] = ;
if(linker[v] == - || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary()
{
int res = ;
memset(linker,-,sizeof(linker));
for(int i=; i<n; i++)
{
memset(used,,sizeof(used));
if(dfs(i)) res++;
}
return res;
} void Floyd()
{
int i,j,k;
for(i=; i<n; i++)
{
for(j=; j<n; j++)
{
if(g[i][j]==)
{
for(k=; k<n; k++)
{
if(g[i][k]==&&g[k][j]==)
{
g[i][j]=;
break;
}
}
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(~scanf("%d %d",&n,&m))
{
if(n == && m == ) break;
memset(g,,sizeof(g));
int u,v;
for(int i=; i<m; i++)
{
scanf("%d %d",&u,&v);
u--;
v--;
g[u][v] = ;
}
Floyd();
int res = hungary();
printf("%d\n",n-res);
}
return ;
}
POJ-2594的更多相关文章
- POJ 2594 Treasure Exploration(最小路径覆盖变形)
POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...
- POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
Treasure Exploration Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- Poj 2594 Treasure Exploration (最小边覆盖+传递闭包)
题目链接: Poj 2594 Treasure Exploration 题目描述: 在外星上有n个点需要机器人去探险,有m条单向路径.问至少需要几个机器人才能遍历完所有的点,一个点可以被多个机器人经过 ...
- POJ 2594 (传递闭包 + 最小路径覆盖)
题目链接: POJ 2594 题目大意:给你 1~N 个点, M 条有向边.问你最少需要多少个机器人,让它们走完所有节点,不同的机器人可以走过同样的一条路,图保证为 DAG. 很明显是 最小可相交路径 ...
- POJ 2594 Treasure Exploration 最小可相交路径覆盖
最小路径覆盖 DAG的最小可相交路径覆盖: 算法:先用floyd求出原图的传递闭包,即如果a到b有路径,那么就加边a->b.然后就转化成了最小不相交路径覆盖问题. 这里解释一下floyd的作用如 ...
- poj 2594 Treasure Exploration(最小路径覆盖+闭包传递)
http://poj.org/problem?id=2594 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total ...
- poj 2594(可相交的最小路径覆盖)
题目链接:http://poj.org/problem?id=2594 思路:本来求最小路径覆盖是不能相交的,那么对于那些本来就可达的点怎么处理,我们可以求一次传递闭包,相当于是加边,这样我们就可以来 ...
- poj 2594 Treasure Exploration (二分匹配)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 6558 Accepted: 2 ...
- POJ 2594 传递闭包的最小路径覆盖
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7171 Accepted: 2 ...
- poj 2594 传递闭包+最大路径覆盖
由于路径可以有重复的点,所以需要将间接相连的点连接 #include<stdio.h> #include<string.h> #include<algorithm> ...
随机推荐
- 【bzoj2743】[HEOI2012]采花 树状数组
题目描述 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于公 ...
- 洛谷 CF1043F Make It One 解题报告
CF1043F Make It One 题意 从一堆数中选择最少的数,使它们的\(\gcd=1\) 输入输出格式 输入格式 第一行:一个正整数\(n\). 第二行:\(n\)个正整数,给出了这个数列. ...
- 【BZOJ 4555】[Tjoi2016&Heoi2016]求和 多项式求逆/NTT+第二类斯特林数
出处0.0用到第二类斯特林数的性质,做法好像很多,我打的是直接ntt,由第二类斯特林数的容斥公式可以推出,我们可以对于每一个i,来一次ntt求出他与所有j组成的第二类斯特林数的值,这个时候我们是O(n ...
- dubbox管理中心
当服务多了,不好管理,可以用管理中心来查看. 管理中心就是一个 web项目(一个war包),可以部署在tomcat下面就能运行了. 用户名密码都是root 每一条服务点开后都可以看生产者和消费者.
- Python数据处理和数据可视化
工具1:numpy 下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy 入门文档:https://docs.scipy.org/doc/nump ...
- Python学习笔记(Django篇)——3、创建第一个数据库模型
Django里面集成了SQLite的数据库,对于初期研究来说,可以用这个学习. 第一步,创建数据库就涉及到建表等一系列的工作,在此之前,要先在cmd执行一个命令: python manage.py ...
- Spring structs2 hibernate 整合(ssh)
ssh项目jar包 项目内容: 1. 加入 Spring 1). 加入 jar 包2). 配置 web.xml 文件3). 加入 Spring 的配置文件.(application.xml) 2. 加 ...
- mysql的数据库 索引
1.两种主要的引擎:MyISAM和InnoDB 2.如何查看自己的表是什么类型:http://www.cnblogs.com/luosongchao/archive/2013/05/23/309592 ...
- 命令行访问远程mysql数据库
命令: mysql -u用户名 -p密码 -h远程数据库IP地址 -P端口 -D数据库名 例子: 使用用root帐号,密码为123456,登录ip地址为192.168.1.110,端口为3306,名称 ...
- redis常用数据结构解析
Redis是一个开源的Key-Value存储引擎,它支持string.hash.list.set和sorted set等多种值类型.由于其卓越的性能表现.丰富的数据类型及稳定性,广泛用于各种需要k/v ...