POJ2531

题意:已知一个邻接矩阵,求如何将矩阵分为两部分 使得两部分的权值之和最大。

算法:DFS 穷举法

 

 

import java.util.Scanner; public class poj2531 { static int a[][]; static int b[]; static int n; static int num = 0; public static void main(String args[]) { Scanner in = new Scanner(System.in); n = in.nextInt(); a = new int[n + 1][n + 1]; b = new int[n + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { a[i][j] = in.nextInt(); } } dfs(0, 1); System.out.println(num); } public static void dfs(int sum, int d) { if (d > n) { if (sum > num) { num = sum; } return; } int temp; b[d] = 1; temp = 0; for (int j = 1; j <= d; j++) { if (b[j] != 1) { temp += a[j][d]; } } dfs(sum+temp, d + 1); b[d] = 2; temp = 0; for (int j = 1; j <= d; j++) { if (b[j] != 2) { temp += a[j][d]; } } dfs(sum+temp, d + 1); } }

你可能感兴趣的:(算法,String,Class,import)