这么写为什么会错
查看原帖
这么写为什么会错
530915
翟翟楼主2023/7/13 18:33
#include<bits/stdc++.h>
namespace IO{
	static const int buf_size=1000000;
	static unsigned char buf[buf_size];
	static int buf_len,buf_pos;
	bool isEOF(){
		if(buf_pos==buf_len){
			buf_pos=0;
			buf_len=fread(buf,1,buf_size,stdin);
			if(buf_pos==buf_len)return 1;
		}
		return 0;
	}
	inline char gc(){
		return isEOF()?EOF:buf[buf_pos++];
	}
	inline int rint(){
		int x=0;
		char c=gc();
		for(;c<48||c>57;c=gc());
		for(;c>=48&&c<=57;c=gc())
			x=(x<<1)+(x<<3)+(c^48);
		return x;
	}
	template<typename T>
	void read(T&x){x=rint();}
	template<typename T,typename...Ts>
	inline void read(T&x,Ts&...rest){
		read(x);
		read(rest...);
	}
}
using IO::read;
using namespace std;
int r,c,a[501][500],b[500][501];
bool vis[501][501];
struct node{
	int x,y,s;
	bool operator<(const node&x)const{
		return s>x.s;
	}
	node(int _x,int _y,int _s){
		x=_x;
		y=_y;
		s=_s;
	}
};
priority_queue<node>q;
int main(){
	read(r,c);
	for(int i=1;i<=r;++i){
		for(int j=1;j<c;++j){
			read(a[i][j]);
		}
	}
	for(int i=1;i<r;++i){
		for(int j=1;j<=c;++j){
			read(b[i][j]);
		}
	}
	q.push(node(1,1,0));
	vis[1][1]=1;
	for(;!q.empty();){
		node p=q.top();
		q.pop();
		int i=p.x,j=p.y;
		if(i==r&&j==c){
			printf("%d\n",p.s);
			return 0;
		}
		if(j<c&&!vis[i][j+1]){
			q.push(node(i,j+1,p.s+a[i][j]));
			vis[i][j+1]=1;
		}
		if(j>1&&!vis[i][j-1]){
			q.push(node(i,j-1,p.s+a[i][j-1]));
			vis[i][j-1]=1;
		}
		if(i<r&&!vis[i+1][j]){
			q.push(node(i+1,j,p.s+b[i][j]));
			vis[i+1][j]=1;
		}
		for(int k=1;k<i;++k)
			if(!vis[i-k][j]){
				q.push(node(i-k,j,p.s+1+k));
				vis[i-k][j]=1;
			}
	}
	return 0;
}
2023/7/13 18:33
加载中...