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 最小路徑覆蓋問題(網路流)
- 利用閉包傳遞引數
- BZOJ1927: [Sdoi2010]星際競速(最小費用最大流 最小路徑覆蓋)
- POJ 3360-Cow Contest(傳遞閉包)
- HDU1874——暢通工程續(floyd最小路)
- 迭代器,閉包,遞迴遞迴
- LeetCode-064-最小路徑和LeetCode
- 最短路徑(dijkstra 與 Floyd)
- 64 - Minimum Path Sum 最小路徑和
- Maven配置覆蓋內嵌tomcat虛擬對映路徑MavenTomcat
- 圖的最短路徑(Dijkstra | Floyd)
- 最短路徑(Floyd演算法)演算法
- 矩形覆蓋
- LeetCode 64號問題 最小路徑和LeetCode
- 51nod 1274 最長遞增路徑
- 最短路徑之Floyd演算法演算法
- [MATLAB]最短路徑Floyd演算法Matlab演算法
- 最小圓覆蓋
- Mysql索引覆蓋MySql索引
- 棋盤覆蓋
- 三角形最小路徑和
- 程式碼覆蓋率與測試覆蓋率比較
- Floyd演算法(計算最短路徑)演算法
- 企業WiFi覆蓋,解決覆蓋四大難題WiFi
- JS函式表示式——函式遞迴、閉包JS函式遞迴
- 全球覆蓋 雜湊
- 線段覆蓋(挖
- idea2022.1 檢視單測覆蓋率展示分支覆蓋率Idea
- 求最短路徑——DFS+Floyd演算法演算法
- BZOJ 1185 [HNOI2007]最小矩形覆蓋:凸包 + 旋轉卡殼
- leetcode 120 三角形最小路徑和LeetCode
- git如何上傳所有的新檔案 gitlab如何上傳所有的新檔案 git本地覆蓋伺服器 強制本地覆蓋伺服器...Gitlab伺服器
- canvas 填充覆蓋描邊Canvas
- php實現矩形覆蓋PHP
- 【劍指Offer】矩形覆蓋
- ESLint: 規則配置覆蓋EsLint