24h購物| | PChome| 登入
2013-04-16 10:52:30| 人氣1,223| 回應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,223) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][maxflow] 10092 - The Problem with the Problem Setter
此分類上一篇:[UVA][匈牙利匹配] 670 - The dog task

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文