程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 1150 Machine Schedule (最小點覆蓋)

hdu 1150 Machine Schedule (最小點覆蓋)

編輯:C++入門知識

hdu 1150 Machine Schedule (最小點覆蓋)

結論:二分圖的最小點覆蓋數=最大匹配數

import java.io.*;
import java.util.*;
import java.math.*;

class Edge {
	int t , next ;
}

class solution {
	static Scanner in = new Scanner ( System.in ) ;
	static PrintWriter out = new PrintWriter ( System.out ) ;
	int n , m ;
	int[] c = new int[1111] ;
	Edge[] edge = new Edge[1111] ;
	int tot ;
	int[] head = new int[1111] ;
	int[] vis = new int[1111] ;
	void init () {
		tot = 0 ;
		for ( int i = 0 ; i < 1111 ; i ++ ) c[i] = head[i] = -1 ;
	}
	void new_edge ( int a , int b ) {
		edge[tot] = new Edge () ;
		edge[tot].t = b ;
		edge[tot].next = head[a] ;
		head[a] = tot ;
		tot ++ ;
	}
	int dfs ( int u ) {
		int i ;
		for ( i = head[u] ; i != -1 ; i = edge[i].next ) {
			int e = edge[i].t ;
			if ( vis[e] == 1 ) continue ;
			vis[e] = 1 ;
			if ( c[e] == -1 || dfs ( c[e] ) == 1 ) {
				c[e] = u ;
				return 1 ;
			}
		}
		return 0 ;
	}
	void solve () {
		int i , k , j ;
		while ( in.hasNext () ) {
			init () ;
			n = in.nextInt() ;
			if ( n == 0 ) break ;
			m = in.nextInt() ; k = in.nextInt() ;
			for ( i = 1 ; i <= k ; i ++ ) {
				int a , b , c ;
				a = in.nextInt() ; b = in.nextInt() ; c = in.nextInt() ;
				new_edge ( b ,c ) ;
			}
			int ans = 0 ;
			for ( i = 1 ; i <= n ; i ++ ) {
				for ( j = 1 ; j <= m ; j ++ ) vis[j] = 0 ;
				ans += dfs ( i ) ;
			}
			out.println ( ans ) ;
		}
		out.close () ;
	}
}

public class Main {
	static public void main ( String[] args ) {
		solution fuck = new solution () ;
		fuck.solve () ;
	}
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved