[UVA][maxflow、黑白染色] 10349 - Antenna Placement@Morris' Blog|PChome Online 人新台
2013-04-16 10:52:30| 人1,261| 回0 | 上一篇 | 下一篇

[UVA][maxflow、黑白染色] 10349 - Antenna Placement

0 收藏 0 0 站台

Problem E

Antenna Placement 

Input: standard input

Output: standard output

Time Limit: 1 second

Memory Limit: 32 MB

 

The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It is called 4DAir, and comes in four types. Each type can only transmit and receive signals in a direction aligned with a (slightly skewed) latitudinal and longitudinal grid, because of the interacting electromagnetic field of the earth. The four types correspond to antennas operating in the directions north, west, south, and east, respectively. Below is an example picture of places of interest, depicted by twelve small rings, and nine 4DAir antennas depicted by ellipses covering them.

 

Obviously, it is desirable to use as few antennas as possible, but still provide coverage for each place of interest. We model the problem as follows: Let A be a rectangular matrix describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement in A such that all points of interest are covered?

 

Input

On the first row of input is a single positive integer n, specifying the number of scenarios that follow. Each scenario begins with a row containing two positive integers h and w, with 1<h<40 and 0<w<10.Thereafter is a matrix presented, describing the points of interest in Sweden in the form of h lines, each containing w characters from the set [‘*’,’o’]. A ‘*’-character symbolises a point of interest, whereas a ‘o’-character represents open space.

Output

For each scenario, output the minimum number of antennas necessary to cover all ‘*’-entries in the scenario’s matrix, on a row of its own.

 

Sample Input      

2                      

7 9                    

ooo**oooo

**oo*ooo*

o*oo**o**

ooooooooo

*******oo

o*o*oo*oo

*******oo

10 1

*

*

*

o

*

*

*

*

*

*

 

Sample Output 

17

5


Swedish National Contest.

 

黑白染色,如棋那。

很明地,相的要匹配的,那必然是不同色。

因此目得是要求二分最大匹配。

最後答案是 未匹配的 + 匹配 = 全部 - 匹配

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct Node {
    int x, y, v;// x->y, v
    int next;
} edge[500005];
int e, head[500], dis[500], prev[500], record[500];
void addEdge(int x, int y, int v) {
    edge[e].x = x, edge[e].y = y, edge[e].v = v;
    edge[e].next = head[x], head[x] = e++;
    edge[e].x = y, edge[e].y = x, edge[e].v = 0;
    edge[e].next = head[y], head[y] = e++;
}
int maxflow(int s, int t) {
    int flow = 0;
    int i, j, x, y;
    while(1) {
        memset(dis, 0, sizeof(dis));
        dis[s] =  0xffff; // oo
        queue<int> Q;
        Q.push(s);
        while(!Q.empty()) {
            x = Q.front();
            Q.pop();
            for(i = head[x]; i != -1; i = edge[i].next) {
                y = edge[i].y;
                if(dis[y] == 0 && edge[i].v > 0) {
                    prev[y] = x, record[y] = i;
                    dis[y] = min(dis[x], edge[i].v);
                    Q.push(y);
                }
            }
            if(dis[t])  break;
        }
        if(dis[t] == 0) break;
        flow += dis[t];
        for(x = t; x != s; x = prev[x]) {
            int ri = record[x];
            edge[ri].v -= dis[t];
            edge[ri^1].v += dis[t];
        }
    }
    return flow;
}
int main() {
    int t, n, m;
    int i, j;
    char g[105][105];
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &n,&m);
        for(i = 0; i < n; i++)
            scanf("%s", g[i]);
        int st = 0, ed = n*m+1;
        //g[i][j] = i*m+j+1
        e = 0;
        memset(head, -1, sizeof(head));
        int cntNode = 0;
        for(i = 0; i < n;  i++) {
            for(j = 0; j < m; j++) {
                if(g[i][j] == '*')
                    cntNode++;
                if((i+j)%2 && g[i][j] == '*') {
                    addEdge(st, i*m+j+1, 1);
                    if(i-1 >= 0 && g[i-1][j] == '*')
                        addEdge(i*m+j+1, (i-1)*m+j+1, 1);
                    if(j-1 >= 0 && g[i][j-1] == '*')
                        addEdge(i*m+j+1, i*m+j-1+1, 1);
                    if(i+1 < n && g[i+1][j] == '*')
                        addEdge(i*m+j+1, (i+1)*m+j+1, 1);
                    if(j+1 < m && g[i][j+1] == '*')
                        addEdge(i*m+j+1, i*m+j+1+1, 1);
                }
                if((i+j)%2 == 0 && g[i][j] == '*') {
                    addEdge(i*m+j+1, ed, 1);
                }
            }
        }
        int flow = maxflow(st, ed);
        printf("%d\n", (cntNode-2*flow)+flow);
    }
    return 0;
}

台: Morris
人(1,261) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][maxflow] 10092 - The Problem with the Problem Setter
此分上一篇:[UVA][匈牙利匹配] 670 - The dog task

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86