• POJ


    You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible.
    Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling
    irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can.
    Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug.
    In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.

    Input
    The input will consist of one case. The first line contains a single positive integer n (1 <= n <= 100) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m (1 <= m <= 100) indicating the number of devices you would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric
    characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k (1 <= k <= 100) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.

    Output
    A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.
    Sample Input
    4 
    A 
    B 
    C 
    D 
    5 
    laptop B 
    phone C 
    pager B 
    clock B 
    comb X 
    3 
    B X 
    X A 
    X D 
    Sample Output
    1

    题意:

    给出N个插座,每种插座的名字是个字符串,N个中可能会有同种的。给出M个设备和每个设备要用的插座类型。给出K种适配器,适配器可以转换插座,例如适配器(B,X)可以把B型插座转换成X型,每种适配器是无限的。最后问最少剩下几台机器没插座用。

    题解:

    老规矩超级源点超级汇点,不过这次不用拆点了。直接让超级源点连接各种插座,边权为已有的插座数。插座再连超级汇点,边权为需要的插座数。如图:其中绿边为适配器边,边权为无限。


    坑点:

    注意适配器(a,b)建边应该是b->a。另外凡是图中涉及到的边,边权为0也要加进去。

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    const int MAXN = 310;
    
    struct Edge{
    	int to,value,rev;
    	Edge(){}
    	Edge(int a,int b,int c):to(a),value(b),rev(c){}
    };
    
    vector<Edge> E[MAXN];
    
    inline void Add(int from,int to,int value){
    	E[from].push_back(Edge(to,value,E[to].size()));
    	E[to].push_back(Edge(from,0,E[from].size()-1));
    }
    
    int deep[MAXN];
    int iter[MAXN];
    
    bool BFS(int from,int to){
    	memset(deep,-1,sizeof deep);
    	deep[from] = 0;
    	queue<int> Q;
    	Q.push(from);
    	while(!Q.empty()){
    		int t = Q.front();
    		Q.pop();
    		for(int i=0 ; i<E[t].size() ; ++i){
    			Edge& e = E[t][i];
    			if(e.value > 0 && deep[e.to] == -1){
    				deep[e.to] = deep[t] + 1;
    				Q.push(e.to);
    			}
    		}
    	}
    	return deep[to] != -1;
    }
    
    int DFS(int from,int to,int flow){
    	if(from == to || flow == 0)return flow;
    	
    	for(int& i=iter[from] ; i<E[from].size() ; ++i){
    		Edge& e = E[from][i];
    		if(e.value > 0 && deep[e.to] == deep[from]+1){
    			int nowflow = DFS(e.to,to,min(flow,e.value));
    			if(nowflow > 0){
    				e.value -= nowflow;
    				E[e.to][e.rev].value += nowflow;
    				return nowflow;
    			}
    		}
    	}
    	return 0;
    }
    
    
    int Dinic(int from,int to){
    	int sumflow = 0;
    	while(BFS(from,to)){
    		memset(iter,0,sizeof iter);
    		int mid;
    		while((mid=DFS(from,to,INF)) > 0)sumflow += mid;
    	}
    	return sumflow;
    }
    
    struct Type{
    	string name;//插座名字 
    	int give;//提供的数量 
    	int need;//需要的数量 
    	Type(){}
    	Type(string a,int b,int c):name(a),give(b),need(c){}
    };
    
    vector<Type> T;
    
    int main(){
    	
    	int N,M,K;
    	while(scanf("%d",&N)!=EOF){
    		for(int i=0 ; i<N ; ++i){
    			string t;
    			cin>>t;
    			int j;
    			for(j=0 ; j<T.size() ; ++j){
    				if(T[j].name == t){
    					T[j].give++;
    					break;
    				}
    			}
    			if(j == T.size())T.push_back(Type(t,1,0));
    		}
    		scanf("%d",&M);
    		for(int i=0 ; i<M ; ++i){
    			string a,b;
    			cin>>a>>b;
    			int j;
    			for(j=0 ; j<T.size() ; ++j){
    				if(T[j].name == b){
    					T[j].need++;
    					break;
    				}
    			}
    			if(j == T.size())T.push_back(Type(b,0,1));
    		}
    		for(int i=0 ; i<T.size() ; ++i){
    			Add(0,i+1,T[i].give);
    			Add(i+1,MAXN-1,T[i].need);
    		}
    		scanf("%d",&K);
    		for(int i=0 ; i<K ; ++i){
    			string a,b;
    			cin>>a>>b;
    			//--------------------------------------------
    			/*这一段检测适配器中出现的插座是否是之前没出现过的
    			如果是还需要建其与超级源点的边。 
    			*/
    			int j;
    			for(j=0 ; j<T.size() ; ++j)if(T[j].name == a)break;
    			if(j == T.size()){
    				T.push_back(Type(a,0,0));
    				Add(0,T.size(),0);
    			}
    			for(j=0 ; j<T.size() ; ++j)if(T[j].name == b)break;
    			if(j == T.size()){
    				T.push_back(Type(b,0,0));
    				Add(0,T.size(),0);
    			}
    			//---------------------------------------------
    			int aa,bb;
    			for(int i=0 ; i<T.size() ; ++i){//循环一遍找适配器对应的点。 
    				if(T[i].name == a)aa = i+1;
    				if(T[i].name == b)bb = i+1;
    			}
    			Add(bb,aa,INF);//根据适配器加边。 
    		}
    		printf("%d
    ",M-Dinic(0,MAXN-1));
    		T.clear();
    		for(int i=0 ; i<MAXN ; ++i)E[i].clear();
    	} 
    	
    	return 0;
    }

  • 相关阅读:
    JSP 072: 处理注册结果样式的显示
    JSP 07: 开发注册页面
    JSP 06: 两个内置对象request和response
    Java Web 01: 什么是http协议
    JSP 05: JSP定义表达式和内容输出表达式
    JSP 04: 如何在JSP页面中书写Java代码
    JSP 03: 创建一个JSP页面并启动运行项目
    Fail 02: Port 8080 required by Tomcat Server at localhost is already in use.
    Fail 03: netstat 不是内部或外部命令
    Build 01: 安装新的JDK
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514088.html
Copyright © 2020-2023  润新知