Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴个写得有点挫的stoer_wagner,4900MS+

Posted by speedcell4 at 2012-03-19 19:22:09 on Problem 2914
In Reply To:两组数据 Posted by:speedcell4 at 2012-03-19 14:09:29
#include<iostream>

using namespace std;

const int MAXN = 512;
const int inf = 0x7f7f7f7f;

bool vis[MAXN];
int u, v, w, n, m, ver[MAXN], dis[MAXN], map[MAXN][MAXN];

int stoer_wagner(int n)
{
	int s, t, cnt = inf;
	
	for (int i = 0; n - i > 0; i++)
		ver[i] = i;
	
	while (n > 1)
	{
		vis[0] = true;
		for (int i = 1; n - i > 0; i++)
		{
			vis[i] = false;
			dis[i] = map[ver[0]][ver[i]];
		}
		s = 0;
		for (int i = 1; n - i > 0; i++)
		{
			t = -1;
			for (int j = 1; n - j > 0; j++)
				if (!vis[j])
				{
					if (t == -1 || dis[j] > dis[t]) t = j;
				}
			if (i == n - 1)
			{
				cnt = min(cnt, dis[t]);
				for (int j = 0; n - j > 0; j++)
				{
					map[ver[s]][ver[j]] += map[ver[t]][ver[j]];
					map[ver[j]][ver[s]] += map[ver[j]][ver[t]];
				}
				ver[t] = ver[--n];
			}
			else
			{
				vis[s = t] = true;
				for (int j = 1; n - j > 0; j++)
					if (!vis[j])
					{
						dis[j] += map[ver[t]][ver[j]];
					}
			}
		}
	}
	return cnt;
}
int main()
{
	while (scanf("%d %d", &n, &m) != EOF)
	{
		memset(map, 0, sizeof(map));
		
		while (m--)
		{
			scanf("%d %d %d", &u, &v, &w);
			map[u][v] += w;
			map[v][u] += w;
		}
		
		printf("%d\n", stoer_wagner(n));
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator