zoukankan      html  css  js  c++  java
  • poj 1087 (最大流)

    题意:在一个会议室里有n种插座,每种插座一个,每个插座只能插一种以及一个电器(或者适配器),有m个电器,每个电器有一个插头需要插在相应一种插座上,不是所有电器都能在会议室找到相应插座,有k种适配器,每种适配器可以有无限多数量,每种适配器(a, b)可以把b类插座变为a类插座,问最后有多少个电器无法使用。

    建图:源点,电器,插座,汇点,,源点跟电器建边,流量为1,电器与对应的插座连边,流量为1,转换器(a,b),a与b连边,流量无穷大,房间已有的插座与汇点连边,流量为1,,





    #include<iostream>
    #include<stdio.h>
    #include<map>
    #include<string.h>
    #include<string>
    #include <algorithm>
    const int N=501;
    const int inf=0x3fffffff;
    int gap[N],dis[N],head[N],num,start,end,ans;
    using namespace std;
    map< string , int > hash;
    struct edge
    {
    	int st,ed,flow,next;
    }E[N*10];
    void addedge(int x,int y,int flow)
    {
    	E[num].st=x;E[num].ed=y;E[num].flow=flow;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,min_dis=ans-1,flow=0;
    	for(i=head[u];i!=-1;i=E[i].next)
    	{
    		if(E[i].flow>0)
    		{
    			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]])))
    			dis[start]=ans;
    		dis[u]=min_dis+1;
    		gap[dis[u]]++;
    	}
    return flow;
    }
    int isap()
    {
    	int maxflow=0;
    	memset(gap,0,sizeof(gap));
    	memset(dis,0,sizeof(dis));
    	gap[0]=ans;
    	while(dis[start]<ans)
    	  maxflow+=dfs(start,inf);
    	return maxflow;
    }
    int main()
    {
    	int n,m,k,i,j;
    	char str1[30],str2[30];
    	while(scanf("%d",&n)!=-1)
    	{
    		memset(head,-1,sizeof(head));
    		num=0;start=0;end=500;ans=1;
    		for(i=0;i<n;i++)
    		{
    			scanf("%s",str1);
    			if(hash[str1]==0)
    			{
    				hash[str1]=ans;
    				addedge(ans++,end,1);
    			}
    		}
    		scanf("%d",&m);
    		for(i=0;i<m;i++)
    		{
    			scanf("%s%s",str1,str2);
    			addedge(start,ans++,1);
    			if(hash[str2]==0)
    			{
    				hash[str2]=ans;
    				addedge(ans-1,ans,1);
    				ans++;
    			}
    			else addedge(ans-1,hash[str2],1);
    		}
    		scanf("%d",&k);
    		for(i=0;i<k;i++)
    		{
    			scanf("%s%s",str1,str2);
    			if(hash[str1]==0)
    				hash[str1]=ans++;
    			if(hash[str2]==0)
    				hash[str2]=ans++;
    			addedge(hash[str1],hash[str2],inf);
    		}
    		ans++;
    		printf("%d
    ",m-isap());
    	}
    	return 0;
    }


  • 相关阅读:
    LeetCode Flatten Binary Tree to Linked List
    LeetCode Longest Common Prefix
    LeetCode Trapping Rain Water
    LeetCode Add Binary
    LeetCode Subsets
    LeetCode Palindrome Number
    LeetCode Count and Say
    LeetCode Valid Parentheses
    LeetCode Length of Last Word
    LeetCode Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/riskyer/p/3253726.html
Copyright © 2011-2022 走看看