| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
好心人帮忙看看,为什么总TLE?//============================================================================
// Name : poj_3020.cpp
// Author : tiger
// Version :
// Copyright : 无聊中
// Description :
/*
* 建图:以'*'为顶点,四个方向相邻的'*'之间建边.
* 所求答案 = 建图求最大匹配 + 没有匹配数
*/
//============================================================================
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXN 401 //顶点数最多为h*w,即400
#define _clr(x) memset(x,0xff,sizeof(int)*MAXN)
char map[40][10];
int mat[401][401];//图中所有顶点数为h*w,最多为400
int match[MAXN];
int aug(int n,int mat[][MAXN],int* match,int* v,int now){
int i,ret=0;
v[now]=1;
for (i=0;i<n;i++)
if (!v[i]&&mat[now][i]){
if (match[i]<0)
match[now]=i,match[i]=now,ret=1;
else{
v[i]=1;
if (aug(n,mat,match,v,match[i]))
match[now]=i,match[i]=now,ret=1;
v[i]=0;
}
if (ret)
break;
}
v[now]=0;
return ret;
}
int graph_match(int n,int mat[][MAXN],int* match){
int v[MAXN],i,j;
for (i=0;i<n;i++)
v[i]=0,match[i]=-1;
for (i=0,j=n;i<n&&j>=2;)
if (match[i]<0&&aug(n,mat,match,v,i))
i=0,j-=2;
else
i++;
for (i=j=0;i<n;i++)
j+=(match[i]>=0);
return j/2;
}
int main() {
//freopen("3020.txt","r",stdin);
int i,j,n,h,w;//h为行数,w为列数
int sum;//记录‘*’的个数
int pipei;//匹配对数
scanf("%d",&n);
while( n --)
{
memset(map,'o',sizeof(map));//初始化map
memset(mat,0,sizeof(mat));//初始化mat
sum = 0;
scanf("%d%d",&h,&w);
for(i = 0; i < h; ++i)
{
scanf("%s",&map[i]);
/*for(j = 0; j < w; j ++)
{
cin>>map[i][j];
}*/
}
//以下为建图
for(i = 0; i < h; ++i)
{
for(j = 0; j < w; j ++)
{
if(map[i][j] == '*')
{
sum ++;//统计‘*’的个数
if(map[i][j + 1] == '*'){
mat[i*w + j][i*w + j + 1] = 1;
mat[i*w + j + 1][i*w + j] = 1;//无向图
}
if(map[i+1][j] == '*'){
mat[i*w + j][(i+1)*w + j] = 1;
mat[(i+1)*w + j][i*w + j] = 1;
}
}
}
}
/*for(i = 0; i < h*w; ++i)
{
for(j = 0; j < h*w; j ++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}*/
pipei = graph_match(h*w,mat,match);
printf("%d\n",sum - pipei );
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator