• 「网络流 24 题」圆桌聚餐


    网络流水题,详细看代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int n,m,tot=-1,h[3005],ans=0,sum=0;
     5 struct node{
     6     int from,next,to,rest,full;
     7     int last;
     8 }e[100005];
     9 void add(int x,int y,int z){
    10     tot++;
    11     e[tot].next=h[x];
    12     h[x]=tot;
    13     e[tot].from=x;
    14     e[tot].to=y;
    15     e[tot].rest=z;
    16     e[tot].full=z;
    17 }
    18 
    19 int dis[3005],g[3005],flow[3005];
    20 bool vis[3005];
    21 
    22 int bfs(int s,int t){
    23     queue<int>q;
    24     dis[s]=0;
    25     q.push(s);vis[s]=true;
    26     while(!q.empty()){
    27         int u=q.front();vis[u]=false;q.pop();
    28         for(int i=h[u];i!=(-1);i=e[i].next){
    29             if(dis[e[i].to]>dis[u]+1&&g[e[i].to]==(-1)&&e[i].rest>0){
    30                 g[e[i].to]=i;
    31                 flow[e[i].to]=min(flow[u],e[i].rest);
    32                 dis[e[i].to]=dis[u]+1;
    33                 if(vis[e[i].to]==false){
    34                     vis[e[i].to]=true;
    35                     q.push(e[i].to);
    36                 }
    37             }
    38         }
    39     }
    40 }
    41 
    42 int EK(int s,int t){
    43     while(1){
    44         memset(vis,false,sizeof(vis));
    45         memset(dis,0x7f,sizeof(dis));
    46         memset(flow,0x7f,sizeof(flow));
    47         memset(g,-1,sizeof(g));
    48         bfs(s,t);
    49         if(g[t]==(-1))return 0;
    50         ans+=flow[t];
    51         for(int p=t;p!=(s);p=e[g[p]].from){
    52             e[g[p]].rest-=flow[t];
    53             e[g[p]^1].rest+=flow[t];
    54         }    
    55         
    56     }
    57 }
    58 
    59 int main(){
    60     memset(h,-1,sizeof(h));
    61     cin>>m>>n;
    62     for(int i=1;i<=m;i++){
    63         int co;cin>>co;sum+=co;
    64         add(0,i,co);
    65         add(i,0,0);
    66         for(int j=1;j<=n;j++){
    67             add(i,j+m,1);
    68             add(j+m,i,0);
    69         }
    70     }
    71     for(int i=1;i<=n;i++){
    72         int co;cin>>co;
    73         add(i+m,n+m+1,co);
    74         add(n+m+1,i+m,0);
    75     }
    76     EK(0,n+m+1);
    77     if(sum>ans){
    78         cout<<0<<endl;
    79         exit(0);
    80     }
    81     cout<<1<<endl;
    82     for(int i=1;i<=m;i++){
    83         for(int j=h[i];j!=(-1);j=e[j].next){
    84             if(e[j].to!=0&&e[j].rest==0){
    85                 cout<<e[j].to-m<<" ";
    86             }
    87         }
    88         cout<<endl;
    89     }
    90 }
    View Code
  • 相关阅读:
    C#,asp.net,命名空间名,类名,方法名的获得
    asp.net引用用户控件
    SQL数据是否存在(是否有数据)判断,表,存储过程是否存在
    asp:Button 事件,点击事件 html Button runat="sever"
    CSS图片最大大小限制
    asp.net 路径
    Js实现网站的重定向,Js转向网址,Js跳转
    ViewState 页面状态保留
    vs 附加到进程
    sql XML处理,sp_xml_preparedocument,openxml
  • 原文地址:https://www.cnblogs.com/shatianming/p/12227600.html
Copyright © 2020-2023  润新知