【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)
題干:
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?
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 0Sample Output
1 1 2題目大意:
給一張n個點的有向圖,問最少放幾個機器人能走完整個圖,機器人可以任意選一個點放置,一個點可以被多個機器人經過(例如1->3,2->3,3->4,3->5.這種情況需要兩個機器人),保證圖中沒有環, 不保證圖連通。
解題報告:
因為我們要選擇最少的路徑來覆蓋所有的點,那么顯然這是一個最小路徑覆蓋問題。但是這題有個問題就在于一個點可以被多個機器人經過(這就是和單純的最小路徑覆蓋的區別)。如果我們用裸的最小路徑覆蓋,那么“題目大意”里給的那個樣例應該輸出3,但是其實應該輸出2,這也就是說我們應該讓每一個點可以到達的點全都置為true,然后再跑二分圖,這樣得到的才是正解、
做法就是求個floyd傳遞閉包,其實鄰接表建圖然后用Dijkstra也可以,好像這樣更快?反正floyd是800ms才水過。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 555 + 5; bool f[MAX][MAX]; int n,m; void floyd() {for(int k = 1; k<=n; k++) {for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(f[i][k] && f[k][j]) f[i][j] = 1;}}} } bool used[MAX]; int nxt[MAX]; bool find(int x) {for(int i = 1; i<=n; i++) {if(used[i] == 0 && f[x][i]) {used[i] = 1;if(nxt[i] == 0|| find(nxt[i])) {nxt[i] = x;return 1;} }}return 0 ; } int match() {int res = 0;for(int i = 1; i<=n; i++) {memset(used,0,sizeof used);if(find(i)) res++;}return res; } int main() {while(~scanf("%d%d",&n,&m) && n+m) {memset(f,0,sizeof f);memset(nxt,0,sizeof nxt);for(int a,b,i = 1; i<=m; i++) {scanf("%d%d",&a,&b);f[a][b]=1;}floyd();printf("%d\n",n - match());}return 0 ; }?
總結:
這題可以用強連通分量嗎?
這題說了沒有環,也就是沒有強連通分量啊,tarjan沒用啊。。。
能不能用樹形dp?
說了沒有環不代表他就是樹,比如這個樣例:
8 8 1 3 2 3 3 4 3 5 4 6 5 6 6 7 6 8 最終應該輸出2?
總結
以上是生活随笔為你收集整理的【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wininfo.exe - wininf
- 下一篇: 【HDU - 5875】Function