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

第一次用王建德的数组模拟邻接表。

Posted by stupidjohn at 2010-08-20 10:15:45 on Problem 1273
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int limit[201][201];
int flow[201][201];
int queue[400];
int rear,front;
int pre[201];
int nowmax[201];
struct node
{
	int to;
	int next;
}mymalloc[410];
int my;
int city[201];
int s,t,n,m;
int maxflow()
{
	int p,u,v,w,maxflow=0;
	memset(flow,0,sizeof(flow));
	for(;;)
	{
		front=rear=-1;
		queue[++front]=s;
		memset(nowmax,0,sizeof(nowmax));
		nowmax[s]=0xfffffff;
		while(front>rear)
		{
			u=queue[++rear];
			p=city[u];
			while(p!=-1)
			{
				v=mymalloc[p].to;
				if(!nowmax[v]&&limit[u][v]>flow[u][v])
				{
					queue[++front]=v;
					pre[v]=u;
					nowmax[v]=nowmax[u]<(limit[u][v]-flow[u][v])?nowmax[u]:(limit[u][v]-flow[u][v]);
				}
				p=mymalloc[p].next;
			}
			if(nowmax[t]) break;
		}
		if(!nowmax[t]) break;
		for(u=t;u!=s;u=pre[u])
		{
			flow[pre[u]][u]+=nowmax[t];
			flow[u][pre[u]]-=nowmax[t];
		}
		maxflow+=nowmax[t];
	}
	return maxflow;
}
void init()
{
	int i,from,to,cost;
	memset(city,-1,sizeof(city));
	s=1;t=n;
	my=0;
	memset(limit,0,sizeof(limit));
	for(i=0;i<m;i++)
	{
		scanf("%d%d%d",&from,&to,&cost);
		limit[from][to]+=cost;
		mymalloc[my].to=to;
		mymalloc[my].next=city[from];
		city[from]=my++;
		mymalloc[my].to=from;
		mymalloc[my].next=city[to];
		city[to]=my++;
	}
}
int main()
{
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		init();
		cout<<maxflow()<<endl;
	}
	return 1;
}

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