POJ2531——Network Saboteur(随机化算法水一发)
Network Saboteur
Description
A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divided the network into two subnetworks in order to minimize traffic between parts.
A disgruntled computer science student Vasya, after being expelled from the university, decided to have his revenge. He hacked into the university network and decided to reassign computers to maximize the traffic between two subnetworks.
Unfortunately, he found that calculating such worst subdivision is one of those problems he, being a student, failed to solve. So he asks you, a more successful CS student, to help him.
The traffic data are given in the form of matrix C, where Cij is the amount of data sent between ith and jth nodes (Cij = Cji, Cii = 0). The goal is to divide the network nodes into the two disjointed subsets A and B so as to maximize the sum ∑Cij (i∈A,j∈B).
Input
The first line of input contains a number of nodes N (2 <= N <= 20). The following N lines, containing N space-separated integers each, represent the traffic matrix C (0 <= Cij <= 10000).
Output file must contain a single integer -- the maximum traffic between the subnetworks.
Output
Output must contain a single integer -- the maximum traffic between the subnetworks.
Sample Input
3
0 50 30
50 0 40
30 40 0
Sample Output
90
题目大意:
把一个图用一条直线隔成两部分,求被这条直线相交的边的权值和的最大值。
解题思路:
图论的无向完全图的最大割问题。
先建立两个集合,一个集合包含全部的点,另一个为空集。
此时的权值为0。
随机选取一个点,将它从所在的集合挪到另外一个集合中。
eg:
当前状态的权值和为sum,将3号点,从集合1中挪到集合2中。
则权值和的变化为
sum+=edge[3][i],i表示在集合1中的点。
sum-=edge[3][j],j表示在集合2中的点,不包括3号点。
每进行一次变化,就会有新的sum产生,保存sum的最大值即可。
(随机选择100W次后过得,随机次数越多,越可能产生标准答案,但要注意时间)
Code:
/*************************************************************************
> File Name: poj2531.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年10月27日 星期一 19时08分08秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<time.h>
#include<climits>
#define MAXN 100000
using namespace std;
void init()
{
srand(time(NULL));
}
int edge[][];
int main()
{
init();
int N;
while (cin>>N)
{
memset(edge,,sizeof(edge));
for (int i=;i<=N;i++)
for (int j=;j<=N;j++)
cin>>edge[i][j];
int times=;
bool gather[];
int ret=,max=;
memset(gather,,sizeof(gather));
while (times--)
{
int tmp=rand()%N+;
gather[tmp]=!gather[tmp];
for (int i=;i<=N;i++)
{
if (gather[i]!=gather[tmp])
ret+=edge[i][tmp];
if (gather[i]==gather[tmp]&&i!=tmp)
ret-=edge[i][tmp];
}
max=max>ret?max:ret;
}
cout<<max<<endl;
}
return ;
}
POJ2531——Network Saboteur(随机化算法水一发)的更多相关文章
- poj2531 Network Saboteur
Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11122 Accepted: 5372 ...
- PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)
题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合 ...
- Network Saboteur(搜索)
Network Saboteur POJ2531 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10351 Accept ...
- POJ 矩阵相乘 (随机化算法-舍伍德(Sherwood))
周三的算法课,主要讲了随机化算法,介绍了拉斯维加斯算法,简单的理解了为什么要用随机化算法,随机化算法有什么好处. 在处理8皇后问题的时候,穷举法是最费时的,回朔比穷举好点,而当数据量比较大的时候,如1 ...
- Network Saboteur 分类: 搜索 POJ 2015-08-09 19:48 7人阅读 评论(0) 收藏
Network Saboteur Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10147 Accepted: 4849 Des ...
- POJ3318--Matrix Multiplication 随机化算法
Description You are given three n × n matrices A, B and C. Does the equation A × B = C hold true? In ...
- CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)
C - Network Saboteur Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 2018.09.14 codeforces364D(随机化算法)
传送门 根据国家集训队2014论文集中胡泽聪的随机化算法可以通过这道题. 对于每个数,它有12" role="presentation" style="posi ...
- PKU 3318 Matrix Multiplication(随机化算法||状态压缩)
题目大意:原题链接 给定三个n*n的矩阵A,B,C,验证A*B=C是否成立. 所有解法中因为只测试一组数据,因此没有使用memset清零 Hint中给的傻乎乎的TLE版本: #include<c ...
随机推荐
- PERL 脚本
PERL: Practical Extraction and Report Language 参考文档 1.Perl 5 version 24.0 documentation
- Windows Phone 8.1 列表控件(2):分组数据
说到 List 控件,Windows Phone 8.1 上推荐使用的是 ListView 和 GridView. 而这两个控件实在太多东西可讲了,于是分成三篇来讲: (1)基本 (2)分组数据 (3 ...
- java 读写锁
http://tutorials.jenkov.com/java-concurrency/read-write-locks.html 翻译 读写锁比LOCK的实现更复杂,想象有一个应用程序能读和写一些 ...
- 当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“C:\WINDOWS2\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files”的写访问权限。
卸载了域控制器之后,IIS出现“当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“C:\WINDOWS2\Microsoft.NET\Framework\v4.0.30319 ...
- [SQL Server]树形结构的创建
对于SQL Server来说,构建显示一个树形结构不是一件容易的事情,逻辑构造能力不是它的强项.不过也不是说它没有能力干这个事情,只要换一种思维方式就可以理解它的工作原理. 例如,现在有一张表的内容如 ...
- laravel--模型中各种属性详解
首先以这个模型为例 首先看, 1.$guarded属性,$fillable属性 $guarded属性一般是和$fillable对应的,不是一起存在但是互相使用,他们都是laravel的批量赋值方法cr ...
- jQuery实现页内查找相关内容
当需要在页面中查找某个关键字时,一是可以通过浏览器的查找功能实现,二是可以通过前端脚本准确查找定位,本文介绍通过jQuery实现的页面内容查找定位的功能,并可扩展显示查找后的相关信息. 本文以查找车站 ...
- git命令行
cmd下运行或者 进入git bash运行 输入 exit退出切换到仓库目录后再git statusgit commit -m 注释 git pull origin1 mastergit push o ...
- Beaglebone Back学习六(Can总线测试)
Can总线测试 1 Can总线 控制器局域网 (Controller Area Network, 简称 CAN 或 CANbus)是一种通信协议,其特点是允许网络上的设备直接互相通信,网络上不需要主机 ...
- WPF中禁止WebBrowser控件打开新窗口
一.针对纯WPF的WebBrowser控件: <summary> Suppress Script Errors In WPF WebBrowser </summary> pub ...