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

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的约束其实可以枚举行列的最大值然后加限制跑最大流(因为行很少)。不过有行等于列的约束,于是再建一条列到行的反向边,上限定为你枚举的约束,做循环流。因为循环流要加边而且判满流,所以要加一个费用才能计算,于是就是上下界循环费用流了。

  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<queue>
  7 using namespace std;
  8 #define Maxn 10100
  9 #define Maxm 1001000
 10 #define INF 0xfffffff
 11
 12 int map[50][50];
 13 int first[Maxn],dis[Maxn],pre[Maxn],flow[Maxn];
 14 char s[50];
 15 bool inq[Maxn];
 16
 17 int st,ed,sum,h;
 18 int n,A,B;
 19
 20 struct node
 21 {
 22     int x,y,f,c,o,next;
 23 }t[Maxm];int len;
 24
 25 int mymax(int x,int y) {return x>y?x:y;}
 26 int mymin(int x,int y) {return x<y?x:y;}
 27
 28 void ins(int x,int y,int f,int c)
 29 {
 30     if(f==0) return;
 31     if(x==st) sum+=f;
 32     t[++len].x=x;t[len].y=y;t[len].f=f;t[len].c=c;
 33     t[len].next=first[x];first[x]=len;t[len].o=len+1;
 34     t[++len].x=y;t[len].y=x;t[len].f=0;t[len].c=-c;
 35     t[len].next=first[y];first[y]=len;t[len].o=len-1;
 36 }
 37
 38
 39 void make_edge(int x,int y,int k1,int k2,int c)
 40 {
 41     ins(st,y,k2,0);
 42     ins(x,ed,k2,c);
 43     ins(y,x,k2-k1,-c);
 44 }
 45
 46 queue<int > q;
 47 bool bfs(int f1,int f2)
 48 {
 49     while(!q.empty()) q.pop();
 50     memset(dis,-63,sizeof(dis));
 51     memset(inq,0,sizeof(inq));
 52     memset(pre,-1,sizeof(pre));
 53     inq[f1]=1;q.push(f1);flow[f1]=INF;pre[f1]=0;dis[st]=0;
 54     while(!q.empty())
 55     {
 56         int x=q.front();q.pop();
 57         for(int i=first[x];i;i=t[i].next) if(t[i].f>0)
 58         {
 59             int y=t[i].y;
 60             if(dis[y]<dis[x]+t[i].c)
 61             {
 62                 dis[y]=dis[x]+t[i].c;
 63                 if(!inq[y]) {q.push(y);inq[y]=1;}
 64                 pre[y]=i;
 65                 flow[y]=mymin(flow[x],t[i].f);
 66             }
 67         }
 68         inq[x]=0;
 69     }
 70     if(pre[f2]==-1) return 0;
 71     return flow[f2];
 72 }
 73
 74 int ffind(int x,int y)
 75 {
 76     int a,sc=0,now;h=0;
 77     while(a=bfs(st,ed))
 78     {
 79         now=y;sc+=a*dis[y];
 80         h+=a;
 81         while(now!=x)
 82         {
 83             t[pre[now]].f-=a;
 84             t[t[pre[now]].o].f+=a;
 85             now=t[pre[now]].x;
 86         }
 87     }
 88     return sc;
 89 }
 90
 91 int get_ans(int x)
 92 {
 93     st=2*n+1,ed=st+1;
 94     len=0;sum=0;
 95     memset(first,0,sizeof(first));
 96     for(int i=1;i<=n;i++)
 97       for(int j=1;j<=n;j++)
 98       {
 99           if(map[i][j]==2) make_edge(i,j+n,1,1,1);
100           else if(map[i][j]==1) ins(i,j+n,1,1);
101       }
102      for(int i=1;i<=n;i++) make_edge(i+n,i,0,x,0);
103     int a=ffind(st,ed);
104     if(h!=sum) return -1;
105     if(x*B<=a*A) return a;
106     return -1;
107 }
108
109 int main()
110 {
111     int kase=0;
112     while(1)
113     {
114         scanf("%d%d%d",&n,&A,&B);
115         if(n==0&&A==0&&B==0) break;
116         bool ok=1;
117         int sc=0;
118         for(int i=1;i<=n;i++)
119         {
120             scanf("%s",s);
121             for(int j=0;j<n;j++)
122             {
123                 if(s[j]==‘/‘) map[i][j+1]=0;
124                 else if(s[j]==‘.‘) map[i][j+1]=1;
125                 else map[i][j+1]=2,sc++;
126             }
127         }
128         for(int i=1;i<=n;i++)
129         {
130             int s1=0,s2=0;
131             for(int j=1;j<=n;j++)
132             {
133                 if(map[i][j]==2) s1++;
134                 if(map[j][i]==2) s2++;
135             }
136             if(s1!=s2||s1*B>sc*A||s2*B>sc*A) {ok=0;break;}
137         }
138         printf("Case %d: ",++kase);
139         int maxx=-1;
140         if(ok) maxx=sc;
141         for(int i=1;i<=n;i++)
142         {
143             maxx=mymax(maxx,get_ans(i));
144
145         }
146         if(maxx!=-1) maxx-=sc;
147         if(maxx==-1) printf("impossible\n");
148         else printf("%d\n",maxx);
149     }
150     return 0;
151 }

[LA5131]

2016-06-04 13:27:29

时间: 2024-10-13 16:55:13

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

BZOJ 2055 80人环游世界 有上下界的费用流

题目大意:给定n个点,每个点有固定的经过次数,m个人从任意节点出发任意节点结束,只能向右走,要求总边权和最小 有源汇.有上下界的费用流 其实上下界费用流有两种写法- - 一种是按照上下界网络流那么转化- - 一种是把必经边的费用减掉一个INF 跑完再加回去 我比较倾向于第一种写法- - 第二种写法在INF的取值上有点麻烦- - #include <cstdio> #include <cstring> #include <iostream> #include <al

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

ls是一个特别堕落的小朋友,对于n个连续的小时,他将要么睡觉要么打隔膜,一个小时内他不能既睡觉也打隔膜 ,因此一个小时内他只能选择睡觉或者打隔膜,当然他也必须选择睡觉或打隔膜,对于每一个小时,他选择睡觉或 打隔膜的愉悦值是不同的,对于第i个小时,睡觉的愉悦值为si,打隔膜的愉悦值为ei,同时又有一个奥妙重重的 规定:对于任意一段连续的k小时,ls必须至少有t1时间在睡觉,t2时间在打隔膜.那么ls想让他获得的愉悦值尽 量大,他该如何选择呢? 题意:就是N天,每天可以选择S或者E,每一天S或者E有

zoj 3229 有源汇有上下界的最大流模板题

/*坑啊,pe的程序在zoj上原来是wa. 题目大意:一个屌丝给m个女神拍照.计划拍照n天,每一天屌丝最多个C个女神拍照,每天拍照数不能超过D张,并且给每一个女神i拍照有数量限制[Li,Ri], 对于每一个女神n天的拍照总和不能超过Gi,假设有解求屌丝最多能拍多少张照,并求每天给相应女神拍多少张照:否则输出-1. 解题思路:增设一源点st,汇点sd.st到第i天连一条上界为Di下界为0的边,每一个女神到汇点连一条下界为Gi上界为oo的边,对于每一天,当天到第i个女孩连一条[Li.Ri]的边. 建

有上下界的最大流解法

问题模型: 给定一个加权的有向图,满足: (1)容量限制条件: (2)流量平衡条件: (2)中的即除了源汇外,所有点都满足流量平衡条件,则称G为有源汇网络:否则,即不存在源汇,所有点都满足流量平衡条件,则称G为无源汇网络. 将这类问题由易到难一一解决: 问题[1] 求无源汇的网络有上下界的可行流 由于下界是一条弧上的流必需要满足的确定值.下面引入必要弧的概念:必要弧是一定流要满的弧.必要弧的构造,将容量下界的限制分离开了,从而构造了一个没有下界的网络G': 1. 将原弧(u,v)分离出一条必要弧

POJ 2396 Budget (有源汇有上下界的可行流)

POJ 2396 Budget 链接:http://poj.org/problem?id=2396 题意:给定一个M*N的矩阵,给定每行每列的和,以及其中一些值的限定条件,问能否构成一个可行的矩阵. 思路: 添加一个源点,向每行连边,每条边的上下界都为该行的和:添加一个汇点,每列向汇点连边,边的上下界都为该列的和.然后每行向每列连边,边的上下界一开始为(0,INF),之后通过一些限定条件更新. 现在问题成了求一个有源汇有上下界的可行流.只需要再添加一个超级源点,一个超级汇点,并且将原图的汇点向源

[ACdream 1211 Reactor Cooling]无源无汇有上下界的可行流

题意:无源无汇有上下界的可行流 模型 思路:首先将所有边的容量设为上界减去下界,然后对一个点i,设i的所有入边的下界和为to[i],所有出边的下界和为from[i],令它们的差为dif[i]=to[i]-from[i],根据流量平衡原理,让出边和入边的下界相抵消,如果dif[i]>0,说明入边把出边的下界抵消了,还剩下dif[i]的流量必须要流过来(否则不满足入边的下界条件),这时从源点向i连一条容量为dif[i]的边来表示即可,如果dif[i]<0,同理应该从i向汇点连一条容量为-dif[i

ZOJ 2314 带上下界的可行流

对于无源汇问题,方法有两种. 1 从边的角度来处理. 新建超级源汇, 对于每一条有下界的边,x->y, 建立有向边 超级源->y ,容量为x->y下界,建立有向边 x-> 超级汇,容量为x->y下界.建立有向边 x->y,容量为x->y的上界减下界. 2 从点的角度来处理. 新建超级源汇,对于每个点流进的下界和为 in, 流出此点的下界和为out.如果in > out. 建立有向边 超级源->i,容量为in-out.反之,建立有向边 i->超级汇

2014多校7(1006)hdu4940(有上下界的最大流)

Destroy Transportation system Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 114    Accepted Submission(s): 83 Problem Description Tom is a commander, his task is destroying his enemy's tran

ZOJ 3229 Shoot the Bullet(有源汇有上下界的最大流)

ZOJ 3229 Shoot the Bullet 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229 题意:一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝最多给C个女神拍照,每天拍照数不能超过D张,而且给每个女神 i 拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能超过Gi,如果有解求屌丝最多能拍多少张照,并求每天给对应女神拍多少张照:否则输出-1. 思路: 有源汇有上下界的最大流 1. 在原先