zoukankan      html  css  js  c++  java
  • [2011WorldFinal]Chips Challenge[流量平衡]

    Chips Challenge

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 96    Accepted Submission(s): 33


    Problem Description
    A prominent microprocessor company has enlisted your help to lay out some interchangeable components (widgets) on some of their computer chips. Each chip’s design is an N×N square of slots. One slot can hold a single component, and you are to try to fit in as many widgets as possible.

    Modern processor designs are complex, of course. You unfortunately have several restrictions:
      • Some of the slots are disabled.

      • Some of the slots are already occupied by other components and cannot be used for widgets.

      • There are sibling memory buses connected to the horizontal and vertical edges of the chip and their bandwidth loads need to match. As such, there must be exactly as many components in the first row as in the first column, exactly as many in the second row as in the second column, and so on. Component counts include both the components already specified on the chip and the added widgets.

      • Similarly, the power supply is connected at the end of each row and column. To avoid hot spots, any given row or column must have no more than A=B of the total components on the chip for a given A and B.



    A specification for a chip is N lines of N characters, where ‘.’ indicates an open slot, ‘/’ indicates a disabled slot, and ‘C’ indicates a slot already occupied by a component. For example:

    CC/..
    ././/
    ..C.C
    /.C..
    /./C/

    If no more than 3/10 of the components may be in any one row or column, the maximum number of widgets that can be added to this 5 × 5 chip is 7. A possible arrangement is below, where ‘W’ indicates a widget added in an open slot.

    CC/W.
    W/W//
    W.C.C
    /.CWW
    /W/C/

     
    Input
    The input consists of several test cases. Each case starts with a line containing three integers: The size of the chip N (1 <= N <= 40), and A and B (1 <= B <= 1000, 0 <= A <= B) as described above. Each of the following N lines contains N characters describing the slots, one of ‘.’, ‘/’ or ‘C’, as described above.
    The last test case is followed by a line containing three zeros.
     
    Output
    For each test case, display a single line beginning with the case number. If there is a solution, display the maximum number of widgets that can be added to the chip. Display “impossible” if there is no solution.
    Follow the format of the sample output.
     
    Sample Input
    2 1 1 /. // 2 50 100 /. C/ 2 100 100 ./ C. 5 3 10 CC/.. ./.// ..C.C /.C.. /./C/ 5 2 10 CC/.. ./.// ..C.C /.C.. /./C/ 0 0 0
     
    Sample Output
    Case 1: 0 Case 2: 1 Case 3: impossible Case 4: 7 Case 5: impossible
     
    Source
     
    Recommend

    //EK 1825ms
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #define m(s) memset(s,0,sizeof s);
    #define EF if(ch==EOF) return x;
    using namespace std;
    const int N=100;
    const int M=N*N<<2;
    struct edge{int v,next,cap,cost;}e[M];int tot=1,head[N];
    int n,cas,A,B,ans,S,T,rd[N],cd[N],dis[N],pre[N],q[N+M];bool vis[N];
    int sum,hav,maxflow,maxcost;char s[N][N];
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;EF;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    void add(int x,int y,int z,int cost=0){
        e[++tot].v=y;e[tot].cap=z;e[tot].cost=cost;e[tot].next=head[x];head[x]=tot;
        e[++tot].v=x;e[tot].cap=0;e[tot].cost=-cost;e[tot].next=head[y];head[y]=tot;
    }
    bool spfa(){
        memset(vis,0,sizeof vis);
        memset(dis,0x3f,sizeof dis);
        unsigned short h=0,t=1;q[t]=S;dis[S]=0;vis[S]=1;
        while(h!=t){
            int x=q[++h];vis[x]=0;
            for(int i=head[x];i;i=e[i].next){
                if(e[i].cap&&dis[e[i].v]>dis[x]+e[i].cost){
                    dis[e[i].v]=dis[x]+e[i].cost;
                    pre[e[i].v]=i;
                    if(!vis[e[i].v]){
                        vis[e[i].v]=1;
                        q[++t]=e[i].v;
                    }                
                }
            }
        }
        return dis[T]<0x3f3f3f3f;
    }
    void augment(){
        int flow=0x3f3f3f3f;         
        for(int i=T;i!=S;i=e[pre[i]^1].v) flow=min(flow,e[pre[i]].cap);
        for(int i=T;i!=S;i=e[pre[i]^1].v){
            e[pre[i]].cap-=flow;
            e[pre[i]^1].cap+=flow;
        }
        maxflow+=flow;
        maxcost+=dis[T]*flow;
    }
    int main(){
        while(1){
            n=read();A=read();B=read();
            if(!n) return 0;
            printf("Case %d: ",++cas);
            S=0,T=n<<1|1;
            sum=0;hav=0;ans=-1;m(rd);m(cd);
            for(int i=1;i<=n;i++){
                scanf("%s",s[i]+1);
                for(int j=1;j<=n;j++){
                    if(s[i][j]=='C'||s[i][j]=='.'){
                        sum++;cd[i]++,rd[j]++;
                        if(s[i][j]=='C') hav++;
                    }
                }
            }
            for(int maxt=0;maxt<=n;maxt++){
                tot=1;m(head);
                for(int i=1;i<=n;i++){
                    add(S,i,cd[i]);add(i,i+n,maxt);add(i+n,T,rd[i]);
                    for(int j=1;j<=n;j++){
                        if(s[i][j]=='.') add(i,j+n,1,1);
                    }
                }
                maxflow=maxcost=0;
                while(spfa()) augment();
                if(maxflow==sum&&(maxflow-maxcost)*A>=maxt*B) ans=max(ans,maxflow-maxcost);
            }
            if(~ans) printf("%d
    ",ans-hav);
            else puts("impossible");
        }
        return 0;
    }
    //zkw 280ms
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #define m(s) memset(s,0,sizeof s);
    #define EF if(ch==EOF) return x;
    using namespace std;
    const int N=100;
    const int M=N*N<<2;
    struct edge{int v,next,cap,cost;}e[M];int tot=1,head[N];
    int n,cas,A,B,ans,S,T,rd[N],cd[N],dis[N],pre[N],q[N+M],tim,mark[N];bool vis[N];
    int sum,hav,maxflow,maxcost;char s[N][N];
    inline int read(){
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;EF;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void add(int x,int y,int z,int cost=0){
        e[++tot].v=y;e[tot].cap=z;e[tot].cost=cost;e[tot].next=head[x];head[x]=tot;
        e[++tot].v=x;e[tot].cap=0;e[tot].cost=-cost;e[tot].next=head[y];head[y]=tot;
    }
    inline bool spfa(){
        memset(vis,0,sizeof vis);
        memset(dis,0x3f,sizeof dis);
        unsigned short h=0,t=1;q[t]=S;dis[S]=0;vis[S]=1;
        while(h!=t){
            int x=q[++h];vis[x]=0;
            for(int i=head[x];i;i=e[i].next){
                if(e[i].cap&&dis[e[i].v]>dis[x]+e[i].cost){
                    dis[e[i].v]=dis[x]+e[i].cost;
                    pre[e[i].v]=i;
                    if(!vis[e[i].v]){
                        vis[e[i].v]=1;
                        q[++t]=e[i].v;
                    }                
                }
            }
        }
        return dis[T]<0x3f3f3f3f;
    }
    int dfs(int x,int f){
        if(x==T) return f;
        int used=0,t;
        mark[x]=tim;
        for(int i=head[x];i;i=e[i].next){
            if((mark[e[i].v]^tim)&&e[i].cap&&dis[e[i].v]==dis[x]+e[i].cost){
                t=dfs(e[i].v,min(f,e[i].cap));
                e[i].cap-=t;e[i^1].cap+=t;
                used+=t;f-=t;
                if(!f) return used;
            }
        }
        if(!used) dis[x]=-1;
        return used;
    }
    inline void zkw(){
        int flow=0;
        while(spfa()){
            while(tim++,flow=dfs(S,0x3f3f3f3f)){
                maxflow+=flow;
                maxcost+=dis[T]*flow;
            }
        }
    }
    int main(){
        while(1){
            n=read();A=read();B=read();
            if(!n) return 0;
            printf("Case %d: ",++cas);
            S=0,T=n<<1|1;
            sum=0;hav=0;ans=-1;m(rd);m(cd);
            for(int i=1;i<=n;i++){
                scanf("%s",s[i]+1);
                for(int j=1;j<=n;j++){
                    if(s[i][j]=='C'||s[i][j]=='.'){
                        sum++;cd[i]++,rd[j]++;
                        if(s[i][j]=='C') hav++;
                    }
                }
            }
            for(int maxt=0;maxt<=n;maxt++){
                tot=1;m(head);
                for(int i=1;i<=n;i++){
                    add(S,i,cd[i]);add(i,i+n,maxt);add(i+n,T,rd[i]);
                    for(int j=1;j<=n;j++){
                        if(s[i][j]=='.') add(i,j+n,1,1);
                    }
                }
                maxflow=maxcost=0;
                zkw();
                if(maxflow==sum&&(maxflow-maxcost)*A>=maxt*B) ans=max(ans,maxflow-maxcost);
            }
            if(~ans) printf("%d
    ",ans-hav);
            else puts("impossible");
        }
        return 0;
    }

  • 相关阅读:
    javascript常用对象
    oracle储存过程,job,视图,触发器(记性不好,写个例子自己记)
    xml直接读取节点
    脑瓜子的文章导航 脑瓜子的学院系列文章汇总
    ASP.NET MVC 中实现View与Controller分离
    开发
    SQLite学习手册(转)
    缓存数据库redis、memcached。 MongoDB 资源集锦
    在GOOGLE浏览器中模拟移动浏览器 调试Web app
    Intelligencia.UrlRewriter在IIS 7.0下的完全配置攻略
  • 原文地址:https://www.cnblogs.com/shenben/p/6666270.html
Copyright © 2011-2022 走看看