BZOJ1295: [SCOI2009]最长距离

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1295

转成最短路。

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<queue>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 105
#define ll long long
using namespace std;
struct data{int x,y;
};
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int n,m,t;
double ans;
int a[maxn][maxn],vis[maxn][maxn],d[maxn][maxn];
ll read(){
    ll x=0,f=1; char ch=getchar();
    while (!isdigit(ch)) {
        if (ch=='-') f=-1; ch=getchar();
    }
    while (isdigit(ch)){
        x=x*10+ch-'0'; ch=getchar();
    }
    return x*f;
}
bool jud(int x,int y){
    if (x<1||x>n||y<1||y>m||vis[x][y]) return 0;
    return 1;
}
int sqr(int x){
    return x*x;
}
double dis(int x,int y,int xx,int yy){
    return sqrt(sqr(x-xx)+sqr(y-yy));
}
void spfa(int x,int y){
    queue<data> q;
    clr(d,100); clr(vis,0);
    if (a[x][y]) d[x][y]=1; else d[x][y]=0;
    q.push((data){x,y});
    while (!q.empty()){
        data u=q.front(); q.pop(); vis[u.x][u.y]=1;
        rep(i,0,3){
            int vx=u.x+dx[i],vy=u.y+dy[i];
            if (jud(vx,vy)&&d[vx][vy]>d[u.x][u.y]+a[vx][vy]){
                d[vx][vy]=d[u.x][u.y]+a[vx][vy];
                if (d[vx][vy]<=t) ans=max(ans,dis(x,y,vx,vy));
                q.push((data){vx,vy});
            }
        }
        vis[u.x][u.y]=0;
    }
}
int main(){
    n=read(); m=read(); t=read();
    rep(i,1,n) rep(j,1,m){
        char ch=getchar(); while (!isdigit(ch)) ch=getchar();
        a[i][j]=ch-'0';
    }
    rep(i,1,n) rep(j,1,m){
        spfa(i,j);
    }
    printf("%.6lf\n",ans);
    return 0;
}

 

posted on 2015-12-08 21:30  ctlchild  阅读(152)  评论(0编辑  收藏  举报

导航