POJ 3140 Contestants Division

WA

1. 忘了是无向图

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <deque>
#include <cstring>
#define MIN(x,y) (x)<(y)?(x):(y)
using namespace std;

int total_weight;
int weight[100100];
vector<int> graph[100100];
int min_cut;

int dfs(int u, int pre) {
	
	int sum = 0;

	for(int i = 0; i < graph[u].size(); i ++)  {
		int v = graph[u][i];
		if(v == pre)  continue;
		int party = dfs(v, u);
		sum += party;

		min_cut = min(min_cut, total_weight-party);
	}

	return sum+weight[u];
}

int main() {
	freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin);
	
	int n, m;
	while(scanf("%d%d", &n, &m) != EOF && n != 0 && m != 0)  {
		total_weight = 0;
		for(int i = 0; i < n; i ++)  {
			scanf("%d", weight+i+1);
			total_weight += weight[i+1];
		}

		for(int i = 0; i < m; i ++)  {
			int from, to;
			scanf("%d%d", &from, &to);
			graph[from].push_back(to);
			graph[to].push_back(from);
		}

		min_cut = 0x3f3f3f3f;

		dfs(1, -1);
		printf("%d
", min_cut);
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/zhouzhuo/p/3677455.html