您的位置:首页 > 其它

二分图(最小路径覆盖)

2015-02-04 00:37 267 查看
求最小路径覆盖通常要拆点,将一个点拆分为两个

Description

Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach the same intersection i.e. the town's streets
form no cycles.

With these assumptions your task is to write a program that finds the minimum number of paratroopers that can descend on the town and visit all the intersections of this town in such a way that more than one paratrooper visits no intersection. Each paratrooper
lands at an intersection and can visit other intersections following the town streets. There are no restrictions about the starting intersection for each paratrooper.

Input

Your program should read sets of data. The first line of the input file contains the number of the data sets. Each data set specifies the structure of a town and has the format:

no_of_intersections

no_of_streets

S1 E1

S2 E2

......

Sno_of_streets Eno_of_streets

The first line of each data set contains a positive integer no_of_intersections (greater than 0 and less or equal to 120), which is the number of intersections in the town. The second line contains a positive integer no_of_streets, which is the number of streets
in the town. The next no_of_streets lines, one for each street in the town, are randomly ordered and represent the town's streets. The line corresponding to street k (k <= no_of_streets) consists of two positive integers, separated by one blank: Sk (1 <= Sk
<= no_of_intersections) - the number of the intersection that is the start of the street, and Ek (1 <= Ek <= no_of_intersections) - the number of the intersection that is the end of the street. Intersections are represented by integers from 1 to no_of_intersections.

There are no blank lines between consecutive sets of data. Input data are correct.

Output

The result of the program is on standard output. For each input data set the program prints on a single line, starting from the beginning of the line, one integer: the minimum number of paratroopers required to visit all the intersections in the town.

Sample Input
2
4
3
3 4
1 3
2 3
3
3
1 3
1 2
2 3

Sample Output
2
1

Source

Dhaka 2002
题意:


一个地图上有n个小镇,以及连接着其中两个小镇的有向边,而且这些边无法形成回路。


现在选择一些小镇空降士兵(1个小镇最多1个士兵),士兵能沿着边走到尽头,问最少


空降几个士兵,能遍历完所有的小镇。

最小路径覆盖问题:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
int mp[330][330];
int used[330],link[330];
int t,m,n;
bool dfs(int x)
{
    REPF(i,1,n)
    {
        if(mp[x][i]&&!used[i])
        {
            used[i]=true;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int x,y;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        CLEAR(mp,0);
        REP(i,m)
        {
            scanf("%d%d",&x,&y);
            mp[x][y]=1;
        }
        int ans=0;
        CLEAR(link,-1);
        REPF(i,1,n)
        {
            CLEAR(used,0);
            if(dfs(i))  ans++;
        }
        printf("%d\n",n-ans);
    }
    return 0;
}


POJ 2594 :

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?
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

Source

POJ Monthly--2005.08.28,Li Haoyuan
此题跟上题不同之处在于此题的点可以重新走,故要用floyd求闭包。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF=0x3f3f3f3f;
int mp[550][550],dis[550][550];
int used[550],link[550];
int t,m,n;
bool dfs(int x)
{
    REPF(i,1,n)
    {
        if(mp[x][i]&&!used[i])
        {
            used[i]=true;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
void floyd()
{
    REPF(k,1,n)
    {
        REPF(i,1,n)
        {
            REPF(j,1,n)
              if(mp[i][k]==1&&mp[k][j]==1)  mp[i][j]=1;
        }
    }
}
int main()
{
    int x,y;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        CLEAR(mp,0);
        REP(i,m)
        {
            scanf("%d%d",&x,&y);
            mp[x][y]=1;
        }
        floyd();
        int ans=0;
        CLEAR(link,-1);
        REPF(i,1,n)
        {
            CLEAR(used,0);
            if(dfs(i))  ans++;
        }
        printf("%d\n",n-ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐