ACM 的一道题提交总是Time Limit Exceeded,求救
我搞了一个下午,得出的结论是本人的确是菜鸟,请各位大虾帮下忙!!!题目:EscapeTimeLimit:2000/1000ms(Java/Other)MemoryLimi...
我搞了一个下午,得出的结论是本人的确是菜鸟,请各位大虾帮下忙!!!
题目:
Escape
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 0
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.
Input
More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
Output
Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.
Sample Input
1 1
1
1
2 2
1 0
1 0
1 1
Sample Output
YES
NO 展开
题目:
Escape
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 0
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.
Input
More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 0 for this person is not suitable for living in the ith planet.
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
Output
Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.
Sample Input
1 1
1
1
2 2
1 0
1 0
1 1
Sample Output
YES
NO 展开
1个回答
展开全部
你是怎么做的? 二分图的多重匹配这题,如果暴力做的话会T的
代码附一个吧:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100010;
int n,m;
int cap[11],map[N][11];
int val[11],mat[11][N];
int mark[11];
bool dfs(int x)
{
for(int i=0;i<m;i++)
{
if(!mark[i]&&map[x][i])
{
mark[i]=1;
if(val[i]<cap[i])
{
mat[i][val[i]++]=x;
return true;
}
else
for(int j=0;j<val[i];j++)
{
if(dfs(mat[i][j]))
{
mat[i][j]=x;
return true;
}
}
}
}
return false;
}
bool Yes()
{
int i;
memset(val,0,sizeof(val));
for(i=0;i<n;i++)
{
memset(mark,false,sizeof(mark));
if(!dfs(i)) return false;
}
return true;
}
int main()
{
while(cin>>n>>m)
{
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&map[i][j]);
}
}
for(int i=0;i<m;i++) scanf("%d",&cap[i]);
if(Yes())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
代码附一个吧:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=100010;
int n,m;
int cap[11],map[N][11];
int val[11],mat[11][N];
int mark[11];
bool dfs(int x)
{
for(int i=0;i<m;i++)
{
if(!mark[i]&&map[x][i])
{
mark[i]=1;
if(val[i]<cap[i])
{
mat[i][val[i]++]=x;
return true;
}
else
for(int j=0;j<val[i];j++)
{
if(dfs(mat[i][j]))
{
mat[i][j]=x;
return true;
}
}
}
}
return false;
}
bool Yes()
{
int i;
memset(val,0,sizeof(val));
for(i=0;i<n;i++)
{
memset(mark,false,sizeof(mark));
if(!dfs(i)) return false;
}
return true;
}
int main()
{
while(cin>>n>>m)
{
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&map[i][j]);
}
}
for(int i=0;i<m;i++) scanf("%d",&cap[i]);
if(Yes())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询