http://poj.org/problem?id=2531
题目大意:
把一个完全图分成两部分,使得连接这两部分边的权和最大。
第一次用随机化,觉得蛮有趣的~
但是应该随机化多少次心里没有谱- -
从题目来看:20个数分成两组,总共会有c(1,20)+c(2,20)+....+c(10,20)种不同的 大约是2e5
而我随机2e5次 ac了。。。总感觉有一定的运气成分- -
时间是297MS 。。。循环2e6次应该没希望了吧
果断超时- -
从限制时间来推,Time Limit: 2000MS,
大约是4e8次循环,循环次数N*20<=4e8 N大约小于2e6
所以稳妥的方案就是用1e6次循环
用随机化还是从题目限制时间往回推比较稳妥- -
#include <cstdio>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include <math.h>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=25;
int w[maxn][maxn];
int main()
{
//freopen("input.txt","r",stdin);
int n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>w[i][j],w[j][i]=w[i][j];
bool subset[maxn]={false};
int randtime=2e5;
int ans=-INF;
int sum=0;
while(randtime--)
{
int x=rand()%n+1;
subset[x]=!subset[x];
for(int i=1;i<=n;i++){
if(subset[i]!=subset[x])
sum+=w[i][x];
else sum-=w[i][x];
}
ans=max(ans,sum);
}
cout<<ans<<endl;
return 0;
}