• poj 1087 最大流


    没啥好说的,慢慢建图

     

    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个不同的插座,有m台不同的机器需要m种插头,有k组转换:插头A能由插头B转换而来。问这些机器最少有几台不能插上插座。

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<queue>
      7 #include<map>
      8 using namespace std;
      9 const int MAXN=500;
     10 const int INF=0x3fffffff;
     11 int g[MAXN][MAXN];//存边的容量,没有边的初始化为0
     12 int path[MAXN],flow[MAXN],start,end;
     13 int n;//点的个数,编号0-n.n包括了源点和汇点。
     14 queue<int>q;
     15 int bfs()
     16 {
     17     int i,t;
     18     while(!q.empty())q.pop();//把清空队列
     19     memset(path,-1,sizeof(path));//每次搜索前都把路径初始化成-1
     20     path[start]=0;
     21     flow[start]=INF;//源点可以有无穷的流流进
     22     q.push(start);
     23     while(!q.empty())
     24     {
     25         t=q.front();
     26         q.pop();
     27         if(t==end)break;
     28         //枚举所有的点,如果点的编号起始点有变化可以改这里
     29         for(i=0;i<=n;i++)
     30         {
     31             if(i!=start&&path[i]==-1&&g[t][i])
     32             {
     33                 flow[i]=flow[t]<g[t][i]?flow[t]:g[t][i];
     34                 q.push(i);
     35                 path[i]=t;
     36             }
     37         }
     38     }
     39     if(path[end]==-1)return -1;//即找不到汇点上去了。找不到增广路径了
     40     return flow[end];
     41 }
     42 int Edmonds_Karp()
     43 {
     44     int max_flow=0;
     45     int step,now,pre;
     46     while((step=bfs())!=-1)
     47     {
     48         max_flow+=step;
     49         now=end;
     50         while(now!=start)
     51         {
     52             pre=path[now];
     53             g[pre][now]-=step;
     54             g[now][pre]+=step;
     55             now=pre;
     56         }
     57     }
     58     return max_flow;
     59 }
     60 
     61 map<string,int> hash;
     62 int main()
     63 {
     64     int i,j,k,m;
     65     //freopen("1.in","r",stdin);
     66     while(scanf("%d",&n)!=EOF)
     67     {
     68         hash.clear();
     69         memset(g,0,sizeof(g));
     70         string s1,s2,s3;
     71         start=0;
     72         end=1;
     73         int tot=2;
     74         while(n--)
     75         {
     76             cin>>s1;
     77             hash[s1]=tot;
     78             g[0][hash[s1]]=1;
     79             tot++;
     80         }
     81         scanf("%d",&m);
     82         for(i=0;i<m;i++)
     83         {
     84             cin>>s1>>s2;
     85             if(hash[s1]==0) hash[s1]=tot++;
     86             if(hash[s2]==0) hash[s2]=tot++;
     87             g[hash[s1]][end]=1;
     88             g[hash[s2]][hash[s1]]=1;
     89         }
     90         scanf("%d",&k);
     91         while(k--)
     92         {
     93             cin>>s1>>s2;
     94             if(hash[s1]==0) hash[s1]=tot++;
     95             if(hash[s2]==0) hash[s2]=tot++;
     96             g[hash[s2]][hash[s1]]=INF;
     97         }
     98         n=tot-1;    //总点数
     99         printf("%d
    ",m-Edmonds_Karp());
    100     }
    101     return 0;
    102 }
  • 相关阅读:
    [转载]在sharepoint里增加PDF图标显示收藏主题
    CuratorFramework开源Zookeeper快速开发框架介绍
    Zookeeper和CuratorFramework实践之:分布式消息队列
    Zookeeper和CuratorFramework实践系列之: 配置管理
    JS 文字向上滚动代码
    sql递归查询问题
    双线单IP和双线双IP机房的区别
    js获取标准北京时间
    ftp 21端口被占用解决办法
    "由于没有远程桌面授权服务器可以提供许可证..."不能远程桌面解决方法
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4316950.html
Copyright © 2020-2023  润新知