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

为何会T

Posted by veasonapple at 2015-09-18 20:17:58 on Problem 2987
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<utility>
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
int n, m, a, b, temp, head, tail, t, ans1;
long long sum, ans2;
int w[5010][5010], q[5010], link[5010];
bool vis[5010];
vector<int>v[5010];
bool bfs()
{
	head = tail = 0;
	q[head++] = 0;
	memset(vis, false, sizeof(vis));
	vis[0] = true;
	while (head != tail)
	{
		temp = q[tail++];
		for (int i = 0; i < v[temp].size(); i++)
		{
			if (!vis[v[temp][i]] && w[temp][v[temp][i]]>0)
			{
				link[v[temp][i]] = temp;
				if (v[temp][i] == t)
				{
					return true;
				}
				vis[v[temp][i]] = true;
				q[head++] = v[temp][i];
			}
		}
	}
	return false;
}
long long ek()
{
	long long s = 0;
	int f;
	for (;;)
	{
		if (bfs())
		{
			f = inf;
			temp = t;
			while (temp)
			{
				f = min(f, w[link[temp]][temp]);
				temp = link[temp];
			}
			s += f;
			temp = t;
			while (temp)
			{
				w[link[temp]][temp] -= f;
				w[temp][link[temp]] += f;
				temp = link[temp];
			}
		}
		else
		{
			return s;
		}
	}
}
void dfs(int x)
{
	vis[x] = true;
	ans1++;
	for (int i = 0; i < v[x].size(); i++)
	{
		if (!vis[v[x][i]] && w[x][v[x][i]]>0)
		{
			dfs(v[x][i]);
		}
	}
}
int main()
{
	scanf("%d%d", &n, &m);
	t = n + 1;
	sum = 0;
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a)
		{
			if (a > 0)
			{
				w[0][i] = a;
				v[0].push_back(i);
				v[i].push_back(0);
				sum += a;
			}
			else
			{
				w[i][t] = -a;
				v[i].push_back(t);
				v[t].push_back(i);
			}
		}
	}
	for (int i = 0; i < m; i++)
	{
		scanf("%d%d", &a, &b);
		if (!w[a][b] && !w[b][a])
		{
			v[a].push_back(b);
			v[b].push_back(a);
		}
		w[a][b] = inf;
	}
	ans1 = 0;
	ans2 = sum - ek();
	memset(vis, false, sizeof(vis));
	dfs(0);
	printf("%d %lld\n", ans1 - 1, ans2);
	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