ZOJ 2887 Server Relocation(BFS)
来源:互联网 发布:java测试类 main方法 编辑:程序博客网 时间:2024/06/05 08:47
刚开始建图,1000 太大了,数据可能比较多,超时了.
后来就不建图了,BFS的时候算距离就可以了.
比较坑爹的是 坐标要用double存,说好的integers呢.
#include <iostream>#include <cmath>#include <cstdio>#include <vector>#include <queue>#include <memory.h>using namespace std;const int maxn = 1005;double x[maxn], y[maxn];int s, e, n;bool vis[maxn];double len1, len2;double dis(double x1, double x2, double y1, double y2){return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));}int bfs(){memset(vis, 0, sizeof(vis));queue<pair<int,int> > q;q.push(make_pair(s, 0));vis[s] = 1;while (q.size()){pair<int,int> t = q.front();q.pop();int u = t.first, tms = t.second;if(u == e)return tms;for (int i = 0; i < n; ++i){if(!vis[i] && dis(x[u], x[i], y[u], y[i]) <= len1 + len2){if(i == e)return tms + 1;vis[i] = 1;q.push(make_pair(i, tms + 1));}}}return -1;}int main(){int t;scanf("%d", &t);while (t--){int t2 = 0;scanf("%d", & t2);while (t2--){scanf("%d %d %d %lf %lf", &n, &s, &e, &len1, &len2);s--, e--;for (int i = 0; i < n; ++i){scanf("%lf %lf", &x[i], &y[i]);}int ans = bfs();if(ans == -1){printf("Impossible\n");}else{printf("%d\n", ans);}}}return 0;}
- ZOJ 2887 Server Relocation(BFS)
- zoj 2887 || poj 3439 Server Relocation
- POJ3439 Server Relocation bfs
- poj 3439 Server Relocation 最短路bfs
- POJ - 2923 Relocation(BFS+状态压缩)
- Relocation
- ZOJ 1649 Rescue (BFS)
- ZOJ 1005 Jugs 【BFS】
- zoj 1438 Asteroids! BFS
- zoj 1649 rescue (bfs)
- zoj 1050 bfs
- zoj 1091 bfs
- zoj 1649 BFS
- zoj 2913 BFS
- ZOJ 1136 Multiple(BFS)
- ZOJ 1675 Push!!(BFS)
- ZOJ 1649 BFS
- ZOJ 2913 BFS
- UVa 10739 - String to Palindrome
- 高薪面试题01—银行业务调度系统
- 优秀电影推荐
- zoj 2838 Utopia
- Java实现基本排序算法
- ZOJ 2887 Server Relocation(BFS)
- s:property用法
- 获取webbrowser的useragent
- DeepLearning(深度学习)原理与实现(二)
- 每天一个linux命令(37):date命令
- debian安装mongodb
- 2013蓝桥杯预赛javaA 世纪末的星期
- 关于Linux下的强制锁
- HDU3294:Girls' research