hdu 2732 && poj 2711 Leapin' Lizards(dinic && 拆点建图经典)
2015-08-12 19:58
441 查看
Leapin' Lizards
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1651 Accepted Submission(s): 678
[align=left]Problem Description[/align]
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room's floor suddenly disappears!
Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below... Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard.
A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after
each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.
[align=left]Input[/align]
The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of
rows in the map, followed by a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing
the number of jumps the pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an 'L' for every position where a lizard is on the pillar and a '.' for every empty pillar. There will never
be a lizard on a position where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance is
always 1 ≤ d ≤ 3.
[align=left]Output[/align]
For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.
[align=left]Sample Input[/align]
4 3 1 1111 1111 1111 LLLL LLLL LLLL 3 2 00000 01110 00000 ..... .LLL. ..... 3 1 00000 01110 00000 ..... .LLL. ..... 5 2 00000000 02000000 00321100 02000000 00000000 ........ ........ ..LLLL.. ........ ........
[align=left]Sample Output[/align]
Case #1: 2 lizards were left behind. Case #2: no lizard was left behind. Case #3: 3 lizards were left behind. Case #4: 1 lizard was left behind.
[align=left]Source[/align]
Mid-Central USA 2005
题目大意:给出两张地图,第一章地图代表的是每根柱子的高度,第二张地图代表的是每只蜥蜴所在的位置
每根柱子只能站一只蜥蜴,蜥蜴离开该柱子时,柱子的高度会下降一个单元,当柱子的高度为0时,该柱子将不可用
现在给出每只蜥蜴能跳跃的距离,问最少有多少只蜥蜴逃不出来
这题还是主要是建图,,对于我这种菜好难想,,,
解题思路:将柱子拆成2个点,权值为柱子的高度
将每只蜥蜴所在的位置和超级源点连接,权值为1
将能通到外界的柱子连接到超级汇点,权值为INF
如果柱子间的距离满足蜥蜴跳跃的距离,连接起来,权值为INF
这样图就完成了。
但是我按照这么建图完成后,提交超时,, 改完提交过了,但是我又改回来后又过了,,这,,,= =+
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define M 1100//这里110会RE #define inf 0x3f3f3f3f #define ABS(a) (a>0 ? a:(-(a))) int head[M],dis[M],n,m,d,s,t,cnt; char g1[M][M],g2[M][M]; struct node{ int u,v,next,w; }mp[M*M]; void add(int u,int v,int w){ mp[cnt].u=u; mp[cnt].v=v; mp[cnt].w=w; mp[cnt].next=head[u]; head[u]=cnt++; mp[cnt].u=v; mp[cnt].v=u; mp[cnt].w=0; mp[cnt].next=head[v]; head[v]=cnt++; } int bfs(){ memset(dis,-1,sizeof(dis)); queue<int> q; dis[s]=0; q.push(s); while(!q.empty()){ int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=mp[i].next){ int v=mp[i].v; if(dis[v]==-1 && mp[i].w){ dis[v]=dis[u]+1; if(v==t) return 1; q.push(v); } } } return 0; } int dinic(int x,int low){ if(x==t) return low; int a,i,ans=0; for(int i=head[x];i!=-1;i=mp[i].next){ int v=mp[i].v; if(mp[i].w && dis[v]==dis[x]+1 && (a=dinic(v,min(low,mp[i].w)))>0){ mp[i].w-=a; mp[i^1].w+=a; ans+=a; if(low==ans) break; } } return ans; } int main(){ int T,i,j,k,l,cas=1; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&d); for(i=1;i<=n;i++) scanf("%s",g1[i]+1); for(i=1;i<=n;i++) scanf("%s",g2[i]+1); m=strlen(g1[1]+1); memset(head,-1,sizeof(head)); int num=0; cnt=s=0; t=n*m*2+1;//s作为超级源点,t为超级汇点 for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(g1[i][j]-'0'>0) add((i-1)*m+j,(i-1)*m+j+n*m,g1[i][j]-'0');//把有柱子的点拆开,(i-1)*m+j+n*m表示这个点拆开的另一个点,中间权值为柱子高度 //把点都编号从一开始,编号用坐标表示就是上边写的, for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(g2[i][j]=='L'){ add(s,(i-1)*m+j,1);//如果这个点上有蜥蜴那么和源点相连 num++; } for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(i<=d || j<=d || j>m-d || i>n-d) add((i-1)*m+j+n*m,t,inf);//如果这个点能够直接跳出去就和汇点相连权值为inf for(i=1;i<=n;i++) for(j=1;j<=m;j++) // if(g1[i][j]-'0'>0){ 开始的时候一直超时,加了这个判断后过了,,但是我又把这个判断注释了,也过了,,,好诡异!! for(k=1;k<=n;k++) for(l=1;l<=m;l++) if( !(i==k&&j==l) && d>=(ABS(i-k)+ABS(j-l)) ) add((i-1)*m+j+n*m,(k-1)*m+l,inf);//如果这个点可以跳到别的点上那么把这两个点也相连 // } int ans=0; while(bfs()){ ans+=dinic(0,inf); } ans=num-ans; printf("Case #%d: ",cas++); if(ans==0) printf("no lizard was left behind.\n"); else if(ans==1) printf("1 lizard was left behind.\n"); else printf("%d lizards were left behind.\n",ans); } return 0; }
相关文章推荐
- Solr(5.2.1)安装、启动和建立索引
- 线段树模板
- hdu 2819 Swap
- ObjC第三节:内存管理
- 杭电 1301 Jungle Roads 【最小生成树&&Kruskal】
- 【Baltic2007】【BZOJ1345】序列问题Sequence
- JAVA多线程编程
- Objective-C:在类中设置不同协议
- _00024 尼娜抹微笑伊拉克_云计算ClouderaManager以及CHD5.1.0群集部署安装文档V1.0
- 解决:LNMP架构下访问php页面出现500错误
- 问题-jdbc连接不上mysql,windows下开启两个mysql服务
- Win2008 R2 标准版升级企业版
- OC的nil, Nil, NSNull, NULL和nullptr. 以及josn转换后的(null)和<null>
- 【DP】 HDOJ 5370 Tree Maker
- ShareSDK的使用
- [HDU 2121] Ice_cream’s world II 最小树形图
- HDU 1116 Play on Words
- 快速幂。素数筛选。辗转相除。
- fd to filename
- hdu 1181 变形课 DFS