关于KM板子的一个小细节

一个一般不怎么会遇到的小细节。
也是在被下面这道题坑了才发现的。

My Brute

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1932 Accepted Submission(s): 714

Problem Description
Seaco is a beautiful girl and likes play a game called “My Brute”. Before Valentine’s Day, starvae and xingxing ask seaco if she wants to spend the Valentine’s Day with them, but seaco only can spend it with one of them. It’s hard to choose from the two excellent boys. So there will be a competition between starvae and xingxing. The competition is like the game “My Brute”.

Now starvae have n brutes named from S1 to Sn and xingxing’s brutes are named from X1 to Xn. A competition consists of n games. At the beginning, starvae’s brute Si must versus xingxing’s brute Xi. But it’s hard for starvae to win the competition, so starvae can change his brutes’ order to win more games. For the starvae’s brute Si, if it wins the game, starvae can get Vi scores, but if it loses the game, starvae will lose Vi scores. Before the competition, starvae’s score is 0. Each brute can only play one game. After n games, if starvae’s score is larger than 0, we say starvae win the competition, otherwise starvae lose it.

It’s your time to help starvae change the brutes’ order to make starvae’s final score be the largest. If there are multiple orders, you should choose the one whose order changes the least from the original one. The original order is S1, S2, S3 … Sn-1, Sn, while the final order is up to you.

For starvae’s brute Si (maybe this brute is not the original brute Si, it is the ith brute after you ordered them) and xingxing’s brute Xi, at first Si has Hi HP and Xi has Pi HP, Si’s damage is Ai and Xi’s is Bi, in other words, if Si attacks, Xi will lose Ai HP and if Xi attacks, Si will lose Bi HP, Si attacks first, then it’s Xi’s turn, then Si… until one of them’s HP is less than 0 or equal to 0, that, it lose the game, and the other win the game.

Come on, starvae’s happiness is in your hand!

Input
First line is a number n. (1<=n<=90) Then follows a line with n numbers mean V1 to Vn. (0<Vi<1000) Then follows a line with n numbers mean H1 to Hn. (1<=Hi<=100)Then follows a line with n numbers mean P1 to Pn. (1<=Pi<=100) Then follows a line with n numbers mean A1 to An.(1<=Ai<=50) Then follows a line with n numbers mean B1 to Bn. (1<=Bi<=50) A zero signals the end of input and this test case is not to be processed.

Output
For each test case, if starvae can win the competition, print the largest score starvae can get, and then follow a real percentage means the similarity between the original order and the final order you had changed, round it to three digits after the decimal point. If starvae can’t win the competition after changing the order, please just print “Oh, I lose my dear seaco!” Maybe the sample can help you get it.

Sample Input
3
4 5 6
6 8 10
12 14 16
7 7 6
7 3 5
3
4 5 6
6 8 10
12 14 16
5 5 5
5 5 5
0

Sample Output
7 33.333%
Oh, I lose my dear seaco!

Author
starvae

Source
HDU “Valentines Day” Open Programming Contest 2010-02-14

Recommend
wxl | We have carefully selected several similar problems for you: 1853 3395 2448 1565 3376

网上有好几种KM算法的板子。每种板子都有一点点不同。
先来一份AC代码

#include<bits/stdc++.h>
using namespace std;
#define mem(a) memset(a,0,sizeof(a))

const int inf = 0x3f3f3f3f;

int n,a[100],h[100],p[100],v[100],b[100];
int g[100][100];

int cx[100],cy[100],ex[100],ey[100],d;
int vx[100],vy[100];
double rate; 

bool dfs(int u)
{
	vx[u] = 1; 
	for (int i = 1;i<=n;i++)
	{
		if (vy[i]) continue;
		int num = ex[u] + ey[i] - g[u][i];
		if (num == 0) 
		{
			vy[i] = 1;
			if (cy[i] == 0 || dfs(cy[i]))
			{
				cx[u] = i;
				cy[i] = u;
				return 1;
			}
		}
	}
	return 0;
}

int KM()
{
	mem(cx); mem(cy); mem(ey);
	for (int i = 1;i<=n;i++)
	{ 
		ex[i] = -2147483647;
		for (int j = 1;j<=n;j++)
		ex[i] = max(ex[i],g[i][j]);
	} 
	for (int i = 1;i<=n;i++)
	{
		while (1)
		{
			mem(vx); mem(vy);
			if (dfs(i)) break;
			d = 2147483647;
			for(int j = 1;j <= n;j++)
			{	
				if (vx[j])
				{
					for (int k = 1;k<=n;k++)
					{
						if (!vy[k]) d = min(d,ex[j] + ey[k] - g[j][k]);
					}
				} 
			}
			for(int j = 1;j <= n;j++)
			{
				if(vx[j]) ex[j] -= d;
				if(vy[j]) ey[j] += d;
			} 
		}
	}
	int res = 0;
	rate = 0;
	for (int i = 1;i<=n;i++)
	{
		res += g[cy[i]][i];
		if (cy[i] == i) rate++;
	}
	return res;
}

int main() 
{
	while(scanf("%d",&n) && (n != 0))
	{
		for (int i = 1;i<=n;i++) scanf("%d",&v[i]);
		for (int i = 1;i<=n;i++) scanf("%d",&h[i]);
		for (int i = 1;i<=n;i++) scanf("%d",&p[i]);
		for (int i = 1;i<=n;i++) scanf("%d",&a[i]);
		for (int i = 1;i<=n;i++) scanf("%d",&b[i]);
		for (int i = 1;i<=n;i++)
		{
			for (int j = 1;j<=n;j++)
			{
				if ((h[i] / b[j] + (h[i] % b[j] > 0)) >= (p[j] / a[i] + (p[j] % a[i] > 0))) g[i][j] = v[i] * 100;
				else g[i][j] = -v[i] * 100;
			}
			g[i][i]++;
		}
		int ans = KM() / 100; 
		if (ans <= 0) printf("Oh, I lose my dear seaco!\n");
		else
		{
			rate = (rate / (double)n) * 100;
			printf("%d %.3f%%\n",ans,rate);
		} 
    }
}

有多种版本的找最小的 两边顶标和-边权 的值的方法
我一开始打的板子是这样的

bool dfs(int u)
{
	vx[u] = 1; 
	for (int i = 1;i<=n;i++)
	{
		if (vy[i]) continue;
		int num = ex[u] + ey[i] - g[u][i];
		if (num == 0) 
		{
			vy[i] = 1;
			if (cy[i] == 0 || dfs(cy[i]))
			{
				cx[u] = i;
				cy[i] = u;
				return 1;
			}
		}
		else d = min(d,num);//改变在这里
	}
	return 0;
}

int KM()
{
	mem(cx); mem(cy); mem(ey);
	for (int i = 1;i<=n;i++)
	{ 
		ex[i] = -2147483647;
		for (int j = 1;j<=n;j++)
		ex[i] = max(ex[i],g[i][j]);
	} 
	for (int i = 1;i<=n;i++)
	{
		while (1)
		{
			mem(vx); mem(vy);
			d = 2147483647;
			if (dfs(i)) break;
			for(int j = 1;j <= n;j++)//还有这里
			{
				if(vx[j]) ex[j] -= d;
				if(vy[j]) ey[j] += d;
			} 
		}
	}
	int res = 0;
	rate = 0;
	for (int i = 1;i<=n;i++)
	{
		res += g[cy[i]][i];
		if (cy[i] == i) rate++;
	}
	return res;
}

上面这个板子在跑dfs的时候,用d记录下了最小的 两边顶标和-边权 的值。
看起来好像没什么问题,
然而我第一份代码46ms跑完了,第二份代码TLE1000ms,调了我半个下午 + 一个晚上终于调出来了。
而且仔细一想第二份代码也没有错。
两者的区别就在于,一个是在 d f s dfs dfs的时候把 d d d找了出来,一个是跑完 d f s dfs dfs之后,从已经访问了的左边的点和未访问的右边的点的组合中,找出最小的 d d d
在dfs里找会出现一种情况就是 当前 v y [ i ] = 0 vy[i] = 0 vy[i]=0 && n u m &gt; 0 num &gt; 0 num>0,然后d把这个值保存下来了,假设它的值是1。然后往下 d f s dfs dfs的时候,又跑到 i i i这个点,这个时候 v y [ i ] = = 1 vy[i] == 1 vy[i]==1 && n u m = = 0 num == 0 num==0。虽然 这个时候 n u m = = 0 num == 0 num==0这个值并不会保存到 d d d当中,但是 d d d里面保存的原来的值1还在。如果是在 d f s dfs dfs完之后再寻找 d d d的话,因为 v y [ i ] = = 1 vy[i] == 1 vy[i]==1 d d d的值可能就不会是1了。也就是说在 d f s dfs dfs里找最小值,可能会出现找到的 d d d是已经访问了的左边的点和已经访问的右边的点的顶标和减去边权这种情况。所以时间复杂度会上升,所以一直TLE。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值