二分圖(行列變化),二分圖行列變化

來源:互聯網
上載者:User

二分圖(行列變化),二分圖行列變化

HDU 4185 :

Problem DescriptionThanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water. 
InputThe input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell. 
OutputFor each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted. 
Sample Input
16.......##....##.......#.....##......
 
Sample Output
Case 1: 3
 求圖中"**"(豎著也可以)。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int maxn=600+10;char s[maxn][maxn];int mp[maxn][maxn],id[maxn][maxn];int used[maxn],link[maxn];int dr[][2]={-1,0,1,0,0,-1,0,1};int t,n,cnt,cas;bool dfs(int x){    REP(i,cnt)    {        if(mp[x][i]&&!used[i])        {            used[i]=true;            if(link[i]==-1||dfs(link[i]))            {                link[i]=x;                return true;            }        }    }    return false;}void work(){    int res=0;    CLEAR(link,-1);    REP(i,cnt)    {        CLEAR(used,0);        if(dfs(i)) res++;    }    printf("Case %d: %d\n",cas++,res/2);}bool ok(int x,int y){    if(x<0||x>=n||y<0||y>=n)  return false;    return  true;}int main(){    cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        cnt=1;        CLEAR(id,0);        REP(i,n)        {            scanf("%s",s[i]);            REP(j,n)              if(s[i][j]=='#') id[i][j]=cnt++;//編號        }        CLEAR(mp,0);        REP(i,n)        {            REP(j,n)            {                if(id[i][j])                {                    REP(k,4)                    {                        int xx=i+dr[k][0];                        int yy=j+dr[k][1];                        if(ok(xx,yy)&&id[xx][yy])                            mp[id[i][j]][id[xx][yy]]=1;                    }                }            }        }        work();    }    return 0;}

POJ 3020:

Description

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

27 9ooo**oooo**oo*ooo*o*oo**o**ooooooooo*******ooo*o*oo*oo*******oo10 1***o******

Sample Output

175

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int maxn=500+10;char s[maxn][maxn];int mp[maxn][maxn],id[maxn][maxn];int used[maxn],link[maxn];int dr[][2]={-1,0,1,0,0,-1,0,1};int t,n,m,cnt,cas;bool dfs(int x){    REPF(i,1,cnt)    {        if(mp[x][i]&&!used[i])        {            used[i]=true;            if(link[i]==-1||dfs(link[i]))            {                link[i]=x;                return true;            }        }    }    return false;}void work(){    int res=0;    CLEAR(link,-1);    REPF(i,1,cnt-1)    {        CLEAR(used,0);        if(dfs(i)) res++;    }    printf("%d\n",cnt-1-res/2);}bool ok(int x,int y){    if(x<0||x>=n||y<0||y>=m)  return false;    return  true;}int main(){    cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        cnt=1;        CLEAR(id,0);        REP(i,n)        {            scanf("%s",s[i]);            REP(j,m)              if(s[i][j]=='*') id[i][j]=cnt++;//編號        }        CLEAR(mp,0);        REP(i,n)        {            REP(j,m)            {                if(id[i][j])                {                    REP(k,4)                    {                        int xx=i+dr[k][0];                        int yy=j+dr[k][1];                        if(ok(xx,yy)&&id[xx][yy])                            mp[id[i][j]][id[xx][yy]]=1;                    }                }            }        }        work();    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.