zoukankan      html  css  js  c++  java
  • 洛谷P3254 圆桌问题(最大流)

    传送门

    一道良心啊……没那么多麻烦了……

    从$S$向所有单位连边,容量为单位人数,从所有桌子向$T$连边,容量为桌子能坐的人数,从每一个单位向所有桌子连边,容量为$1$,然后跑一个最大流,看一看$S$到单位这一边流满了没,如果没有就无解。方案的话,就看单位到哪一个桌子有流就行

    因为手写队列然后两个$t$弄混了结果死都不知道哪里错……

     1 //minamoto
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<queue>
     6 #define inf 0x3f3f3f3f
     7 using namespace std;
     8 #define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
     9 char buf[1<<21],*p1=buf,*p2=buf;
    10 inline int read(){
    11     #define num ch-'0'
    12     char ch;bool flag=0;int res;
    13     while(!isdigit(ch=getc()))
    14     (ch=='-')&&(flag=true);
    15     for(res=num;isdigit(ch=getc());res=res*10+num);
    16     (flag)&&(res=-res);
    17     #undef num
    18     return res;
    19 }
    20 char sr[1<<21],z[20];int C=-1,Z;
    21 inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
    22 inline void print(int x){
    23     if(C>1<<20)Ot();
    24     while(z[++Z]=x%10+48,x/=10);
    25     while(sr[++C]=z[Z],--Z);
    26 }
    27 const int N=505,M=100005;
    28 int head[N],Next[M],ver[M],edge[M],cur[N],tot=1;
    29 int dep[N],n,m,u,v,e,vis[N],s,t,sum=0;
    30 queue<int> q;
    31 inline void add(int u,int v,int e){
    32     ver[++tot]=v,Next[tot]=head[u],head[u]=tot,edge[tot]=e;
    33     ver[++tot]=u,Next[tot]=head[v],head[v]=tot,edge[tot]=0;
    34 }
    35 bool bfs(){
    36     memset(dep,-1,sizeof(dep));
    37     while(!q.empty()) q.pop();
    38     for(int i=0;i<=n+m+1;++i) cur[i]=head[i];
    39     q.push(s),dep[s]=0;
    40     while(!q.empty()){
    41         int u=q.front();q.pop();
    42         for(int i=head[u];i;i=Next[i]){
    43             int v=ver[i];
    44             if(dep[v]<0&&edge[i])
    45             dep[v]=dep[u]+1,q.push(v);
    46         }
    47     }
    48     return ~dep[t];
    49 }
    50 int dfs(int u,int limit){
    51     if(!limit||u==t) return limit;
    52     int flow=0,f;
    53     for(int i=cur[u];i;i=Next[i]){
    54         int v=ver[i];cur[u]=i;
    55         if(dep[v]==dep[u]+1&&(f=dfs(v,min(limit,edge[i])))){
    56             flow+=f,limit-=f;
    57             edge[i]-=f,edge[i^1]+=f;
    58             if(!limit) break;
    59         }
    60     }
    61     return flow;
    62 }
    63 int dinic(){
    64     int flow=0;
    65     while(bfs()) flow+=dfs(s,inf);
    66     return flow;
    67 }
    68 int main(){
    69     m=read(),n=read();
    70     s=0,t=n+m+1;
    71     for(int i=1;i<=m;++i){
    72         e=read(),add(s,i,e),sum+=e;
    73         for(int j=1;j<=n;++j)
    74         add(i,j+m,1);
    75     }
    76     for(int i=1;i<=n;++i){
    77         e=read();add(i+m,t,e);
    78     }
    79     if(dinic()!=sum) return puts("0"),0;
    80     print(1),sr[++C]='
    ';
    81     for(int i=1;i<=m;++i){
    82         for(int j=head[i];j;j=Next[j]){
    83             if(!edge[j]&&ver[j]>m&&ver[j]<=n+m)
    84             print(ver[j]-m),sr[++C]=' ';
    85         }
    86         sr[++C]='
    ';
    87     }
    88     Ot();
    89     return 0;
    90 }
  • 相关阅读:
    How To Install a 2 Finger Gripper on ABB Robots
    Guide: How to Set Up I/O on an ABB Robot with an IRC5 Controller
    SIGVerse
    unity与ROS SIGVerse 仿真
    论文阅读:Automated acquisition of structured, semantic models of manipulation activities from human VR demonstration
    Python2.7+ROS环境:AttributeError:‘module’ has no attribute ‘CALIB_HAND_EYE_TSAI
    Qt音视频开发39-人脸识别在线版
    Qt音视频开发38-USB摄像头解码linux方案
    Qt音视频开发37-USB摄像头解码ffmpeg方案
    由浅入深讲述MVVM
  • 原文地址:https://www.cnblogs.com/bztMinamoto/p/9499673.html
Copyright © 2011-2022 走看看