隐藏页面特效

[2011WorldFinal]Chips Challenge[流量平衡]

1|0Chips 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\n",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\n",ans-hav); else puts("impossible"); } return 0; }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/6666270.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(444)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示