POJ 2536 Gopher II(二分圖的最大匹配),poj2536

來源:互聯網
上載者:User

POJ 2536 Gopher II(二分圖的最大匹配),poj2536

題目連結:http://poj.org/problem?id=2536


題意:已知有n只老鼠的座標,m個洞的座標,老鼠的移動速度為V,S秒以後有一隻老鷹要吃老鼠,問有多少個老鼠被吃。

很明晰,二分匹配,老鼠為X集合,洞為Y集合


思路:計算當前老鼠 Xi 到達洞 Yi 的時間(dis/v),如果小於S的話,則Xi與Yi聯通,


被吃的老鼠數 = n - 最大匹配數


#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <math.h>#define init(a) memset(a,0,sizeof(a))#define PI acos(-1,0)using namespace std;const int maxn = 310;const int maxm = 100001;#define lson left, m, id<<1#define rson m+1, right, id<<1|1#define min(a,b) (a>b)?b:a#define max(a,b) (a>b)?a:bint n,m,s,v,ma[500][500];bool vis[500];int line[500];struct node{  double x,y;};node g[300],h[300];int DFS(int u){  for(int v = 1;v<=m;v++)    {    if(!vis[v]&&ma[u][v])    {      vis[v]=1;      if(line[v]==-1 || DFS(line[v]))    {        line[v] = u;        return 1;      }    }  }  return 0;}int K_M(){    memset(line,-1,sizeof(line));    int ans=0;    for(int i = 1;i<=n;i++)    {      init(vis);      ans += DFS(i);    }    return ans;}int main(){  while(scanf("%d%d%d%d",&n,&m,&s,&v)!=EOF)    {    init(ma);    for(int i=1;i<=n;i++)    {      scanf("%lf%lf",&g[i].x,&g[i].y);    }    for(int i=1;i<=m;i++)    {      scanf("%lf%lf",&h[i].x,&h[i].y);    }    for(int i=1;i<=n;i++)    {      for(int j=1;j<=m;j++)        {  double dis = sqrt((h[j].x-g[i].x)*(h[j].x-g[i].x)+(h[j].y-g[i].y)*(h[j].y-g[i].y));//老鼠與洞的距離        if(dis / v <= (double)s)//老鼠到達洞的時間<S            {                ma[i][j] = 1;            }        }    }    int ans = K_M();    printf("%d\n",n - ans);  }  return 0;}



二分圖最大匹配的Matlab程式

[num h] = maxnum(g);%g是二分圖鄰接矩陣%調用了一個自己寫maxnum函數,返回num就是最大值,h是hij(不唯一)以下是maxnum.m的內容,用的是匈牙利演算法其中還用了一個遞迴的incpath函數,尋找增廣路徑function [num h] = maxnum(g)s=size(g);global G_h;%矩陣hij記錄選中global G_g;%矩陣gij記錄匹配global G_v;%記錄當前一次路徑訪問過的節點G_h=false(s);%矩陣hij初始為空白G_g=g;%矩陣gij就是傳遞進來的參數gfor i=1:s(1) G_v=false(1,s(2));%每次初始化徑訪問過的節點為空白 incpath(i);%從Ai開始尋找增廣路徑endh=G_h;num=sum(h(:));%輸出最大匹配數,和匹配矩陣hclear global 'G_h';clear global 'G_g';endfunction OK = incpath(i)%從Ai開始global G_h;global G_g;global G_v;OK=false;j=find(~G_h(i,:)&G_g(i,:)&~G_v,1);%尋找合條件的Bjif isempty(j),return;end%找不到返回falseG_v(j)=true;%找到了,標記Bj為以訪問節點ii=find(G_h(:,j));%尋找Bj在原來匹配中if isempty(ii)%如果不在原匹配中G_h(i,j)=true;OK=true;return;end%找到增廣路徑末端,返回trueok=incpath(ii);%如果在原來的匹配中,根據匹配對應的Aii遞迴調用incpath尋找if ok %如果遞迴尋找返回成功G_h(i,j)=~G_h(i,j);G_h(ii,j)=~G_h(ii,j);OK=true;return;end%路徑反色返回trueend
 
二分圖最大匹配Matlab程式(,感激不盡)

第一個問題比較簡單,這裡懶得對著你的圖片敲資料,用隨機數代替
long=100;
A=0.1*rand(long,1);
B=0.15*rand(long,1);
[AA BB]=meshgrid(A,B);
gg=0.5017*AA-0.65*BB;
g=gg>=-0.03&gg<=0.03; %這就是gij矩陣,相當於二分圖的串連矩陣
[num h] = maxnum(g);%第二個問題就是求二分圖的最大匹配問題,這裡
%調用了一個自己寫maxnum函數,返回num就是最大值,h是hij(不唯一)

以下是maxnum.m的內容,用的是匈牙利演算法
其中還用了一個遞迴的incpath函數,尋找增廣路徑
function [num h] = maxnum(g)s=size(g);global G_h;%矩陣hij記錄選中global G_g;%矩陣gij記錄匹配global G_v;%記錄當前一次路徑訪問過的節點G_h=false(s);%矩陣hij初始為空白G_g=g;%矩陣gij就是傳遞進來的參數gfor i=1:s(1) G_v=false(1,s(2));%每次初始化徑訪問過的節點為空白 incpath(i);%從Ai開始尋找增廣路徑endh=G_h;num=sum(h(:));%輸出最大匹配數,和匹配矩陣hclear global 'G_h';clear global 'G_g';endfunction OK = incpath(i)%從Ai開始global G_h;global G_g;global G_v;OK=false;j=find(~G_h(i,:)&G_g(i,:)&~G_v,1);%尋找合條件的Bjif isempty(j),return;end%找不到返回falseG_v(j)=true;%找到了,標記Bj為以訪問節點ii=find(G_h(:,j));%尋找Bj在原來匹配中if isempty(ii)%如果不在原匹配中G_h(i,j)=true;OK=true;return;end%找到增廣路徑末端,返回trueok=incpath(ii);%如果在原來的匹配中,根據匹配對應的Aii遞迴調用incpath尋找if ok %如果遞迴尋找返回成功G_h(i,j)=~G_h(i,j);G_h(ii,j)=~G_h(ii,j);OK=true;return;end%路徑反色返回trueend
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.