标签:
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
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.
There‘s a blank line between test cases.
1 4 A B C D 5 laptop B phone C pager B clock B comb X 3 B X X A X D
1
Source
题意:输入n,有n个插座,下面n行是每个插座的类型(最后24个字母来表示一个插座,没有空格放心用scanf,但是有可能插座会相同,但是这个没有什么影响)
输入m,有m个电器,下面m行每行两个单词分别是电器的名字和插头类型(同样24个字母单词内没空格,两个单词空格隔开)
输入k,有k个转换器,下面k行每行两个单词,分别表示转换器的入口类型和插头类型
每种转换器的个数是无限的,转换器本身可以与转换器相连
要你求,让最多的电器能够插在插座上(可以用转换器辅助也可以直接插上去),输出不能插上去的电器的数量
s与所有电器建一条有向边,容量为1,所有插座与汇点建一条有向边,容量为1。对于每个电器,如果能直接和插座相连的,和每个能相连的插座建一条有向边,容量为1.另外,所有电器和所有能连接上的转换器建一条有向边,容量为1,转换器和转换器之间能相连的建一条有向边容量为INF,转换器和插座能相连建一条有向边容量为1。
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; int head[int(1e3)],tail; struct Edge { int from,to,next,cap,flow; }edge[int(1e6)]; void add(int from,int to,int cap,int flow) { edge[tail].from=from; edge[tail].to=to; edge[tail].cap=cap; edge[tail].flow=flow; edge[tail].next=head[from]; head[from]=tail++; } int up[int(1e3)],path[int(1e3)]; int work(int ed) { int ans=0; while(1) { queue<int>qq; memset(up,0,sizeof(up)); memset(path,-1,sizeof(path)); up[0]=INT_MAX; qq.push(0); while(qq.size()) { int from=qq.front(); qq.pop(); for(int i=head[from];i!=-1;i=edge[i].next) if(up[edge[i].to]==0&&edge[i].cap>edge[i].flow) { path[edge[i].to]=i; qq.push(edge[i].to); up[edge[i].to]=min(up[from],edge[i].cap-edge[i].flow); } } if(up[ed]==0) return ans; ans+=up[ed]; for(int i=path[ed];i!=-1;i=path[edge[i].from]) { edge[i].flow+=up[ed]; edge[i^1].flow-=up[ed]; } } } string rp[110],dv[110][2],ap[110][2]; int main() { int T; cin>>T; while(T--) { int n; cin>>n; for(int i=0;i<n;i++) cin>>rp[i]; int m; cin>>m; for(int i=0;i<m;i++) for(int j=0;j<2;j++) cin>>dv[i][j]; int k; cin>>k; for(int i=0;i<k;i++) for(int j=0;j<2;j++) cin>>ap[i][j]; tail=0; memset(head,-1,sizeof(head)); for(int i=0;i<m;i++) { add(0,n+i+1,1,0); add(n+i+1,0,0,0); for(int j=0;j<n;j++) if(dv[i][1]==rp[j]) { add(n+1+i,1+j,1,0); add(1+j,n+1+i,0,0); } for(int j=0;j<k;j++) if(dv[i][1]==ap[j][0]) { add(n+1+i,n+m+1+j,1,0); add(n+m+1+j,n+1+i,0,0); } } for(int i=0;i<k;i++) { for(int j=0;j<k;j++) if(ap[i][1]==ap[j][0]) { add(n+m+1+i,n+m+1+j,INT_MAX,0); add(n+m+1+j,n+m+1+i,0,0); } for(int j=0;j<n;j++) if(ap[i][1]==rp[j]) { add(n+m+1+i,1+j,1,0); add(1+j,n+m+1+i,0,0); } } for(int i=0;i<n;i++) { add(1+i,n+m+k+1,1,0); add(n+m+k+1,1+i,0,0); } cout<<m-work(n+m+k+1)<<endl; if(T) cout<<endl; } }
标签:
原文地址:http://blog.csdn.net/stl112514/article/details/44310745