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> ...
随机推荐
- HDOJ.1075 What Are You Talking About(map)
What Are You Talking About 点我跳转到题面 点我一起学习STL-MAP 题意分析 首先第一组START-END给出翻译的字典,第二组START-END给出一句话,查找里面出现 ...
- sourcemap总结
sourcemap在线上压缩文件调试中很重要,在此总结如下: 1. 开启sourcemap (1). 浏览器要开启source-map支持(2). 压缩文件底部要有source-map的URL,压缩要 ...
- maven根据不同的运行环境,打包不同的配置文件(转载)
使用maven管理项目中的依赖,非常的方便.同时利用maven内置的各种插件,在命令行模式下完成打包.部署等操作,可方便后期的持续集成使用. 但是每一个maven工程(比如web项目),开发人员在开发 ...
- Gaby And Addition Gym - 101466A (初学字典树)
Gaby is a little baby who loves playing with numbers. Recently she has learned how to add 2 numbers ...
- Naive Operations HDU多校(线段树上线段果)
Problem Description In a galaxy far, far away, there are two integer sequence a and b of length n.b ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- [linux]ubuntu限速软件
wondersharper 1 安装wondershaper:sudo apt-get install wondershaper2 限制下载,上传速度(1500是限制下载速度(实际限速150k左右), ...
- ACM1004 Let the balloons fly
These code is for the problem "Let the balloons Fly" in ACM 1004 which need deal with stri ...
- uboot主Makefile分析(t配置和编译过程详解)
1.编译uboot前需要三次make make distcleanmake x210_sd_configmake -j4 make distclean为清楚dist文件. make x210_sd_c ...
- 洛谷 P1722 矩阵 II
题目背景 usqwedf 改编系列题. 题目描述 如果你在百忙之中抽空看题,请自动跳到第六行. 众所周知,在中国古代算筹中,红为正,黑为负…… 给定一个1*(2n)的矩阵(usqwedf:这不是一个2 ...