POJ1466_Girls and Boys(二分圖/最大獨立集=N-最大匹配),poj1466_girlsn-

來源:互聯網
上載者:User

POJ1466_Girls and Boys(二分圖/最大獨立集=N-最大匹配),poj1466_girlsn-

解題報告

http://blog.csdn.net/juncoder/article/details/38160591

題目傳送門

題意:

求滿足條件的最大集合:集合內任何兩個人都沒有浪漫關係

思路:

跟POJ2771一樣的題,變的簡單多了。POJ2771解題報告

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n,mmap[550][550],pre[550],vis[550];int dfs(int x){    for(int i=0; i<n; i++) {        if(!vis[i]&&mmap[x][i]) {            vis[i]=1;            if(pre[i]==-1||dfs(pre[i])) {                pre[i]=x;                return 1;            }        }    }    return 0;}int main(){    int i,j,a,b,k;    while(~scanf("%d",&n)) {        memset(mmap,0,sizeof(mmap));        memset(pre,-1,sizeof(pre));        for(i=1; i<=n; i++) {            scanf("%d: (%d) ",&a,&k);            for(j=1; j<=k; j++) {                scanf("%d",&b);                mmap[a][b]=1;            }        }        int ans=0;        for(i=0; i<n; i++) {            memset(vis,0,sizeof(vis));            ans+=dfs(i);        }        printf("%d\n",n-ans/2);    }    return 0;}

Girls and Boys
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 10348   Accepted: 4608

Description

In the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been "romantically involved". The result of the program is the number of students in such a set.

Input

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description: 

the number of students 
the description of each student, in the following format 
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ... 
or 
student_identifier:(0) 

The student_identifier is an integer number between 0 and n-1 (n <=500 ), for n subjects.

Output

For each given data set, the program should write to standard output a line containing the result.

Sample Input

70: (3) 4 5 61: (2) 4 62: (0)3: (0)4: (2) 0 15: (1) 06: (2) 0 130: (2) 1 21: (1) 02: (1) 0

Sample Output

52



幫我解釋下網路流

必須知識:最短路徑問題
1.Dijkstra
適用於滿足所有權係數大於等於0(lij≥0)的網路最短路問題,能求出起點v1到所有其他點vj的最短距離;
樸素的Dijkstra演算法複雜度為O(N^2),堆實現的Dijkstra複雜度為O(NlogN).

2.bellman-ford
適用於有負權係數,但無負迴路的有向或無向網路的最短路問題,能求出起點v1到所有其它點 vj的最短距離。bellman-ford演算法複雜度為O(V*E)。

3.Floyed
適用於有負權係數,可以求出圖上任意兩點之間的最短路徑。DP思想的演算法,時間複雜度為O(N^3);
for ( k= 1; k<= n; k++)
for ( i= 1; i<= n; i++)
if (graph[i][k]!=INF)
for ( j= 1; j<= n; j++)
if (graph[k][j]!=INF && graph[i][k]+graph[k][j]< graph[i][j])
graph[i][j]= graph[i][k]+ graph[k][j];

NO.1 s-t最大流
兩大類演算法
1.增廣路演算法
Ford-Fulkerson演算法: 殘留網路中尋找增加路徑
STEP0:置初始可行流。
STEP1:構造原網路的殘量網路,在殘量網路中找s-t有向路。如果沒有,演算法得到最大流結束。否則繼續下一步。
STEP2:依據殘量網路中的s-t有向路寫出對應到原網路中的s-t增廣路。對於增廣路中的前向弧,置s(e)=u(e)- f(e)。對於反向弧,置s(e)=f(e) STEP3:計算crement=min{s(e1),s(e2),…,s(ek)}
STEP4:對於增廣路中的前向弧,令f(e)=f(e)+crement;對於其中的反向弧,令f(e)=f(e)-crement,轉STEP1。
關鍵點:尋找可增廣路。決定了演算法複雜度。
實現:Edmonds-Karp 通過採用了廣度優先的搜尋策略得以使其複雜度達到O(V*E^2)。

最佳化—> Dinic演算法(*)
Dinic演算法的思想是為了減少增廣次數,建立一個輔助網路L,L與原網路G具有相同的節點數,但邊上的容量有所不同,在L上進行增廣,將增廣後的流值回寫到原網路上,再建立當前網路的輔助網路,如此反覆,達到最大流。分層的目的是降低尋找增廣路的代價。
演算法的時間複雜度為O(V^2*E)。其中m為弧的數目,是多項式演算法。鄰接表表示圖,空間複雜度為O(V+E)。

2.預流推進演算法
一般性的push-relabel演算法: 時間複雜度達到O(V^2*E)。(*)
relabel-to-front演算法->改進
最高標號預流推進:時間複雜度O(V^2*sqrt(E))

NO2. 最小費用最大流
求解最小費用流的步驟和求最大流的步驟幾乎完全一致,只是在步驟1時選一條非飽和路時,應選代價和最小的路,即最短路。
步驟1. 選定一條總的單位費用最小的路,即要給定最小費用的初始可行流,而不是包含邊數最小的路。
步驟2. 不斷重複求最大流的步驟來進行,直到沒有飽和路存在為止。然後計算每......餘下全文>>
 
POJ 1466 Girls and Boys 不好意思,沒分了 我什地方錯了???

你沒錯,出題人SB,資料出的不完整!
 

聯繫我們

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