加载中...

P1294 高手去散步

P1294 高手去散步 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

思路:

  1. dfs + 邻接矩阵
  2. 暴力穷举
public class P1294 {
	static int n;
	static int[] isVis;
	static int[][] edge;
	static int sum = 0;
	static int max;

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		n = scanner.nextInt();
		int m = scanner.nextInt();
		edge = new int[n + 1][n + 1];
		isVis = new int[n + 1];
		for (int i = 1; i <= m; i++) {
			int a = scanner.nextInt();
			int b = scanner.nextInt();
			int c = scanner.nextInt();
			edge[a][b] = c;
			edge[b][a] = c;// 无向图
		}
		for (int i = 1; i <= n; i++) {
			isVis[i] = 1;// 对第一行选取的点进行标记
			dfs(i);
		}
		System.out.println(max);
	}

	private static void dfs(int p) {
		max = Math.max(sum, max);// 求举例最大值
		for (int i = 1; i <= n; i++) {// 遍历没一列元素
			if (isVis[i] == 0 && edge[i][p] > 0) {// 状态符合要求
				sum += edge[i][p];
				isVis[i] = 1;// 取点,标记
				dfs(i);
				sum -= edge[i][p];// 回溯
			}
		}
		isVis[p] = 0;// 清除标志位,也属于回溯
	}
}

image-20230313162645204

posted @ 2023-03-13 16:28  ChuenSan  阅读(19)  评论(0编辑  收藏  举报