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> ...
随机推荐
- BZOJ3427 Poi2013 Bytecomputer 【dp】
题目链接 BZOJ3427 题解 容易发现最终序列一定是\(\{-1,0,1\}\)组成的 因为如果有一个位置不是,那么这个位置一定大于\(1\),那么上一个位置一定为\(1\),所以该位置一定加到过 ...
- HDOJ(HDU).1864 最大报销额 (贪心)
HDOJ(HDU).1864 最大报销额 题意分析 题目有点问题,原题中说的 单项物品的价值不得超过600元 应该是单类物品的价值不能超过600元. 一开始以为是01背包,后来按贪心写过了. 一张一张 ...
- [转]Android 如何根据网络地址获取网络图片方法
http://blog.csdn.net/xiazdong/article/details/7724103 目录(?)[-] h2pre namecode classhtml stylefont-we ...
- Codeforces Round #401 (Div. 2) A B C 水 贪心 dp
A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...
- Eclipse集成Android NDK及导出Jar和so动态库
一.安装Cygwin 在Windows环境而又不想使用linux环境,可以安装cygwin(http://www.cygwin.com/ ),为了使用gcc注意cygwin的必选安装包在devel目录 ...
- Java中的String为什么是不可变的? — String源码分析
原文地址:http://www.importnew.com/16817.html 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为 ...
- linux用户登录指定目录
一.创建用户和用户组 [root@web4 lianyu]# groupadd lianyu [root@web4 lianyu]# useradd lianyu -g lianyu [root@we ...
- maven中package和install命令的区别
mvn package 编译并打包工程,根据pom文件中packaging是jar还是war进行打包,会在项目的target目录下生成一个jar/war包. mvn install 打包并安装到mav ...
- 2015/8/28 Python基础(2):对象
Python用对象模型来存储数据.构造任何类型的值都是一个对象.Python对象都有是三个特性:身份,类型和值 身份是每个对象的唯一身份标识.任何对象都可以用内建函数id()来得到身份.如: > ...
- .net core 安装Swagger
Install-Package Swashbuckle -Pre 1.Startup // This method gets called by the runtime. Use this metho ...