HDU 2594 Treasure Exploration(有向图最小可重叠路径覆盖)
来源:互联网 发布:文件恢复软件recovery 编辑:程序博客网 时间:2024/06/03 18:38
Treasure Exploration
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 8570 Accepted: 3500
Description
Have you ever read any book about treasure exploration? Have you ever see any film about treasure exploration? Have you ever explored treasure? If you never have such experiences, you would never know what fun treasure exploring brings to you.
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?
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
The 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
For each test of the input, print a line containing the least robots needed.
Sample Input
1 02 11 22 00 0
Sample Output
112
Source
POJ Monthly--2005.08.28,Li Haoyuan
题目大意:
给你一个有向图,让你找出最少的可重叠路径,使得它们能够覆盖全部点。
解题思路:
这题和POJ 1422很像,我最开始也把这道题当成和那道题解法一样了,不过仔细读题可以发现POJ 1422的路径是不能重叠的,也就是通常说的有向图最小路径覆盖问题。而这题不同路径之间是可以有重叠部分的,这就导致了两题的答案不同。
仔细思考就可以发现,造成答案不同的只有一种情况,就是两条路径开始部分和起点部分是相互独立的,但是中间有了一段公共部分,按照最小路径覆盖这样会把其中一条路径拆成两条。如果我们能够把那一段公共路径跳过去就可以用最小路径覆盖解决这题了,于是我们就可以利用传递闭包。
那么这题的解法就出来了,先对原图求传递闭包,然后在新图上跑有向图最小路径覆盖,得到的结果就是有向图最小可重叠路径覆盖。
AC代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <ctime>#include <vector>#include <queue>#include <stack>#include <deque>#include <string>#include <map>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define fi first#define se second#define mem(a,b) memset((a),(b),sizeof(a))const int MAXV=500+3;int V,E;bool G[MAXV][MAXV];int num_x;//左边顶点数int match_x[MAXV],match_y[MAXV];//顶点匹配对象int dis,dis_x[MAXV],dis_y[MAXV];//距离(用于多路增广)bool used[MAXV];bool searchP()//标记距离(用于多路增广){ queue<int> que; dis=INF; mem(dis_x,-1); mem(dis_y,-1); for(int i=0;i<num_x;++i) if(match_x[i]==-1) { que.push(i); dis_x[i]=0; } while(!que.empty()) { int u=que.front(); que.pop(); if(dis_x[u]>dis) break; for(int v=0;v<V;++v) if(G[u][v]) { if(dis_y[v]==-1) { dis_y[v]=dis_x[u]+1; if(match_y[v]==-1) dis=dis_y[v]; else { dis_x[match_y[v]]=dis_y[v]+1; que.push(match_y[v]); } } } } return dis!=INF;}bool dfs(int u)//dfs增广{ for(int v=0;v<V;++v) if(G[u][v]) { if(!used[v]&&dis_y[v]==dis_x[u]+1) { used[v]=true; if(match_y[v]!=-1&&dis_y[v]==dis) continue; if(match_y[v]==-1||dfs(match_y[v])) { match_y[v]=u; match_x[u]=v; return true; } } } return false;}int hopcroft_carp()//二分图最大匹配{ int res=0; mem(match_x,-1); mem(match_y,-1); while(searchP()) { mem(used,0); for(int i=0;i<num_x;++i) if(match_x[i]==-1&&dfs(i)) ++res; } return res;}void init()//初始化{ for(int i=0;i<V;++i) for(int j=0;j<V;++j) G[i][j]=false;}int main(){ while(~scanf("%d%d",&V,&E)&&(V||E)) { init(); for(int i=0;i<E;++i) { int u,v; scanf("%d%d",&u,&v); G[u-1][v-1]=true; } for(int k=0;k<V;++k)//floyd求传递闭包 for(int i=0;i<V;++i) for(int j=0;j<V;++j) G[i][j]|=G[i][k]&G[k][j]; num_x=V; printf("%d\n",V-hopcroft_carp()); } return 0;}
阅读全文
0 0
- HDU 2594 Treasure Exploration(有向图最小可重叠路径覆盖)
- Treasure Exploration(POJ_2594) -有向图可重复点-最小路径覆盖
- Treasure Exploration----有路径重叠的最小路径覆盖
- 可重叠的最小路径覆盖 poj2594 Treasure Exploration
- POJ-2594-Treasure Exploration(可重点最小路径覆盖)
- POJ 2594 Treasure Exploration-有向图最小路径覆盖&Floyd
- Poj 2594 Treasure Exploration【可相交最小路径覆盖】
- poj2594 Treasure Exploration(可相交最小路径覆盖)
- poj 2594 Treasure Exploration 可重复覆盖的二分图最小覆盖路径
- poj 2594 Treasure Exploration(最小路径覆盖(可重点)+floyd)
- POJ 2594 Treasure Exploration (最小路径覆盖+传递闭包(解决可重点))
- POJ 2594 Treasure Exploration 最小路径覆盖
- POJ2594 Treasure Exploration(最小路径覆盖)
- POJ 2594 Treasure Exploration(最小路径覆盖变形)
- POJ 题目2594 Treasure Exploration(最小路径覆盖)
- POJ 2594 Treasure Exploration(最小路径覆盖-hungary+Floyd)
- POJ 2594 Treasure Exploration(最小路径覆盖)
- POJ-2594 Treasure Exploration(最小路径覆盖)
- Python中数据可视化方法汇总
- 窗体位置设置StartPosition属性(转)
- Hadoop集群配置(最全面总结)
- PHP学习1
- iOS根据model里面的时间字段进行排序
- HDU 2594 Treasure Exploration(有向图最小可重叠路径覆盖)
- BZOJ 1093: [ZJOI2007]最大半连通子图
- 物联网的工业设备接入因特网之我见
- 微信公众平台发送客服消息返回 45047说明
- JavaScript基础随笔 pop()方法
- shell脚本学习第三天——特殊变量
- 三道NOIP(?)巧题
- 使用存储过程实现删除指定列名上存在的索引
- sublime部分插件安装详解