P4473 [国家集训队]飞飞侠

题面:https://www.luogu.org/problem/P4473

三遍dijkstra乱搞就过了。。。
Code:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N=155,inf=0x3f3f3f3f;
int n,m,b[N][N],x[4],y[4];
bool vis[N][N];
long long a[N][N],dis[N][N],ansd[4][4];
struct Node {
    int x,y;
	long long q;
    bool operator < (const Node &a) const {
        return q>a.q;
    }
};
void dijkstra(int opt){
    memset(dis,inf,sizeof(dis));
	memset(vis,0,sizeof(vis));
    priority_queue<Node> Q;
    Q.push((Node){x[opt],y[opt],0});
    dis[x[opt]][y[opt]]=0;
    while(!Q.empty()){
        Node u=Q.top();
        Q.pop();
		if(vis[u.x][u.y]){
			continue;
		}
		vis[u.x][u.y]=1;
        int len=b[u.x][u.y];
        for(int i=max(1,u.x-len);i<=min(n,u.x+len);++i){
            int tmp=len-abs(u.x-i);
            for(int j=max(1,u.y-tmp);j<=min(m,u.y+tmp);j++){
                if(dis[i][j]>dis[u.x][u.y]+a[u.x][u.y]){
                    dis[i][j]=dis[u.x][u.y]+a[u.x][u.y];
					if(!vis[i][j]){
						Q.push((Node){i,j,dis[i][j]});
					}
                }
            }
        }
    }
    for(int i=1;i<=3;++i){
		ansd[opt][i]=dis[x[i]][y[i]];
	}
}
int main(){
	scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j){
            scanf("%d",&b[i][j]);
		}
	}
    for(int i=1;i<=n;++i){
        for(int j=1;j<=m;++j){
            scanf("%lld",&a[i][j]);
		}
	}
    for(int i=1;i<=3;++i){
		scanf("%d%d",&x[i],&y[i]);
    }
    for(int i=1;i<=3;++i){
		dijkstra(i);
	}
    int id=0,ans=inf;
    for(int i=1;i<=3;++i){
		int sum=ansd[1][i]+ansd[2][i]+ansd[3][i];
        if(ans>sum){
			ans=sum;
			id=i;
		}
    }
    if(ans==inf){
		printf("NO\n");
		return 0;
	}
	if(id==1){
		printf("X\n");
	}
	if(id==2){
		printf("Y\n");
	}
   if(id==3){
		printf("Z\n");
	}
    printf("%d",ans);
    return 0;
}
posted @ 2019-10-19 23:26  prestige  阅读(133)  评论(0编辑  收藏  举报