把这个题用迪杰斯特拉跑结果tle了
查看原帖
把这个题用迪杰斯特拉跑结果tle了
237893
donkeys楼主2021/12/1 12:38
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<queue>
#include<bitset>
#define N 1003
using namespace std;
int n, m;
int map[N][N];
int dis[N][N];
const int trans[4][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
struct Node
{
	int x, y, v;
	Node(int a, int b, int c) :x(a), y(b), v(c) {}
	friend bool operator<(Node a, Node b)
	{
		return a.v < b.v;
	}
	friend bool operator>(Node a, Node b)
	{
		return a.v > b.v;
	}
};
inline bool legal(int x, int y)
{
	if(x > n || x<1 || y>m || y < 1)
		return 0;
	return 1;
}
inline void read(int &x)
{
	x = 0; char c = getchar();
	while('0' > c || c > '9')
		c = getchar();
	while('0' <= c && c <= '9')
		x = (x << 3) + (x << 1) + c - '0', c = getchar();
}
priority_queue<Node, vector<Node>, greater<Node>>q;
bitset<N>vis[N];
void bfs()
{
	q.emplace(Node(1, 1, 0));
	int times = 0;
	while(!q.empty())
	{
		Node p = q.top(); q.pop();
		if(vis[p.x][p.y])
			continue;
		vis[p.x][p.y] = 1;
		dis[p.x][p.y] = p.v;
		for(int i = 0; i < 4; ++i)
		{
			times++;
			int tx = p.x + trans[i][0], ty = p.y + trans[i][1];
			if(!legal(tx, ty) || vis[tx][ty])
				continue;
			q.emplace(Node(tx, ty, max(p.v, map[tx][ty])));
		}
	}
	//cout << times<<'\n';
}
int main()
{
	read(n), read(m);
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
			read(map[i][j]);
	bfs();
	cout << dis[n][1];
	return 0;
}

快读也上了,在while循环里统计次数4e6,理论上t不掉啊(优先队列极限能跑1e8的数据)

2021/12/1 12:38
加载中...