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

2144K 500MS 二分图的匹配(付代码),构图还是比较精妙的

Posted by bobten2008 at 2009-07-15 22:24:50 on Problem 1034
#include <iostream>
#include <cmath>
#define MAX_POINT 100
#define MAX_NODE 400
#define DIST(x1, y1, x2, y2) (((x1) - (x2)) * ((x1) -  (x2)) + ((y1) - (y2)) * ((y1) -  (y2)))
using namespace std;

static int graph[MAX_NODE + 5][MAX_NODE + 5];
static int gf[MAX_NODE + 5][MAX_NODE + 5];
static int f[MAX_NODE + 5][MAX_NODE + 5];

static int posH[MAX_POINT + 5][2];
static int posI[MAX_POINT + 5][2];
int start = 0, hNum = 0;
int end, iNum = 0;

int pre[MAX_NODE + 5];
char color[MAX_NODE + 5];
int bfsQ[MAX_NODE + 5];
int qhead, qtail;
int cfp;


int getShortest()
{
	for(int i = 1; i <= end; i++)
	{
		color[i] = 'W';
		pre[i] = -1;
	}
	qhead = qtail = 1;
	pre[0] = -1;
	bfsQ[qtail] = 0;
	qtail = qtail % (MAX_NODE + 5) + 1;

	while(qhead != qtail)
	{
		int curNode = bfsQ[qhead];
		qhead = qhead % (MAX_NODE + 5) + 1;
		for(int newNode = 0; newNode <= end; newNode++)
		{
			if(newNode != curNode && gf[curNode][newNode] >= 1 && color[newNode] == 'W')
			{
				color[newNode] = 'G';
				pre[newNode] = curNode;
				bfsQ[qtail] = newNode;
				qtail = qtail % (MAX_NODE + 5) + 1;
			}
		}
		color[curNode] = 'B';
	}
	if(pre[end] == -1)
		return -1;
	cfp = 999999999;
	int lastV = end;
	int preV = pre[lastV];
	while(preV != 0)
	{
		if(gf[preV][lastV] < cfp)
			cfp = gf[preV][lastV];

		lastV = preV;
		preV = pre[lastV];
	}
	
	return 0;	
}
void admondsKarp()
{
	while(getShortest() != -1)
	{
		int lastV = end;
		int preV = pre[lastV];
		while(preV != -1)
		{
			gf[preV][lastV] = gf[preV][lastV] - cfp;
			gf[lastV][preV] = gf[lastV][preV] + cfp;

			f[preV][lastV] = f[preV][lastV] + cfp;
			f[lastV][preV] = -f[preV][lastV];

			lastV = preV;
			preV = pre[lastV];
		}
	}
}
int main()
{
	int i, j, tempX, tempY;
	cin>>hNum>>iNum;
	end = MAX_NODE + 1;
	for(i = 1; i <= hNum; i++)
	{
		cin>>tempX>>tempY;
		posH[i][0] = tempX, posH[i][1] = tempY;
		if(i != 1)
			graph[100 + i][end] = gf[100 + i][end] = 1;
		if(i != end - 1)
			graph[0][i] = gf[0][i] = 1;
	}
	for(i = 1; i <= iNum; i++)
	{
		cin>>tempX>>tempY;
		posI[i][0] = tempX, posI[i][1] = tempY;
		graph[200 + i][300 + i] = gf[200 + i][300 + i] = 1;
	}
	for(i = 1; i <=hNum - 1; i++)
	{
		for(j = 1; j <= iNum; j++)
		{
			double dist1 = sqrt(double(DIST(posH[i][0], posH[i][1], posI[j][0], posI[j][1])));
			double dist2 = sqrt(double(DIST(posH[i + 1][0], posH[i + 1][1], posI[j][0], posI[j][1])));
			double dist3 = sqrt(double(DIST(posH[i][0], posH[i][1], posH[i + 1][0], posH[i + 1][1])));

			if(dist1 + dist2 <= 2 * dist3)
			{
				graph[i][200 + j] = gf[i][200 + j] = 1;
				graph[300 + j][100 + i + 1] = gf[300 + j][100 + i + 1] = 1;
			}
		}
	}
	admondsKarp();
	int nodeCount = hNum;
	for(i = 1; i <= hNum - 1; i++)
	{
		for(j = 1; j <= iNum; j++)
			if(f[i][j + 200] == 1)
				nodeCount++;
	}
	cout<<nodeCount<<endl;
	for(i = 1; i <= hNum - 1; i++)
	{
		if(i != 1)
			cout<<" ";
		cout<<posH[i][0]<<" "<<posH[i][1];
		for(j = 1; j <= iNum; j++)
			if(f[i][j + 200] == 1)
				cout<<" "<<posI[j][0]<<" "<<posI[j][1];
	}
	cout<<" "<<posH[hNum][0]<<" "<<posH[hNum][1];
	cout<<endl;
	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