[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
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/
The last test case is followed by a line containing three zeros.
Follow the format of the sample output.
/.
//
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
Case 2: 1
Case 3: impossible
Case 4: 7
Case 5: impossible

//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[流量平衡]的更多相关文章
- bzoj3961[WF2011]Chips Challenge
题意 给出一个n*n的网格,有些格子必须染成黑色,有些格子必须染成白色,其他格子可以染成黑色或者白色.要求最后第i行的黑格子数目等于第i列的黑格子数目,且某一行/列的格子数目不能超过格子总数的A/B. ...
- 【UVALive - 5131】Chips Challenge(上下界循环费用流)
Description A prominent microprocessor company has enlisted your help to lay out some interchangeabl ...
- 【BZOJ 2673】[Wf2011]Chips Challenge
题目大意: 传送门 $n*n$的棋盘,有一些位置可以放棋子,有一些已经放了棋子,有一些什么都没有,也不能放,要求放置以后满足:第i行和第i列的棋子数相同,同时每行的棋子数占总数比例小于$\frac{A ...
- Bzoj2673 3961: [WF2011]Chips Challenge 费用流
国际惯例题面:如果我们枚举放几个零件的话,第二个限制很容易解决,但是第一个怎么办?(好的,这么建图不可做)考虑我们枚举每行每列最多放几个零件t,然后计算零件总数sum.这样如果可行的话,则有t*B&l ...
- 解题:BZOJ 2673 World Final 2011 Chips Challenge
题面 数据范围看起来很像网络流诶(滚那 因为限制多而且强,数据范围也不大,我们考虑不直接求答案,而是转化为判定问题 可以发现第二个限制相对好满足,我们直接枚举这个限制就可以.具体来说是枚举所有行中的最 ...
- BZOJ2673 [Wf2011]Chips Challenge 费用流 zkw费用流 网络流
https://darkbzoj.cf/problem/2673 有一个芯片,芯片上有N*N(1≤N≤40)个插槽,可以在里面装零件. 有些插槽不能装零件,有些插槽必须装零件,剩下的插槽随意. 要求装 ...
- [Wf2011]Chips Challenge
两个条件都不太好处理 每行放置的个数实际很小,枚举最多放x 但还是不好放 考虑所有位置先都放上,然后删除最少使得合法 为了凑所有的位置都考虑到,把它当最大流 但是删除最少,所以最小费用 行列相关,左行 ...
- bzoj 3961: [WF2011]Chips Challenge【最小费用最大流】
参考:https://blog.csdn.net/Quack_quack/article/details/50554032 神建图系列 首先把问题转为全填上,最少扣下来几个能符合条件 先考虑第2个条件 ...
- 【题解】uva1104 chips challenge
原题传送门 题目分析 给定一张n*n的芯片. '.'表示该格子可以放一个零件. 'C'表示该格子已经放了一个零件(不能拆下). '/'表示该格子不能放零件. 要求在芯片的现有基础上,放置尽可能多的零件 ...
随机推荐
- Win32 SDK - 打开文件对话框
OPENFILENAME ofn; // common dialog box structure TCHAR szFile[MAX_PATH]; // buffer for file name // ...
- resharper警告 :linq replace with single call to FirstOrDefault
使用resharper时对linq使用的FirstOrDefault 一直产生一个警告,
- 将相关数据拼成所需JSON数据
参考: http://www.cnblogs.com/shuilangyizu/p/6019561.html 有时候我们需要将一些数据拼装成所需要格式的JSON,可以使用如下方法,本人觉得还是比较方便 ...
- Intel 5 6 7 8系列芯片组介绍
Intel 5 6 7 8系列芯片组介绍 Iknow.2015-11-05 22:40|知识编号:122257 操作步骤: [Inetl 5.6.7.8系列芯片组介绍] 芯片组是主板电路的核心.一定意 ...
- (oneway void) release中oneway的意思
oneway is used with the distributed objects API, which allows use of objective-c objects between dif ...
- jQuery新建HTML Element
举个例: 创建一个<i>HelloWorld.</i> var italicText = $("<i></i>").text(&qu ...
- node js 调试出现同一个端口启动多次报错处理方案 Error: listen EADDRINUSE
windows 下 1.查询端口占用的进程ID: netstat -aon | findstr "80" 80为端口号, 输出为: TCP 0.0.0.0:3000 ...
- 一款基于jQuery的图片下滑切换焦点图插件
之前为大家分享了好多款jquery插件,今天我们要分享的一款jQuery插件也比较实用,是一款jQuery焦点图插件.焦点图相当普通,一共可以循环播放4张图片,并且每一张图片在切换的时候都是向下滑动的 ...
- lua文件读写
lua里的文件读写模型来自C语言,分为完整模型(和C一样).简单模型. 1.简单模型 io.input([file]) 设置默认的输入文件,file为文件名(此时会以文本读入)或文件句柄(可以理解为 ...
- 转:PHP获取浏览器类型及版本号
function getBrowser(){ $agent=$_SERVER["HTTP_USER_AGENT"]; if(strpos($agent,'MSIE')!==fals ...