ZOJ 2887 Server Relocation(BFS)
2013-05-05 20:41
399 查看
刚开始建图,1000 太大了,数据可能比较多,超时了.
后来就不建图了,BFS的时候算距离就可以了.
比较坑爹的是 坐标要用double存,说好的integers呢.
后来就不建图了,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 1091 Knight Moves (BFS)
- ZOJ 2416题解BFS
- ZOJ 3620 Escape Time II(BFS)
- ACM-ZOJ 1649 BFS 广度优先搜索
- zoj 3820 Building Fire Stations(二分+bfs)
- ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS
- ZOJ 3820 Building Fire Stations 二分+BFS
- zoj 3820 Building Fire Stations The 2014 ACM-ICPC Asia Mudanjiang Regional Contest bfs
- ZOJ 1649 Rescue(BFS)
- poj2245 zoj 1089 bfs_水
- zoj 2165 Red and Black ——BFS入门题
- ZOJ 2416 Open the Lock (BFS)
- ZOJ 1649 Rescue(BFS)
- ZOJ 3675 Trim the Nails(状态压缩 + BFS)
- ZOJ 1091 BFS
- zoj 1649 rescue (bfs)
- zoj 1940 || poj 2251 Dungeon Master(bfs)
- ZOJ 2050BFS加位运算,这样节省了空间,由于4*4的空间太大状态数组存不下,看了别人的代码顿悟了!用二进制只要2^16-1=65535个数组便可存下。白色用状态1表示很色用0表示。
- ZOJ 3596 Digit Number【状态压缩】【BFS】
- ZOJ 3781 Paint the Grid Reloaded (缩点,bfs)