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/erb

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

【题意】

  有一个n*n的矩阵(n<=40),每个位置有三种情况,/表示不能用,C表示这个位置有一个芯片,'.'表示这个位置可以放芯片,要求第i行的芯片总数等于第i列的芯片总数,每行或的每列的芯片总数不能超过总芯片数的A/B。

【分析】

  没有行列约束的时候直接行列建边。对于A/B的约束其实可以枚举行列的最大值然后加限制跑最大流(因为行很少)。不过有行等于列的约束,于是再建一条列到行的反向边,上限定为你枚举的约束,做循环流。因为循环流要加边而且判满流,所以要加一个费用才能计算,于是就是上下界循环费用流了。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 10100
#define Maxm 1001000
#define INF 0xfffffff int map[][];
int first[Maxn],dis[Maxn],pre[Maxn],flow[Maxn];
char s[];
bool inq[Maxn]; int st,ed,sum,h;
int n,A,B; struct node
{
int x,y,f,c,o,next;
}t[Maxm];int len; int mymax(int x,int y) {return x>y?x:y;}
int mymin(int x,int y) {return x<y?x:y;} void ins(int x,int y,int f,int c)
{
if(f==) return;
if(x==st) sum+=f;
t[++len].x=x;t[len].y=y;t[len].f=f;t[len].c=c;
t[len].next=first[x];first[x]=len;t[len].o=len+;
t[++len].x=y;t[len].y=x;t[len].f=;t[len].c=-c;
t[len].next=first[y];first[y]=len;t[len].o=len-;
} void make_edge(int x,int y,int k1,int k2,int c)
{
ins(st,y,k2,);
ins(x,ed,k2,c);
ins(y,x,k2-k1,-c);
} queue<int > q;
bool bfs(int f1,int f2)
{
while(!q.empty()) q.pop();
memset(dis,-,sizeof(dis));
memset(inq,,sizeof(inq));
memset(pre,-,sizeof(pre));
inq[f1]=;q.push(f1);flow[f1]=INF;pre[f1]=;dis[st]=;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=first[x];i;i=t[i].next) if(t[i].f>)
{
int y=t[i].y;
if(dis[y]<dis[x]+t[i].c)
{
dis[y]=dis[x]+t[i].c;
if(!inq[y]) {q.push(y);inq[y]=;}
pre[y]=i;
flow[y]=mymin(flow[x],t[i].f);
}
}
inq[x]=;
}
if(pre[f2]==-) return ;
return flow[f2];
} int ffind(int x,int y)
{
int a,sc=,now;h=;
while(a=bfs(st,ed))
{
now=y;sc+=a*dis[y];
h+=a;
while(now!=x)
{
t[pre[now]].f-=a;
t[t[pre[now]].o].f+=a;
now=t[pre[now]].x;
}
}
return sc;
} int get_ans(int x)
{
st=*n+,ed=st+;
len=;sum=;
memset(first,,sizeof(first));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(map[i][j]==) make_edge(i,j+n,,,);
else if(map[i][j]==) ins(i,j+n,,);
}
for(int i=;i<=n;i++) make_edge(i+n,i,,x,);
int a=ffind(st,ed);
if(h!=sum) return -;
if(x*B<=a*A) return a;
return -;
} int main()
{
int kase=;
while()
{
scanf("%d%d%d",&n,&A,&B);
if(n==&&A==&&B==) break;
bool ok=;
int sc=;
for(int i=;i<=n;i++)
{
scanf("%s",s);
for(int j=;j<n;j++)
{
if(s[j]=='/') map[i][j+]=;
else if(s[j]=='.') map[i][j+]=;
else map[i][j+]=,sc++;
}
}
for(int i=;i<=n;i++)
{
int s1=,s2=;
for(int j=;j<=n;j++)
{
if(map[i][j]==) s1++;
if(map[j][i]==) s2++;
}
if(s1!=s2||s1*B>sc*A||s2*B>sc*A) {ok=;break;}
}
printf("Case %d: ",++kase);
int maxx=-;
if(ok) maxx=sc;
for(int i=;i<=n;i++)
{
maxx=mymax(maxx,get_ans(i)); }
if(maxx!=-) maxx-=sc;
if(maxx==-) printf("impossible\n");
else printf("%d\n",maxx);
}
return ;
}

[LA5131]

2016-06-04 13:27:29

【UVALive - 5131】Chips Challenge(上下界循环费用流)的更多相关文章

  1. 【BZOJ-4213】贪吃蛇 有上下界的费用流

    4213: 贪吃蛇 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 58  Solved: 24[Submit][Status][Discuss] Desc ...

  2. BZOJ2055 80人环游世界 网络流 费用流 有源汇有上下界的费用流

    https://darkbzoj.cf/problem/2055 https://blog.csdn.net/Clove_unique/article/details/54864211 ←对有上下界费 ...

  3. BZOJ 2055: 80人环游世界(有上下界的费用流)

    题面 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 693 Solved: 434 [Submit][Status][Discuss] Descript ...

  4. 【上下界网络流 费用流】bzoj2055: 80人环游世界

    EK费用流居然写错了…… Description     想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么     一个80人的团 ...

  5. Codeforces Gym 101190 NEERC 16 .D Delight for a Cat (上下界的费用流)

    ls是一个特别堕落的小朋友,对于n个连续的小时,他将要么睡觉要么打隔膜,一个小时内他不能既睡觉也打隔膜 ,因此一个小时内他只能选择睡觉或者打隔膜,当然他也必须选择睡觉或打隔膜,对于每一个小时,他选择睡 ...

  6. UvaL-7670 上下界可行费用流

    #include <iostream> #include <cstdio> #include <cstring> #include <queue> #d ...

  7. BZOJ 3876 支线剧情 有源汇有上下界最小费用可行流

    题意: 给定一张拓扑图,每条边有边权,每次只能从第一个点出发沿着拓扑图走一条路径,求遍历所有边所需要的最小边权和 分析: 这道题乍一看,可能会想到什么最小链覆盖之类的,但是仔细一想,会发现不行,一是因 ...

  8. BZOJ 2055 80人环游世界 有上下界最小费用可行流

    题意: 现在有这么一个m人的团伙,也想来一次环游世界. 他们打算兵分多路,游遍每一个国家.    因为他们主要分布在东方,所以他们只朝西方进军.设从东方到西方的每一个国家的编号依次为1...N.假若第 ...

  9. zoj3229 Shoot the Bullet(有源汇有上下界的最大流)

    题意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给给定的C个女神拍照,每天拍照数不能超过D张,而且给每个女神i拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能少于Gi,如果有解求屌 ...

随机推荐

  1. java输入输出流小细节

    package System; public class Systemdemo { public static void main(String args[]){ demo1(); demo2(); ...

  2. ZeroClipboard.swf

    ZeroClipboard.config({             moviePath: "source/ZeroClipboard.swf",             hove ...

  3. System Operations on AWS - Lab 5W - Managing Resources (Windows)

    登陆到CommandHost实例,通过AWS CLI来管理各个资源 1. 通过Tags来管理资源 1.1 登陆到CommandHost实例 1.2 找出development实例 1.2.1 打开Po ...

  4. JQuery判断子Iframe 加载完成的技术解决

    当需要我们给当前页面动态创建Iframe子框架的时候,并且同时需要操作子Iframe里的方法的时候,我们发现无法成功实现.这是为什么呢?经小程总结,发现子Iframe还没有来的及加载完成,就去执行里面 ...

  5. javascript 【js‘s word】

    http://mp.weixin.qq.com/s?__biz=MjM5MzY2NDY0Ng==&mid=214013689&idx=1&sn=21e03f6c7bf73893 ...

  6. PHP ajax实现数组返回

    首先,我想要实这样一个功能, 当选择一个下拉框时,让其它三个文本框得到从服务器上返回的值!也就把返回的值,赋给那三个文本框! 我用的是jquery+php!! 由于我前台,后台,js,数据库采用的都是 ...

  7. C#线程池基础

    池(Pool)是一个很常见的提高性能的方式.比如线程池连接池等,之所以有这些池是因 为线程和数据库连接的创建和关闭是一种比较昂贵的行为.对于这种昂贵的资源我们往往会考虑在一个池容器中放置一些资源,在用 ...

  8. ios7 苹果原生二维码扫描(和微信类似)

    在ios7苹果推出了二维码扫描,以前想要做二维码扫描,只能通过第三方ZBar与ZXing. ZBar在扫描的灵敏度上,和内存的使用上相对于ZXing上都是较优的,但是对于 “圆角二维码” 的扫描确很困 ...

  9. iOS-UI控件精讲之UIView

    道虽迩,不行不至:事虽小,不为不成. 相关阅读 1.iOS-UI控件精讲之UIView(本文) 2.iOS-UI控件精讲之UILabel ...待续 UIView是所有UI控件的基类,在布局的时候通常 ...

  10. MVC初学1

    MVC - Model ,View, Control 主要的程序思想:约定优于配置 百度下载程序:百度一下 密码:654321 百度视频地址:百度一下