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:

 1 /*************************************************************************
 2     > File Name: poj2531.cpp
 3     > Author: Enumz
 4     > Mail: [email protected]
 5     > Created Time: 2014年10月27日 星期一 19时08分08秒
 6  ************************************************************************/
 7
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<string>
12 #include<cstring>
13 #include<list>
14 #include<queue>
15 #include<stack>
16 #include<map>
17 #include<set>
18 #include<algorithm>
19 #include<cmath>
20 #include<bitset>
21 #include<time.h>
22 #include<climits>
23 #define MAXN 100000
24 using namespace std;
25 void init()
26 {
27     srand(time(NULL));
28 }
29 int edge[50][50];
30 int main()
31 {
32     init();
33     int N;
34     while (cin>>N)
35     {
36         memset(edge,0,sizeof(edge));
37         for (int i=1;i<=N;i++)
38             for (int j=1;j<=N;j++)
39                 cin>>edge[i][j];
40         int times=1000000;
41         bool gather[50];
42         int ret=0,max=0;
43         memset(gather,0,sizeof(gather));
44         while (times--)
45         {
46             int tmp=rand()%N+1;
47             gather[tmp]=!gather[tmp];
48             for (int i=1;i<=N;i++)
49             {
50                 if (gather[i]!=gather[tmp])
51                     ret+=edge[i][tmp];
52                 if (gather[i]==gather[tmp]&&i!=tmp)
53                     ret-=edge[i][tmp];
54             }
55             max=max>ret?max:ret;
56         }
57         cout<<max<<endl;
58     }
59     return 0;
60 }

    

    

时间: 2024-07-29 20:48:09

POJ2531——Network Saboteur(随机化算法水一发)的相关文章

POJ2531:Network Saboteur

Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10978   Accepted: 5290 Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully d

PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)

题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合:true  B集合:false 解法一:dfs+剪枝 #include<iostream> #include<cstring> using namespace std; int n,ans; bool in[25]; int graph[25][25]; void dfs(int i

poj2531(Network Saboteur)

题目地址:Network Saboteur 题目大意: 一个学校的网络由N台计算机组成,为了减少网络节点之间的流量,学校将N个节点分解成两个集合(A.B),student Vasya打算入侵学校的网络,致使集合之间的流量最大,集合之间的流量计算是A集合中的所有节点到B集合中的所有节点的流量之和. 例如 1.2.3 分解集合,A可以为{1}这时B为{2.3}.然后计算 1-2的流量加上 1-3的流量为sum  求出所有可能的sum  取最大的那个. 解题思路: 题意可能有点难理解.  数据很水,直

【POJ 2531】Network Saboteur

[POJ 2531]Network Saboteur 图的搜索 剪枝真是门学问..剪好了快的可真不是一倍两倍 刚开始搜的思路有问题 TLE了 后来枚举点暴力搜了一发 两百多ms 由于查找时权值是不断增加的 所以直接找集合间最大权的话不方便设置return点 看disscuss发现有一大牛 建了两个数组 通过所有边权-两集合内部边权(去重) 得到答案 dfs的时候找最小内部边权即可 当前状态权值>当前最小内部边权时直接跳出 两个数组分别寸当前状态两个集合中所含的点 每加一个点分别往两边加 假设要将

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

HDU 4006 The kth great number (基本算法-水题)

The kth great number Problem Description Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too mu

HDU 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there

poj 2531 Network Saboteur 解题报告

题目链接:http://poj.org/problem?id=2531 题目意思:将 n 个点分成两个部分A和B(也就是两个子集啦), 使得子集和最大(一定很难理解吧,呵呵).举个例子吧,对于样例,最佳的分法就是把点2分为一个子集,另一个子集理所当然就是1.3了. 2-1 的权值是50,2-3的权值是40,那么最大就是50+40 = 90了. 首先dfs的话,我不太会做啦.看了队长的用了状态压缩来做,一下子觉得好神奇!!!! 可能第一次接触,理解得不是太深刻,先留着吧.就觉得好神奇,好神奇...

CSU-ACM2016暑假集训训练2-DFS(C - Network Saboteur)

C - Network Saboteur Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description A university network is composed of N computers. System administrators gathered information on the traffic between nodes, and carefully divide