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=;
const int M=N*N<<;
struct edge{int v,next,cap,cost;}e[M];int tot=,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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;EF;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void add(int x,int y,int z,int cost=){
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=;e[tot].cost=-cost;e[tot].next=head[y];head[y]=tot;
}
bool spfa(){
memset(vis,,sizeof vis);
memset(dis,0x3f,sizeof dis);
unsigned short h=,t=;q[t]=S;dis[S]=;vis[S]=;
while(h!=t){
int x=q[++h];vis[x]=;
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]=;
q[++t]=e[i].v;
}
}
}
}
return dis[T]<0x3f3f3f3f;
}
void augment(){
int flow=0x3f3f3f3f;
for(int i=T;i!=S;i=e[pre[i]^].v) flow=min(flow,e[pre[i]].cap);
for(int i=T;i!=S;i=e[pre[i]^].v){
e[pre[i]].cap-=flow;
e[pre[i]^].cap+=flow;
}
maxflow+=flow;
maxcost+=dis[T]*flow;
}
int main(){
while(){
n=read();A=read();B=read();
if(!n) return ;
printf("Case %d: ",++cas);
S=,T=n<<|;
sum=;hav=;ans=-;m(rd);m(cd);
for(int i=;i<=n;i++){
scanf("%s",s[i]+);
for(int j=;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=;maxt<=n;maxt++){
tot=;m(head);
for(int i=;i<=n;i++){
add(S,i,cd[i]);add(i,i+n,maxt);add(i+n,T,rd[i]);
for(int j=;j<=n;j++){
if(s[i][j]=='.') add(i,j+n,,);
}
}
maxflow=maxcost=;
while(spfa()) augment();
if(maxflow==sum&&(maxflow-maxcost)*A>=maxt*B) ans=max(ans,maxflow-maxcost);
}
if(~ans) printf("%d\n",ans-hav);
else puts("impossible");
}
return ;
}
//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=;
const int M=N*N<<;
struct edge{int v,next,cap,cost;}e[M];int tot=,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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;EF;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void add(int x,int y,int z,int cost=){
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=;e[tot].cost=-cost;e[tot].next=head[y];head[y]=tot;
}
inline bool spfa(){
memset(vis,,sizeof vis);
memset(dis,0x3f,sizeof dis);
unsigned short h=,t=;q[t]=S;dis[S]=;vis[S]=;
while(h!=t){
int x=q[++h];vis[x]=;
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]=;
q[++t]=e[i].v;
}
}
}
}
return dis[T]<0x3f3f3f3f;
}
int dfs(int x,int f){
if(x==T) return f;
int used=,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^].cap+=t;
used+=t;f-=t;
if(!f) return used;
}
}
if(!used) dis[x]=-;
return used;
}
inline void zkw(){
int flow=;
while(spfa()){
while(tim++,flow=dfs(S,0x3f3f3f3f)){
maxflow+=flow;
maxcost+=dis[T]*flow;
}
}
}
int main(){
while(){
n=read();A=read();B=read();
if(!n) return ;
printf("Case %d: ",++cas);
S=,T=n<<|;
sum=;hav=;ans=-;m(rd);m(cd);
for(int i=;i<=n;i++){
scanf("%s",s[i]+);
for(int j=;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=;maxt<=n;maxt++){
tot=;m(head);
for(int i=;i<=n;i++){
add(S,i,cd[i]);add(i,i+n,maxt);add(i+n,T,rd[i]);
for(int j=;j<=n;j++){
if(s[i][j]=='.') add(i,j+n,,);
}
}
maxflow=maxcost=;
zkw();
if(maxflow==sum&&(maxflow-maxcost)*A>=maxt*B) ans=max(ans,maxflow-maxcost);
}
if(~ans) printf("%d\n",ans-hav);
else puts("impossible");
}
return ;
}

[2011WorldFinal]Chips Challenge[流量平衡]的更多相关文章

  1. bzoj3961[WF2011]Chips Challenge

    题意 给出一个n*n的网格,有些格子必须染成黑色,有些格子必须染成白色,其他格子可以染成黑色或者白色.要求最后第i行的黑格子数目等于第i列的黑格子数目,且某一行/列的格子数目不能超过格子总数的A/B. ...

  2. 【UVALive - 5131】Chips Challenge(上下界循环费用流)

    Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...

  3. 【BZOJ 2673】[Wf2011]Chips Challenge

    题目大意: 传送门 $n*n$的棋盘,有一些位置可以放棋子,有一些已经放了棋子,有一些什么都没有,也不能放,要求放置以后满足:第i行和第i列的棋子数相同,同时每行的棋子数占总数比例小于$\frac{A ...

  4. Bzoj2673 3961: [WF2011]Chips Challenge 费用流

    国际惯例题面:如果我们枚举放几个零件的话,第二个限制很容易解决,但是第一个怎么办?(好的,这么建图不可做)考虑我们枚举每行每列最多放几个零件t,然后计算零件总数sum.这样如果可行的话,则有t*B&l ...

  5. 解题:BZOJ 2673 World Final 2011 Chips Challenge

    题面 数据范围看起来很像网络流诶(滚那 因为限制多而且强,数据范围也不大,我们考虑不直接求答案,而是转化为判定问题 可以发现第二个限制相对好满足,我们直接枚举这个限制就可以.具体来说是枚举所有行中的最 ...

  6. BZOJ2673 [Wf2011]Chips Challenge 费用流 zkw费用流 网络流

    https://darkbzoj.cf/problem/2673 有一个芯片,芯片上有N*N(1≤N≤40)个插槽,可以在里面装零件. 有些插槽不能装零件,有些插槽必须装零件,剩下的插槽随意. 要求装 ...

  7. [Wf2011]Chips Challenge

    两个条件都不太好处理 每行放置的个数实际很小,枚举最多放x 但还是不好放 考虑所有位置先都放上,然后删除最少使得合法 为了凑所有的位置都考虑到,把它当最大流 但是删除最少,所以最小费用 行列相关,左行 ...

  8. bzoj 3961: [WF2011]Chips Challenge【最小费用最大流】

    参考:https://blog.csdn.net/Quack_quack/article/details/50554032 神建图系列 首先把问题转为全填上,最少扣下来几个能符合条件 先考虑第2个条件 ...

  9. 【题解】uva1104 chips challenge

    原题传送门 题目分析 给定一张n*n的芯片. '.'表示该格子可以放一个零件. 'C'表示该格子已经放了一个零件(不能拆下). '/'表示该格子不能放零件. 要求在芯片的现有基础上,放置尽可能多的零件 ...

随机推荐

  1. some nets were not able to be matched

    原因是:PCB画好之后再次更改原理图,将更改后的原理图更新至PCB的时候会导致原理图中新生成的网络和PCB中原有的网络名不匹配 解决办法:PCB---设计----网络表---编辑网络,把PCB中不匹配 ...

  2. unity, GUIStyle and Skin

    例1: using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic ...

  3. PCH简单介绍

    https://wenku.baidu.com/view/3c9c5f190a4e767f5acfa1c7aa00b52acfc79cb0.html

  4. mount -o remount,rw / (这是个求命的命令)

    当系统无法启动的时候.我们会前进入单用户模式(正常情况下单用户莫式是权限是正常的,只在在无法启动的情况下, 再进入单用户模式,权限才会是只读),这时候没有对文件的修改权限(所有的文件都是只读) 用这条 ...

  5. Overflow sort stage buffered data usage of 33554495 bytes exceeds internal limit of 33554432 bytes

    MongoDB执行错误: Overflow sort stage buffered data usage of 33554495 bytes exceeds internal limit of 335 ...

  6. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创]

    mkdir -p /data0/software cd /data0/software wget http://sysoev.ru/nginx/nginx-0.8.46.tar.gz wget htt ...

  7. oracle 函数判断字符串是否包含图片格式

    首先是写一个分割字符串的函数,返回table类型 CREATE OR REPLACE FUNCTION fn_split (p_str IN VARCHAR2, p_delimiter IN VARC ...

  8. 一定要使用-server参数来调试并发程序

    在阅读JCIP的时候想手工测试一下,结果发现总是没有出现书中描述的并发问题 后来我琢磨,以前记得书上说过,在debugging的环境下,JVM是低并发的,一定要在server的环境下测试,让JVM在高 ...

  9. 为什么 Windows API 使用 stdcall 调用约定?

    作者:知乎用户链接:https://www.zhihu.com/question/31453641/answer/52001143来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  10. MySQL 找回密码

    Windows: 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant-tables回车.如果没有出现提示信息,那就对了. ...