社区讨论

求助大佬! bfs90分,#5能算出正确答案,但是不开O2就超时

P1514[NOIP 2010 提高组] 引水入城参与者 6已保存回复 6

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
6 条
当前快照
1 份
快照标识符
@lob7c7n9
此快照首次捕获于
2023/10/29 16:21
2 年前
此快照最后确认于
2023/11/03 22:39
2 年前
查看原帖
CPP
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

int n, m, h[501][501], is[501];
vector<int> a[501];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};

struct node{
	int x, y;
};

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	while(ch > '9' || ch < '0')
	{
		if(ch == '-')
			f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}

inline void init()
{
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
			h[i][j] = read();	
}

void bfs(int x, int y)
{
	queue<node> q;
	q.push({x,y});
	int book[501][501] = {0};
	while(!q.empty())
	{
		node b = q.front();
		q.pop();
		if(book[b.x][b.y])
			continue;
		book[b.x][b.y] = 1;
		if(b.x == n)
			a[y].push_back(b.y);
		for(int i = 0; i < 4; ++i)
		{
			int xx = b.x+dx[i], yy = b.y+dy[i];
			if(xx > 0 && xx <= n && yy > 0 && yy <= m && h[b.x][b.y] > h[xx][yy])
				q.push({xx, yy});
		}
	}
}

inline void work()
{
	for(int i = 1; i <= m; ++i)
		for(int j = 0; j < a[i].size(); ++j)
			is[a[i][j]]++;
	int tot = 0;
	for(int i = 1; i <= m; ++i)
		if(!is[i])
			tot++;
	if(tot)
		cout << 0 << endl << tot << endl;
	else
	{
		tot = m;
		for(int i = 1; i <= m; ++i)
		{
			int flag = 1;
			for(int j = 0; j < a[i].size(); ++j)
				if(is[a[i][j]] <= 1)
				{
					flag = 0;
					break;
				}
			if(flag)
			{
				for(int j = 0; j < a[i].size(); ++j)
					is[a[i][j]]--;
				tot--;
			}
		}
		cout << 1 << endl << tot << endl;
	}
}

int main()
{
	init();
	for(int i = 1; i <= m; ++i)
		bfs(1,i);
	work();
	return 0;
}

 

回复

6 条回复,欢迎继续交流。

正在加载回复...