Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 6978 | Accepted: 1592 |
Description
Input
Output
Sample Input
8 9 1 1 1 3 2 1 1 3 3 1 1 3 4 1 1 3 1 1 0 3 1 2 0 3 1 3 0 3 1 4 0 3 2 1 1 2 2 1 2 3 1 3 1 1 3 2 1 3 3 1 1 2 0 3 3 0 4 3 1 1.5 1.5 4 0 1 1 0 1 1 1 1 1 2 1 1 1 1 2 0 1 1.5 1.7 -1 -1
Sample Output
5 -1
Source
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include <cstring> #include <map> #include <string> #include <stack> #include <cctype> #include <vector> #include <queue> #include <set> using namespace std; //#define Online_Judge #define outstars cout << "***********************" << endl; #define clr(a,b) memset(a,b,sizeof(a)) #define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++) #define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++) #define REP(i , x , n) for(int i = (x) ; i > (n) ; i--) #define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--) const int MAXN = 200 + 5; const int maxw = 100 + 20; const int MAXNNODE = 1000000 +10; const long long LLMAX = 0x7fffffffffffffffLL; const long long LLMIN = 0x8000000000000000LL; const int INF = 9999999; const int IMIN = 0x80000000; #define eps 1e-8 #define mod 1000000007 typedef long long LL; const double PI = acos(-1.0); typedef double D; typedef pair<int , int> pi; struct node { int x , y , len; bool operator < (const node & a)const { return len > a.len; } }; int n , sx , m , sy ,xmax , ymax; int h[MAXN][MAXN] , l[MAXN][MAXN] , dis[MAXN][MAXN]; priority_queue <node> q; void bfs() { int x , y; FORR(x , 0 , xmax) { FORR(y , 0 , ymax) { dis[x][y] = INF; } } while(!q.empty())q.pop(); node pn; pn.x = 0; pn.y = 0; pn.len = 0; dis[0][0] = 0; q.push(pn); while(!q.empty()) { pn = q.top(); q.pop(); x = pn.x , y = pn.y; if(x == sx&&y == sy)return ; if(y + 1 <= ymax&&dis[x][y + 1] > dis[x][y] + h[x][y + 1]) { dis[x][y + 1] = dis[x][y] + h[x][y + 1]; pn.x = x ; pn.y = y + 1; pn.len = dis[x][y + 1]; q.push(pn); } if(y - 1 >= 0&&dis[x][y -1] > dis[x][y] + h[x][y]) { dis[x][y - 1] = dis[x][y] + h[x][y]; pn.x = x ; pn.y = y - 1; pn.len = dis[x][y - 1]; q.push(pn); } if(x - 1 >= 0&&dis[x - 1][y] > dis[x][y] + l[x][y]) { dis[x - 1][y] = dis[x][y] + l[x][y]; pn.x = x - 1 ; pn.y = y; pn.len = dis[x - 1][y]; q.push(pn); } if(x + 1 <= xmax&&dis[x + 1][y] > dis[x][y] + l[x + 1][y]) { dis[x + 1][y] = dis[x][y] + l[x + 1][y]; pn.x = x + 1 ; pn.y = y; pn.len = dis[x + 1][y]; q.push(pn); } } dis[sx][sy] = -1; } int main() { //ios::sync_with_stdio(false); #ifdef Online_Judge freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif // Online_Judge int x , y , d , t ; double tx , ty; while(scanf("%d%d" , &n , &m) == 2) { if(m == -1&&n == -1)break; xmax = ymax = -1; clr(h , 0), clr(l , 0); FOR(i , 0 , n) { scanf("%d%d%d%d" , &x , &y , &d , &t); if(d == 0) { FOR(j , 0 , t) { h[x + j][y] = INF; } xmax = max(xmax , x + t); ymax = max(ymax , y); } else { FOR(j , 0 ,t) { l[x][y + j] = INF; } xmax = max(xmax , x); ymax = max(ymax , y + t); } } FOR(i , 0 , m) { scanf("%d%d%d" , &x , &y ,&d); if(d == 0)h[x][y] = 1; else l[x][y] = 1; } scanf("%lf%lf" , &tx ,&ty); if(tx > xmax||ty > ymax) { printf("0\n"); } else { sx = (int)tx; sy = (int)ty; bfs(); printf("%d\n" , dis[sx][sy]); } } return 0; }