POJ 2531 暴力深搜

Network Saboteur

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 13494   Accepted: 6543

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抄自 http://blog.csdn.net/martin31hao/article/details/8098302

题目大意:有n个点,把这些点分别放到两个集合里,在两个集合的每个点之间都会有权值,求可能形成的最大权值。

思路:1、把这两个集合标记为0和1,先默认所有点都在集合0里。

2、依次枚举每个点id,把每个点都放到集合1里去,这个时候就要调整集合的权值了,原来和id都在集合0里的点,要把权值加上;而在集合1里的点,要把权值减去。

3、权值调整完毕后,和ans比较,如果比ans要大, 调整ans。

4、如果放到集合1中,调整节点后的权值比放在集合0中要大,那么就默认这个点在集合1中,继续枚举下面的点进行DFS。最终是可以把最有状态都枚举出来的。

AC代码

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <vector>
11 #define maxn 100005
12 #define maxm 50000
13 using namespace std;
14 typedef long long ll;
15 int g[25],a[25][25];
16 int ans,n;
17 void dfs(int k,int temp)
18 {
19   g[k]=1;
20   int t=temp;
21   for(int i=1;i<=n;i++)
22   {
23     if(g[i]==1)
24       t-=a[k][i];
25     else
26       t+=a[k][i];
27   }
28   if(t>ans)
29     ans=t;
30   for(int i=k+1;i<=n;i++)
31   {
32     if(t>temp)
33     {
34       dfs(i,t);
35       g[i]=0;
36     }
37   }
38 }
39 int main(int argc, char const *argv[])
40 {
41   while(scanf("%d",&n)!=EOF)
42   {
43     memset(g,0,sizeof(g));
44     for(int i=1;i<=n;i++)
45     {
46       for(int j=1;j<=n;j++)
47       {
48         cin>>a[i][j];
49       }
50     }
51     ans=0;
52     dfs(1,0);
53     cout<<ans<<endl;
54   }
55   return 0;
56 }

这题貌似最大割

时间: 2024-12-27 10:17:18

POJ 2531 暴力深搜的相关文章

poj 2531 搜索剪枝

Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description A university network is composed of N computers. System administrators gathered informat

【POJ 2531】Network Saboteur

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

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的话,我不太会做啦.看了队长的用了状态压缩来做,一下子觉得好神奇!!!! 可能第一次接触,理解得不是太深刻,先留着吧.就觉得好神奇,好神奇...

POJ 2531 Network Saboteur(dfs)

题目代号:POJ 2531 题目链接:http://poj.org/problem?id=2531 Language: Default Network Saboteur Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13290   Accepted: 6428 Description A university network is composed of N computers. System administrator

poj 2531 -- Network Saboteur

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

POJ 2251 三维广搜。

B - Dungeon Master Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is com

DFS/POJ 2531 Network Saboteur

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int n,ans; 5 int a[30][30],f[30]; 6 int cmax(int a,int b){return a>b?a:b;} 7 void dfs(int x,int now) 8 { 9 if (x>n) 10 { 11 ans=cmax(ans,now); 12 return; 13 } 14 int temp=0;

蓝桥杯校内选拔赛/POJ 数独(深搜)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14530   Accepted: 7178   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure.

poj 2531 Network Saboteur (dfs)

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