[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'表示该格子已经放了一个零件(不能拆下). '/'表示该格子不能放零件. 要求在芯片的现有基础上,放置尽可能多的零件 ...
随机推荐
- ACCESS与MSSQL比较:SQL语句关于时间格式使用的注意点
ACCESS与MSSQL比较:SQL语句关于时间字符串的使用:ACCESS数据库使用 # 来控制时间格式字符串:mssql数据库使用单引号 ' 来控制时间格式字符串.例: ACCESS版本:UPDAT ...
- tp模型和数据库操作方法
一.新建的模型名和表名一样,采用驼峰式,如表名user_type模型取名为UserType namespace app\index\model;use think\Model;class UserTy ...
- CYQ学习主要摘要4
http://www.cnblogs.com/cyq1162/archive/2010/11/03/1867642.html Xml的处理 http://www.cnblogs.com/cyq1162 ...
- Observable 示例之 Windows Phone 列表内项目逐个加载
在写 Windows phone应用性能优化(一)的时候,在 ListBox 的项加载的时候,添加了一些简单的动画. 其实在 Windows Phone 的应用中使用 Blend 设计动画是很容易的, ...
- Qt禁止调整窗口的大小
项目中使用的是基于对话框的程序,所以限制调整窗口大小会比较合适,下面是两种方法. 1.使用代码修改 #include "dialog.h" #include "ui_di ...
- rpc 理解
RPC=Remote Produce Call 是一种技术的概念名词. HTTP是一种协议,RPC可以通过HTTP来实现,也可以通过Socket自己实现一套协议来实现. rpc是一种概念,http也是 ...
- 使用python对mysql主从进行监控,并调用钉钉发送报警信息
1.编写python的监控脚本 A.通过获取mysql库中的状态值来判断这个mysql主从状态是否正常 B.进行两个状态值的判断 C.进行调取钉钉机器人,发送消息 2.设置定时任务进行脚本运行 cro ...
- c#第一个程序-计算平方根
上课教的内容.做笔记了. using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...
- cocos2dx对所有子节点设置透明度
看到cocos2dx2.2.5发布了,修复了输入框的bug,于是我们的项目也升级到了2.2.5, 升级过程还是比较顺利,没想到后来发现设置透明度无效了. 经过调试发现要调用一下setCascadeOp ...
- [初学WPF]控件大小自适应
想在Win上自己写点小工具用,GUI自然是免不了的,于是决定学一学WPF,直接拖控件是很方便啊.控件拖出来以后发现运行时改变窗口大小控件不会重绘,搜索了一下发现了解决办法:使用Viewbox控件. V ...