Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9130 | Accepted: 4513 |
Description
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
Output
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
Source
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
int str[1001][1010];
int Map[1010][1010];
int match[1010],vis[1010];
int n,m,num;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int dfs(int u)
{
for(int i=1;i<=num;i++)
if(!vis[i]&&Map[u][i])
{
vis[i]=1;
if(!match[i]||dfs(match[i]))
{
match[i]=u;
return 1;
}
}
return 0;
}
int main()
{
int t;
cin>>t;
while(t--)
{
num=0;
int sum=0;
memset(str,0,sizeof(str));
memset(Map,0,sizeof(Map));
memset(match,0,sizeof(match));
char ch;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>ch;
if(ch=='*')
str[i][j]=++num;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(str[i][j])
{
for(int k=0;k<4;k++)
{
int x=i+dx[k];
int y=j+dy[k];
if(str[x][y])
{
Map[str[i][j]][str[x][y]]=1;
}
}
}
}
for(int i=1;i<=num;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
sum++;
}
printf("%d\n",num-sum/2);
}
}
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
int w[405][405];
int vis[555],s[445];
char Map[45][45];
int v[45][15];
int tt;
int dfs(int a)
{
int i;
for(i=1;i<tt;i++)
{
if(w[a][i]&&!vis[i])
{
vis[i]=1;
if(s[i]==0||dfs(s[i]))
{
s[i]=a;
return 1;
}
}
}
return 0;
}
int main()
{
int t;
int n,m;
int i,j;
cin>>t;
while(t--)
{
memset(w,0,sizeof(w));
cin>>n>>m;
tt=1;
for(i=0;i<n;i++)
{
cin>>Map[i];
for(j=0;j<m;j++)
if(Map[i][j]=='o')
v[i][j]=0;
else
v[i][j]=tt++;
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(v[i-1][j]!=0&&i>=1)
w[v[i][j]][v[i-1][j]]=1;
if(v[i+1][j]!=0&&i+1<n)
w[v[i][j]][v[i+1][j]]=1;
if(v[i][j-1]!=0&&j>=1)
w[v[i][j]][v[i][j-1]]=1;
if(v[i][j+1]!=0&&j+1<m)
w[v[i][j]][v[i][j+1]]=1;
}
int ans=0;
memset(s,0,sizeof(s));
for(i=1;i<tt;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
ans++;
}
printf("%d\n",tt-1-ans/2);
}
}