poj 1459 (最大流)

最大流简单题,,这题重要的是知道了scanf("%s",str);sscanf(str,"(%d,%d)%d",&x,&y,&w);读入方式






 

#include<stdio.h>

#include<string.h>

const int N=210;

const int inf=0x3fffffff;

int dis[N],gap[N],head[N],num,start,end,ans,n,m;

struct edge

{

	int st,ed,flow,next;

}E[21000];

struct node

{

	int x,y,w;

}P[40000];

void addedge(int x,int y,int w)

{

	E[num].st=x;E[num].ed=y;E[num].flow=w;E[num].next=head[x];head[x]=num++;

	E[num].st=y;E[num].ed=x;E[num].flow=0;E[num].next=head[y];head[y]=num++;

}

int dfs(int u,int minflow)

{

	if(u==end)return minflow;

	int i,v,f,flow=0,min_dis=ans-1;

	for(i=head[u];i!=-1;i=E[i].next)

	{

		if(E[i].flow)

		{

			v=E[i].ed;

			if(dis[v]+1==dis[u])

			{

				f=dfs(v,E[i].flow>minflow-flow?minflow-flow:E[i].flow);

				E[i].flow-=f;

				E[i^1].flow+=f;

				flow+=f;

				if(flow==minflow)break;

				if(dis[start]>=ans)return flow;

			}

			min_dis=min_dis>dis[v]?dis[v]:min_dis;

		}

	}

	if(flow==0)

	{

		if(--gap[dis[u]]==0)

			dis[start]=ans;

		dis[u]=min_dis+1;

		gap[dis[u]]++;

	}

	return flow;

}

int isap()

{

	int maxflow=0;

	memset(dis,0,sizeof(dis));

	memset(gap,0,sizeof(gap));

	gap[0]=ans;

	while(dis[start]<ans)

	   maxflow+=dfs(start,inf);

	return maxflow;

}

int main()

{

	int i,k,x,y,w,t;

	char str[20];

	while(scanf("%d%d%d%d",&n,&m,&k,&t)!=-1)

	{

		start=0;end=n+1;ans=end+1;

		memset(head,-1,sizeof(head));

		num=0;

		for(i=0;i<t;i++)

		{

			scanf("%s",str);

			sscanf(str,"(%d,%d)%d",&x,&y,&w);

			addedge(x+1,y+1,w);

		}

		for(i=0;i<m;i++)

		{

			scanf("%s",str);

			sscanf(str,"(%d)%d",&x,&w);

			addedge(start,x+1,w);

		}

		for(i=0;i<k;i++)

		{

			scanf("%s",str);

			sscanf(str,"(%d)%d",&x,&w);

			addedge(x+1,end,w);

		}

		printf("%d\n",isap());



	}

	return 0;

}




 

 

你可能感兴趣的:(poj)