HDU 1151 Air Aaid 最小路徑覆蓋

來源:互聯網
上載者:User

題目大意:

在一個城鎮,有m個路口,和n條路,這些路都是單向的,而且路不會形成環,現在要弄一些傘兵去巡查這個城鎮,

傘兵只能沿著路的方向走,問最少需要多少傘兵才能把所有的路口搜一遍。這個題目就轉換成求解有向非循環圖的最

小路徑覆蓋問題了。

一個結論:有向非循環圖的最小路徑覆蓋=該圖的頂點數-該圖的最大匹配。

 

AC代碼:

#include<iostream>using namespace std;const int MAX=121;int no_of_intersections,no_of_streets,t;int link[MAX];    //記錄當前與street終點相連的起點點bool map[MAX][MAX];  //儲存二分圖,起點到終點有路徑,則map[x][y]=true;反之,為falsebool useif[MAX];   //記錄路口點是否使用了)void getMap()  //存圖{memset(map,false,sizeof(map));int a,b;for(int i=1;i<=no_of_streets;i++){cin>>a>>b;map[a][b]=true;}}bool can(int t){for(int i=1;i<=no_of_intersections;i++){if(!useif[i] && map[t][i]){useif[i]=true;if(link[i]==-1 || can(link[i])){link[i]=t;return true;}}}return false;}int maxMatch(){int num=0;memset(link,int(-1),sizeof(link));  //未連的計為-1for(int i=1;i<=no_of_intersections;i++)   //從street起點開始依次搜尋增廣路經{memset(useif,false,sizeof(useif));if(can(i)) num++;}return num;}int main(){cin>>t;while(t--){cin>>no_of_intersections>>no_of_streets;getMap();cout<<no_of_intersections-maxMatch()<<endl;}return 0;}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.