BZOJ 1295: [SCOI2009]最长距离( 最短路 )

把障碍点看做点(边)权为1, 其他为0. 对于每个点跑spfa, 然后和它距离在T以内的就可以更新答案 

------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<queue>
#include<cmath>
 
using namespace std;
 
#define id(x, y) ((x) * M + (y))
#define check(x, y) ((x) >= 0 && (x) < N && (y) >= 0 && (y) < M)
 
const int dir[4][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}};
const int maxn = 909;
 
struct edge {
int to;
edge* next;
} E[200000], *pt = E, *head[maxn];
 
void AddEdge(int u, int v) {
pt->to = v; pt->next = head[u]; head[u] = pt++;
}
 
int N, M, T, n, w[maxn], d[maxn];
deque<int> q;
bool inq[maxn];
double ans = 0;
 
double dist(int x, int y, int _x, int _y) {
return sqrt((x - _x) * (x - _x) + (y - _y) * (y - _y));
}
 
void calculate(int X, int Y) {
int s = id(X, Y);
for(int i = N * M; i--; )
d[i] = maxn, inq[i] = false;
d[s] = w[s]; inq[s] = true; q.push_front(s);
while(!q.empty()) {
int x = q.front(); q.pop_front(); inq[x] = false;
for(edge* e = head[x]; e; e = e->next) if(d[e->to] > d[x] + w[e->to]) {
d[e->to] = d[x] + w[e->to];
if(inq[e->to]) continue;
inq[e->to] = true;
(!q.empty() && d[q.front()] > d[e->to]) ? q.push_front(e->to) : q.push_back(e->to);
}
}
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++) 
if(d[id(i, j)] <= T) ans = max(ans, dist(i, j, X, Y));
}
 
void init() {
scanf("%d%d%d", &N, &M, &T);
for(int i = 0; i < N; i++) 
for(int j = 0; j < M; j++) {
char c = getchar();
for(; !isdigit(c); c = getchar());
w[id(i, j)] = c - '0';
}
}
 
int main() {
init();
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
for(int k = 0; k < 4; k++) {
int x = i + dir[k][0], y = j + dir[k][1];
if(check(x, y)) AddEdge(id(i, j), id(x, y));
}
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++) calculate(i, j);
printf("%.6lf\n", ans);
return 0;
}

------------------------------------------------------------------------

1295: [SCOI2009]最长距离

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1105  Solved: 590
[Submit][Status][Discuss]

Description

windy有一块矩形土地,被分为 N*M 块 1*1 的小格子。 有的格子含有障碍物。 如果从格子A可以走到格子B,那么两个格子的距离就为两个格子中心的欧几里德距离。 如果从格子A不可以走到格子B,就没有距离。 如果格子X和格子Y有公共边,并且X和Y均不含有障碍物,就可以从X走到Y。 如果windy可以移走T块障碍物,求所有格子间的最大距离。 保证移走T块障碍物以后,至少有一个格子不含有障碍物。

Input

输入文件maxlength.in第一行包含三个整数,N M T。 接下来有N行,每行一个长度为M的字符串,'0'表示空格子,'1'表示该格子含有障碍物。

Output

输出文件maxlength.out包含一个浮点数,保留6位小数。

Sample Input

【输入样例一】
3 3 0
001
001
110


【输入样例二】
4 3 0
001
001
011
000


【输入样例三】
3 3 1
001
001
001

Sample Output

【输出样例一】
1.414214

【输出样例二】
3.605551

【输出样例三】
2.828427

HINT

20%的数据,满足 1 <= N,M <= 30 ; 0 <= T <= 0 。
40%的数据,满足 1 <= N,M <= 30 ; 0 <= T <= 2 。
100%的数据,满足 1 <= N,M <= 30 ; 0 <= T <= 30 。

Source

 

posted @ 2015-11-04 18:04  JSZX11556  阅读(297)  评论(0编辑  收藏  举报