poj2594Treasure Exploration【最小路徑覆蓋+floyd傳遞閉包】
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 0 2 1 1 2 2 0 0 0
Sample Output
1 1 2
看到這個題想到是最小路徑覆蓋了,一頓亂套模板,也想到是單向加邊了,但是floyd閉包頭一次聽說並且使用(讓我想起來JS的閉包了==)就是i->j j->k 求得i->k
#include<cstdio>
#include<cstring>
using namespace std;
/* **************************************************************************
//二分圖匹配(匈牙利演算法的DFS實現)
//初始化:g[][]兩邊頂點的劃分情況
//建立g[i][j]表示i->j的有向邊就可以了,是左邊向右邊的匹配
//g沒有邊相連則初始化為0
//uN是匹配左邊的頂點數,vN是匹配右邊的頂點數
//呼叫:res=hungary();輸出最大匹配數
//優點:適用於稠密圖,DFS找增廣路,實現簡潔易於理解
//時間複雜度:O(VE)
//***************************************************************************/
//頂點編號從0開始的
const int MAXN=509;
int n;//u,v數目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
// int k,m,n,u,v;
void Floyd(int N) //求傳遞閉包
{
for(int k = 1; k <= N; ++k)
{
for(int i = 1; i <= N; ++i)
{
for(int j = 1; j <= N; ++j)
{
if(i != j && g[i][k] && g[k][j])
g[i][j] = 1;
}
}
}
}
bool dfs(int u)//從左邊開始找增廣路徑
{
int v;
for(v=1;v<=n;v++)//這個頂點編號從0開始,若要從1開始需要修改
if(g[u][v]&&!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{//找增廣路,反向
linker[v]=u;
return true;
}
}
return false;//這個不要忘了,經常忘記這句
}
int hungary()
{
int res=0;
int u;
memset(linker,-1,sizeof(linker));
for(u=1;u<=n;u++)
{
memset(used,0,sizeof(used));
if(dfs(u)) res++;
}
return res;
}
//******************************************************************************/
int main()
{
//freopen("cin.txt","r",stdin);
int m;
while(~scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
// printf("n=%d,m=%d\n",n,m);
if(m==0) {printf("%d\n",n);continue;}
memset(g,0,sizeof(g));
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
g[u][v]=1;
}
Floyd(n);
printf("%d\n",n-hungary());
}
return 0;
}
相關文章
- 最小路徑可重複點覆蓋
- 最大匹配、最小頂點覆蓋、最大獨立集、最小路徑覆蓋(轉)(再轉)
- POJ3660 Cow Contest【Floyd演算法 傳遞閉包】演算法
- 洛谷OJ:P2764 最小路徑覆蓋問題(網路流)
- HDU 4606 Occupy Cities (計算幾何+最短路+最小路徑覆蓋)
- 利用閉包傳遞引數
- BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比賽:floyd傳遞閉包
- POJ 3360-Cow Contest(傳遞閉包)
- [20160213]閉包傳遞5.txt
- [20160212]閉包傳遞4.txt
- [20160211]閉包傳遞3.txt
- [20160210]閉包傳遞2.txt
- [20160125]閉包傳遞問題.txt
- swift 閉包傳值Swift
- 最短路徑(Floyd演算法)演算法
- 64 - Minimum Path Sum 最小路徑和
- Mysql索引覆蓋MySql索引
- 最小圓覆蓋
- 程式碼覆蓋率與測試覆蓋率比較
- 最短路徑之Floyd演算法演算法
- LeetCode-064-最小路徑和LeetCode
- 三角形最小路徑和
- 企業WiFi覆蓋,解決覆蓋四大難題WiFi
- 第9條:覆蓋equals時總要覆蓋hashCode方法
- Java方法覆蓋和變數覆蓋的區別詳解Java變數
- 51nod 1274 最長遞增路徑
- 程式碼覆蓋率測試:從誤傳到現實
- EMMA 覆蓋率工具
- [MATLAB]最短路徑Floyd演算法Matlab演算法
- Floyd演算法(計算最短路徑)演算法
- 語法:Python中的可覆蓋物件與不可覆蓋物件Python物件
- LeetCode 64號問題 最小路徑和LeetCode
- JS函式表示式——函式遞迴、閉包JS函式遞迴
- canvas 填充覆蓋描邊Canvas
- pHp程式碼覆蓋率PHP
- php實現矩形覆蓋PHP
- 程式碼覆蓋率分析
- 線段覆蓋問題