您的位置:首页 > 其它

poj 2594 二分图 Floyd闭包+最小路径覆盖

2014-08-04 16:06 375 查看
题意:机器人可以从一个点沿着路往下走,每个点可以多次访问,求最少需要多少机器人

解法:Floyd+最小路径覆盖

由于点是可以多次访问的,对于传统的路径覆盖,点的访问仅为一次。如果两个未被匹配的点,某个点是它们的中介点,传统的路径覆盖是不允许这两个点匹配的,这个题允许这样匹配,所以首先要用Floyd判断闭包。

最小路径覆盖数=节点数-其对应二分图的最大匹配数

/*
----------------------------------

Love is more than a word.
It says so much.
When I see these four letters,
I almost feel your touch.
This is only happened since
I fell in love with you.
Why this word does this,
I haven't got a clue.

To My Goddess
CY
----------------------------------
*/

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>

#define maxn 500+5

#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define REP(i,a,b) for(i=a;i<=b;i++)
#define rep(i,n) for(i=0;i<n;i++)

#define cle(a) memset(a,0,sizeof(a))
#define clehead(a) rep(i,maxn)a[i]=-1

/*
The time of story :
**  while(1)
{
once upon a time,
there was a mountain,
on top of which there was a temple,
in which there was an old monk and a little monk.
Old monk was telling stories inside the temple.
What was he talking about?
**  }

ÎûÎû
(*^__^*)
*/

#define sci(a) scanf("%d",&a)
#define scd(a) scanf("%lf",&a)
#define pri(a) printf("%d",a)
#define prie(a) printf("%d\n",a)
#define prd(a)  printf("%lf",a)
#define prde(a) printf("%lf\n",a)
#define pre printf("\n")

#define LL(x) x<<1
#define RR(x) x<<|1

#define pb push_back
#define mod 90001
#define PI 3.141592657

const ull INF = 1LL << 61;
const int inf =   int(1e5)+10;
const double eps=1e-5;

using namespace std;

struct node
{
int u,v,w;
int next;
};

bool cmp(int a,int b){
return a>b;
}
int n,m;
bool bmap[maxn][maxn];
bool bmark[10000];
int nx,ny;
int cx[10000];
int cy[10000];
int dir[4][2]={0,-1,-1,0,1,0,0,1};

int findpath(int u)
{
int i,j,k;
rep(i,ny){
if(bmap[u][i]&&!bmark[i]){
bmark[i]=1;
if(cy[i]==-1||findpath(cy[i]))
{
cy[i]=u;
cx[u]=i;
return 1;
}
}
}
return 0;
}

int maxmatch()
{
int i,j,k;
int res(0);
rep(i,nx)cx[i]=-1;
rep(j,ny)cy[j]=-1;
rep(i,nx){
if(cx[i]==-1){
rep(j,ny)bmark[j]=0;
res+=findpath(i);
}
}
return res;
}
void floyd()//求闭包
{
int i,j,k;
rep(i,n)
rep(j,n)
if(!bmap[i][j])
{
rep(k,n){
if(bmap[i][k]&&bmap[k][j])
{
bmap[i][j]=true;
break;
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>n>>m)
{
if(n+m==0)break;
int i,j,k;
nx=ny=n;
cle(bmap);
rep(i,m)
{
int x,y;
cin>>x>>y;
bmap[x-1][y-1]=true;
}
floyd();
cout<<n-maxmatch()<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: