zoukankan      html  css  js  c++  java
  • HDU2732(KB11-K 最大流)

    Leapin' Lizards

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3050    Accepted Submission(s): 1251


    Problem Description

    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.
     

    Input

    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.
     

    Output

    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.
     

     

    Sample Input

    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.. ........ ........
     

    Sample Output

    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.
     

    Source

     
    点上有权,显然要拆点。
    源点与蜥蜴连边
    蜥蜴向所在柱子入点连边,容量为1。
    从柱子跳出边界,与汇点连边
    从柱子跳到下一根柱子,本跳的出点与下跳的入点连边
      1 //2017-08-25
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <iostream>
      5 #include <algorithm>
      6 #include <queue>
      7 #include <cmath>
      8 
      9 using namespace std;
     10 
     11 const int N = 2000;
     12 const int M = 1000000;
     13 const int INF = 0x3f3f3f3f;
     14 int head[N], tot;
     15 struct Edge{
     16     int next, to, w;
     17 }edge[M];
     18 
     19 void add_edge(int u, int v, int w){
     20     edge[tot].w = w;
     21     edge[tot].to = v;
     22     edge[tot].next = head[u];
     23     head[u] = tot++;
     24 
     25     edge[tot].w = 0;
     26     edge[tot].to = u;
     27     edge[tot].next = head[v];
     28     head[v] = tot++;
     29 }
     30 
     31 struct Dinic{
     32     int level[N], S, T;
     33     void init(int _S, int _T){
     34         S = _S;
     35         T = _T;
     36         tot = 0;
     37         memset(head, -1, sizeof(head));
     38     }
     39     bool bfs(){
     40         queue<int> que;
     41         memset(level, -1, sizeof(level));
     42         level[S] = 0;
     43         que.push(S);
     44         while(!que.empty()){
     45             int u = que.front();
     46             que.pop();
     47             for(int i = head[u]; i != -1; i = edge[i].next){
     48                 int v = edge[i].to;
     49                 int w = edge[i].w;
     50                 if(level[v] == -1 && w > 0){
     51                     level[v] = level[u]+1;
     52                     que.push(v);
     53                 }
     54             }
     55         }
     56         return level[T] != -1;
     57     }
     58     int dfs(int u, int flow){
     59         if(u == T)return flow;
     60         int ans = 0, fw;
     61         for(int i = head[u]; i != -1; i = edge[i].next){
     62             int v = edge[i].to, w = edge[i].w;
     63             if(!w || level[v] != level[u]+1)
     64               continue;
     65             fw = dfs(v, min(flow-ans, w));
     66             ans += fw;
     67             edge[i].w -= fw;
     68             edge[i^1].w += fw;
     69             if(ans == flow)return ans;
     70         }
     71         if(ans == 0)level[u] = 0;
     72         return ans;
     73     }
     74     int maxflow(){
     75         int flow = 0;
     76         while(bfs())
     77           flow += dfs(S, INF);
     78         return flow;    
     79     }
     80 }dinic;
     81 
     82 int T, n, m, d;
     83 string G1[30], G2[30];
     84 
     85 int getId(int x, int y, int op){
     86     if(op == 0)
     87           return x*m+y+1;//柱子入点编号
     88     else if(op == 1)
     89           return n*m+x*m+y+1;//柱子出点编号
     90     else
     91           return 2*n*m+x*m+y+1;//蜥蜴编号
     92 }
     93 
     94 int main()
     95 {
     96     std::ios::sync_with_stdio(false);
     97     //freopen("inputK.txt", "r", stdin);
     98     cin>>T;
     99     int kase = 0;
    100     while(T--){
    101         cin>>n>>d;
    102         for(int i = 0; i < n; i++)
    103               cin>>G1[i];
    104         for(int i = 0; i < n; i++)
    105               cin>>G2[i];
    106         m = G1[0].length();
    107         int s = 0, t = 3*n*m+1;
    108         dinic.init(s, t);
    109         for(int i = 0; i < n; i++){
    110             for(int j = 0; j < m; j++){
    111                 if(G1[i][j] != '0'){
    112                     add_edge(getId(i, j, 0), getId(i, j, 1), G1[i][j]-'0');
    113                     for(int dx = -d; dx <= d; dx++){
    114                         for(int dy = -d; dy <= d; dy++){
    115                             if(!dx && !dy)continue;
    116                             int nx = i + dx;
    117                             int ny = j + dy;
    118                             if(abs(dx)+abs(dy) > d)continue;
    119                             if(nx<0 || nx>=n || ny<0 || ny>=m){
    120                                 add_edge(getId(i, j, 1), t, INF);//跳出边界,与汇点连边
    121                                 continue;
    122                             }
    123                             if(G1[nx][ny]!='0'){
    124                                 add_edge(getId(i, j, 1), getId(nx, ny, 0), INF);//跳到下一根柱子,本跳出点与下跳入点连边
    125                             }
    126                         }
    127                     }
    128                 }
    129             }
    130         }
    131         int cnt = 0;
    132         for(int i = 0; i < n; i++){
    133             for(int j = 0; j < m; j++){
    134                 if(G2[i][j] == 'L'){
    135                     cnt++;
    136                     add_edge(s, getId(i, j, 2), INF);
    137                     add_edge(getId(i, j, 2), getId(i, j, 0), 1);//蜥蜴向所在柱子入点连边,容量为1,INF则WA
    138                 }
    139             }
    140         }
    141         int ans = dinic.maxflow();
    142         if(cnt-ans > 1)
    143               cout<<"Case #"<<++kase<<": "<<cnt-ans<<" lizards were left behind."<<endl;
    144         else if(cnt-ans == 1)
    145               cout<<"Case #"<<++kase<<": "<<cnt-ans<<" lizard was left behind."<<endl;
    146         else
    147               cout<<"Case #"<<++kase<<": no lizard was left behind."<<endl;
    148 
    149     }
    150     return 0;
    151 }
  • 相关阅读:
    cocos2dx 3.x(获取当前系统时间)
    cocos2dx 3.x(加载cocostudio进度条)
    cocos2dx 3.x(Button传统按钮)
    cocos2dx 3.x以上(Sprite精灵类的相关属性与创建)
    cocos2dx 3.x版本搭建Mac环境工程(创建一个新的C++工程)百分百可行
    iOS
    iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)
    iOS -Swift 3.0 -String(字符串常规用法)
    iOS -Swift 3.0 -UIButton属性大全
    iOS -Swift 3.0 -UILabel属性大全
  • 原文地址:https://www.cnblogs.com/Penn000/p/7429035.html
Copyright © 2011-2022 走看看