救救孩子!!!悬关!很急!
  • 板块灌水区
  • 楼主yjjh
  • 当前回复1
  • 已保存回复2
  • 发布时间2024/4/29 16:24
  • 上次更新2024/4/29 19:27:32
查看原帖
救救孩子!!!悬关!很急!
1036002
yjjh楼主2024/4/29 16:24

题目传送门(原题)

#include<bits/stdc++.h>
using namespace std;
const int N=503;
int h[N],e[N],w[N],ne[N],idx;
int n,m,dist[N];
bool st[N];
typedef pair<int,int> PII;
inline void add(int a,int b,int c){
    e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
inline int dijstra(){
    priority_queue<PII,vector<PII>,greater<PII> > heap;
    memset(dist,0x3f,sizeof dist);
    dist[1]=0;
    heap.push({0,1});
    while(heap.size()){
        PII t=heap.top();
        int v=t.second,d=t.first;
        heap.pop();
        if(st[v]) continue;
        st[v]=true;
        for(int i=h[v];i!=-1;i=ne[i]){
            int j=e[i];
            if(dist[j]>d+w[i]){
                dist[j]=d+w[i];
                heap.push({dist[j],j});
            }
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    memset(h,-1,sizeof h);
	for(int i=1;i<=n;i++){
    	for(int j=1;j<=m;j++){
    		char c;
    		cin >>c;
    		if(c=='/'){
    			int a=(i-1)*m+j,b=(i-1)*m+j+1,c=i*m+j,d=i*m+j+1;
    			add(b,c,0),add(c,d,0);
    			add(a,d,1),add(d,a,1);
			}
			else{
    			int a=(i-1)*m+j,b=(i-1)*m+j+1,c=i*m+j,d=i*m+j+1;
    			add(b,c,1),add(c,d,1);
    			add(a,d,0),add(d,a,0);
			}
		}
	}
    dijstra();
    if(dist[n]==0x3f3f3f3f) cout <<"NO SOLUTION";
    else cout <<dist[n];
    return 0;
}
2024/4/29 16:24
加载中...