[POJ2531 Network Saboteur]

[题目来源]:Northeastern Europe 2002, Far-Eastern Subregion

[关键字]:搜索

[题目大意]:给出每两台电脑之间的流量,将所有电脑分为两组只有不同组之间才有流量求出怎样分能达到最大流量。

//=====================================================================================================

[分析]:对于每一台电脑都有两种选择:归入第一组还是第二组。所以只要枚举每一种可能的状态,找出最优值。

[代码]:

View Code
 1 program Project1;
2 var
3 n, ans, now: longint;
4 c: array[0..20,0..20] of longint;
5 f: array[0..1,0..20] of longint;
6
7 procedure init;
8 var
9 i, j: longint;
10 begin
11 readln(n);
12 for i := 1 to n do
13 begin
14 for j := 1 to n do read(c[i,j]);
15 readln;
16 end;
17 end;
18
19 function done(k: longint):longint;
20 var
21 t, sum, x, i, j: longint;
22 begin
23 t := 1;
24 fillchar(f,sizeof(f),0);
25 while t <= n do
26 begin
27 x := k and 1;
28 inc(f[x,0]);
29 f[x,f[x,0]] := t;
30 k := k shr 1;
31 inc(t);
32 end;
33 sum := 0;
34 for i := 1 to f[1,0] do
35 for j := 1 to f[0,0] do
36 inc(sum,c[f[1,i],f[0,j]]);
37 exit(sum);
38 end;
39
40 procedure dfs(k: longint);
41 var
42 temp: longint;
43 begin
44 if k > n then
45 begin
46 temp := done(now);
47 if temp > ans then ans := temp;
48 exit;
49 end;
50 dfs(k+1);
51 temp := 1 shl (k-1);
52 now := now xor temp;
53 dfs(k+1);
54 now := now xor temp;
55 end;
56
57 begin
58 init;
59 now := 0;
60 ans := 0;
61 dfs(1);
62 writeln(ans);
63 end.



你可能感兴趣的:(NetWork)