zoukankan      html  css  js  c++  java
  • POJ-1087 A Plug for UNIX(网络流)(EK,dinic,二分匹配)

    A Plug for UNIX
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 17928 Accepted: 6200
    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.
    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







    laptop B 
    phone C 
    pager B 
    clock B 
    comb X 

    B X 
    X A 
    X D 
    Sample Output

    1

    题意:插座有n个,电器有m个,每个电器有自己适合的插座,有k个转换器。求最少有几个不能充电的电器。

    思路:可以求最大匹配,也可以用最大流,建一个源点一个汇点。

    二分图:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<string>
    #include<map>
    #include<vector>
    #define maxn 1510
    using namespace std;
    int g[maxn][maxn];
    bool used[maxn];
    int linker[maxn];
    int k,ans,y,x,z;
    map<string,int>p,q;
    bool dfs(int u){
        for(int i=1; i<=x; i++){
            if(g[u][i]&&!used[i]){
                used[i]=true;
                if(linker[i]==-1||dfs(linker[i])){
                    linker[i]=u;
                    return true;}
            }
        }
        return false;}
    int hungary(){
        int res=0;
        memset(linker,-1,sizeof(linker));
        for(int i=x+y+101; i<=x+y+101+y; i++){
            memset(used,false,sizeof(used));
            if(dfs(i))res++;}
        return y-res;}
    int main(){
        while(~scanf("%d",&x)){
            p.clear();
            q.clear();
            memset(g,0,sizeof(g));
            k=x+1;
            string s1,s2;
            for(int i=1; i<=x; i++){
                cin>>s2;p[s2]=i;}
            scanf("%d",&y);
            ans=x+y+100+1;
            for(int i=1; i<=y; i++){
                cin>>s1>>s2;
                q[s1]=ans;
                ans++;
                if(!p[s2]){
                    p[s2]=k;
                    k++;}
                g[q[s1]][p[s2]]=1;}
            scanf("%d",&z);
            for(int i=0; i<z; i++){
                cin>>s1>>s2;
                if(!p[s1]){
                    p[s1]=k;
                    k++;}
                if(!p[s2]){
                    p[s2]=k;
                    k++;}
                g[p[s1]][p[s2]]=1;}
            for(int i=1; i<ans; i++) {
                for(int j=1; j<ans; j++){
                    for(int K=1; K<ans; K++){
                        g[j][K]=g[j][K]||(g[j][i]&&g[i][K]);}
                }
            }
            cout<<hungary()<<endl;}
    }

    最大流:

    EK

    #include<map>
    #include<set>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define inf 0x3f3f3f
    #define ll long long
    #define maxn 600
    using namespace std;
    map<string,int>plug,device;
    int g[maxn][maxn];
    bool used[maxn];
    int linker[maxn];
    int x,e,s;
    string s1,s2;
    void net(int u,int flow){
        while(linker[u]!=-1){
            g[linker[u]][u]-=flow;
            g[u][linker[u]]+=flow;
            u=linker[u];}}
    int bfs(int s,int t){
        memset(used,false,sizeof(used));
        memset(linker,-1,sizeof(linker));
        used[s]=true;
        queue<int>q;
        q.push(s);int minn=inf;
        while(!q.empty()){
            int cur=q.front();
            q.pop();if(cur==t)break;
            for(int i=0;i<=t;i++){
                if(!used[i]&&g[cur][i]){
                    used[i]=true;linker[i]=cur;q.push(i);
                    minn=minn<g[cur][i]?minn:g[cur][i];}
            }
        }
        if(linker[t]==-1)return 0;
        return minn;}
    int ek(int s,int t){
        int an=0,ans=0;do{
        an=bfs(s,t);
        ans+=an;net(t,an);
        }while(an!=0);
        return ans;}
    int main(){
        int n;
        while(~scanf("%d",&n)){
            memset(g,0,sizeof(g));
            x=1;plug.clear();e=500;s=0;
            for(int i=0;i<n;i++){
               string s;cin>>s;
               if(plug[s]==0)plug[s]=x++;
               g[0][plug[s]]=1;
            }int m;scanf("%d",&m);
            for(int i=0;i<m;i++){
                cin>>s1>>s2;
                if(plug[s1]==0)plug[s1]=x++;
                if(plug[s2]==0)plug[s2]=x++;
                g[plug[s2]][plug[s1]]=1;
                g[plug[s1]][e]=1;
            }int T;scanf("%d",&T);
            for(int i=0;i<T;i++){
                string s0,ss;cin>>s0>>ss;
                if(plug[ss]==0)plug[ss]=x++;
                if(plug[s0]==0)plug[s0]=x++;
                g[plug[ss]][plug[s0]]=inf;}
            cout<<m-ek(s,e)<<endl;
        }
    }
    

    dinic:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    #include<string>
    #include<vector>
    #include<queue>
    #include<map>
    #define maxn 1100
    #define inf 0x3f3f3f
    using namespace std;
    struct node{
        int v,w;
        struct node *next;
    }*h[maxn];
    int d[maxn];
    int s,t,x,y,z,k;
    map<string,int>p,q;
    void w(int u,int v,int w){
        if(h[u]!=NULL){
            struct node*q;
            for(q=h[u]; q!=NULL; q=q->next){
                if(q->v==v){
                    q->w+=w;
                    return;}
            }
        }
        struct node*p=(struct node*)malloc(sizeof(struct node));
        p->w=w;
        p->v=v;
        p->next=h[u];
        h[u]=p;}
    bool bfs(){
        queue<int>q;
        while(!q.empty())q.pop();
        memset(d,0,sizeof(d));
        d[s]=1;
        q.push(s);
        do{
            int u=q.front();
            q.pop();
            struct node*T;
            for(T=h[u]; T!=NULL; T=T->next){
                if((T->w)>0&&d[T->v]==0){
                    d[T->v]=d[u]+1;
                    q.push(T->v);}
            }
        }
        while(!q.empty());
        if(d[t]==0)return false;
        else
            return true;}
    int  dfs(int u,int dist){
        if(u==t)
            return dist;
        struct node *p;
        for(p=h[u]; p!=NULL; p=p->next){
            if((d[p->v]==d[u]+1)&&(p->w)>0){
                int di=dfs(p->v,min(dist,p->w));
                if(di>0){
                    w(u,p->v,-di);
                    w(p->v,u,di);
                    return di;}
            }
        }
        return 0;}
    int dinic(){
        int an=0;
        while(bfs()){
            while(int d=dfs(s,inf)){
                an+=d;}
        }
        return y-an;}
    int main(){
        while(~scanf("%d",&x)){
            for(int i=0;i<maxn;i++)h[i]=NULL;
            p.clear();k=x+1;s=0;t=500;
            string s1,s2;
            for(int i=1; i<=x; i++){
                cin>>s2;
                p[s2]=i;
                w(0,p[s2],1);}
            scanf("%d",&y);
            for(int i=1; i<=y; i++){
                cin>>s1>>s2;
                if(!p[s1]){
                    p[s1]=k;k++;}
                if(!p[s2]){
                    p[s2]=k;k++;}
                w(p[s2],p[s1],1);
                w(p[s1],t,1);}
            scanf("%d",&z);
            for(int i=0; i<z; i++){
                cin>>s1>>s2;
                if(!p[s1]){
                    p[s1]=k; k++;}
                if(!p[s2]){
                    p[s2]=k; k++;}
                w(p[s2],p[s1],inf);}
            cout<<dinic()<<endl;}
    }




  • 相关阅读:
    3、SpringBoot执行原理
    10、@Controller跟@RestController注解的使用
    2、Spring项目的创建【官网跟IDEA】
    1、了解SpringBoot
    PHP算法之IP 地址无效化
    PHP算法之宝石与石头
    MYSQL查询查找重复的电子邮箱
    PHP算法之猜数字
    PHP算法之盛最多水的容器
    PHP算法之回文数
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053272.html
Copyright © 2011-2022 走看看