BOJ 6399 奔小康赚大钱 //二分图KM模板题

奔小康赚大钱

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID:  2255
64-bit integer IO format:  %I64d      Java class name:  Main


  Status  Statistics  Discuss  Next
Font Size:  +   -
Type:    None Graph Theory      2-SAT     Articulation/Bridge/Biconnected Component      Cycles/Topological Sorting/Strongly Connected Component      Shortest Path          Bellman Ford         Dijkstra/Floyd Warshall      Euler Trail/Circuit      Heavy-Light Decomposition      Minimum Spanning Tree      Stable Marriage Problem      Trees      Directed Minimum Spanning Tree      Flow/Matching         Graph Matching              Bipartite Matching              Hopcroft–Karp Bipartite Matching              Weighted Bipartite Matching/Hungarian Algorithm          Flow              Max Flow/Min Cut              Min Cost Max Flow  DFS-like     Backtracking with Pruning/Branch and Bound      Basic Recursion      IDA* Search     Parsing/Grammar      Breadth First Search/Depth First Search      Advanced Search Techniques          Binary Search/Bisection          Ternary Search  Geometry      Basic Geometry     Computational Geometry      Convex Hull      Pick's Theorem Game Theory      Green Hackenbush/Colon Principle/Fusion Principle      Nim      Sprague-Grundy Number  Matrix     Gaussian Elimination      Matrix Exponentiation  Data Structures      Basic Data Structures      Binary Indexed Tree      Binary Search Tree      Hashing     Orthogonal Range Search      Range Minimum Query/Lowest Common Ancestor      Segment Tree/Interval Tree      Trie Tree      Sorting     Disjoint Set  String      Aho Corasick     Knuth-Morris-Pratt      Suffix Array/Suffix Tree  Math      Basic Math     Big Integer Arithmetic      Number Theory          Chinese Remainder Theorem          Extended Euclid          Inclusion/Exclusion          Modular Arithmetic      Combinatorics         Group Theory/Burnside's lemma          Counting      Probability/Expected Value  Others     Tricky      Hardest     Unusual      Brute Force      Implementation     Constructive Algorithms      Two Pointer      Bitmask     Beginner      Discrete Logarithm/Shank's Baby-step Giant-step Algorithm      Greedy      Divide and Conquer  Dynamic Programming                      Tag it!
传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子。
这可是一件大事,关系到人民的住房问题啊。村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房子住的话,容易引起不安定因素),每家必须分配到一间房子且只能得到一间房子。
另一方面,村长和另外的村领导希望得到最大的效益,这样村里的机构才会有钱.由于老百姓都比较富裕,他们都能对每一间房子在他们的经济范围内出一定的价格,比如有3间房子,一家老百姓可以对第一间出10万,对第2间出2万,对第3间出20万.(当然是在他们的经济范围内).现在这个问题就是村领导怎样分配房子才能使收入最大.(村民即使有钱购买一间房子但不一定能买到,要看村领导分配的).

Input

输入数据包含多组测试用例,每组数据的第一行输入n,表示房子的数量(也是老百姓家的数量),接下来有n行,每行n个数表示第i个村名对第j间房出的价格(n<=300)。

Output

请对每组数据输出最大的收入值,每组的输出占一行。

Sample Input

2
100 10
15 23

Sample Output

123

Source

HDOJ 2008 Summer Exercise(4)- Buffet Dinner
#include <stdio.h>
#include <string.h>

#define p 305
#define m 999999999

int n, tu[p][p];
int link[p], slack[p];
int lx[p], ly[p];
int visitx[p], visity[p];

int MAX(int a, int b)
{
	if(a>b)
		return a;
	return b;
}
int MIN(int a, int b)
{
	if(a<b)
		return a;
	return b;
}

int dfs(int k)
{
	int i, d;

	visitx[k] = 1;
	for(i=0; i<n; i++)
	{
		if(visity[i])
			continue;
		d = lx[k]+ly[i]-tu[k][i];

		if(d==0)
		{
			visity[i] = 1;
			if(link[i]==-1 || dfs(link[i]))
			{
				link[i] = k;
				return 1;
			}
		}
		else
			slack[i] = MIN(slack[i], d);
	}
	return 0;
}
int KM()
{
	int i, j, ans;

	for(i=0; i<n; i++)
		link[i] = -1;
	memset(lx, 0, sizeof(lx));
	memset(ly, 0, sizeof(ly));
	for(i=0; i<n; i++)
	{
		for(j=0; j<n; j++)
			lx[i] = MAX(lx[i], tu[i][j]);
	}
	for(i=0; i<n; i++)
	{
		for(j=0; j<n; j++)
		{
			slack[j] = m;
		}
		while(1)
		{
			memset(visitx, 0, sizeof(visitx));
			memset(visity, 0, sizeof(visity));
			if(dfs(i))
				break;
			else
			{
				int d;
				d = m;
				for(j=0; j<n; j++)
				{
					if(!visity[j])
						d = MIN(d, slack[j]);
				}
				for(j=0; j<n; j++)
				{
					if(visitx[j])
						lx[j] -= d;
					if(visity[j])
						ly[j] += d;
				}
			}
		}
	}
	ans = 0;
	for(i=0; i<n; i++)
		ans += tu[link[i]][i];
	return ans;
}
int main()
{
	int i, j;
	while(scanf("%d", &n)!=-1)
	{
		for(i=0; i<n; i++)
		{
			for(j=0; j<n; j++)
			{
				scanf("%d", &tu[i][j]);
			}
		}
		printf("%d\n", KM());
	}
	return 0;
}


你可能感兴趣的:(二分图)