1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include<stdio.h> #include<algorithm> #include<queue> #define pli pair<long long,int> using namespace std; vector<int>E[101000], L[101000]; struct point{ int x, y, num, dir; bool operator <(const point &p)const{ return x!=p.x?x<p.x:y<p.y; } }w[101000], ori[101000]; int n; long long T, D[101000]; priority_queue<pli>PQ; void Ins(int a, long long d){ if(D[a]<=d)return; D[a]=d; PQ.push(pli(-d,a)); } void Dijk(){ int i, a; for(i=1;i<=n;i++)D[i] = 2e18; Ins(1,0); pli tp; while(1){ while(!PQ.empty()){ tp = PQ.top(); if(D[tp.second] == -tp.first)break; PQ.pop(); } if(PQ.empty())break; tp = PQ.top(); a = tp.second; PQ.pop(); for(i=0;i<E[a].size();i++){ Ins(E[a][i],D[a]+L[a][i]); } } } void Add(int a, int b, int c){ E[a].push_back(b); L[a].push_back(c); } int main(){ int i, j, e, pv, y; char pp[3]; scanf("%d%lld",&n,&T); for(i=1;i<=n;i++){ scanf("%d%d",&w[i].x,&w[i].y); scanf("%s",pp); if(pp[0]=='U')w[i].dir = 1; if(pp[0]=='D')w[i].dir = 2; if(pp[0]=='R')w[i].dir = 3; if(pp[0]=='L')w[i].dir = 4; w[i].num = i; ori[i] = w[i]; } sort(w+1,w+n+1); for(i=1;i<=n;i++){ for(j=i;j<=n;j++)if(w[j].x!=w[i].x)break; e=j-1; pv = 0; for(j=i;j<=e;j++){ if(pv)Add(pv,w[j].num,w[j].y-y); if(w[j].dir == 1)pv = w[j].num, y = w[j].y; } pv = 0; for(j=e;j>=i;j--){ if(pv)Add(pv,w[j].num,y-w[j].y); if(w[j].dir == 2)pv = w[j].num, y = w[j].y; } i=e; } for(i=1;i<=n;i++)swap(w[i].x,w[i].y); sort(w+1,w+n+1); for(i=1;i<=n;i++){ for(j=i;j<=n;j++)if(w[j].x!=w[i].x)break; e=j-1; pv = 0; for(j=i;j<=e;j++){ if(pv)Add(pv,w[j].num,w[j].y-y); if(w[j].dir == 3)pv = w[j].num, y = w[j].y; } pv = 0; for(j=e;j>=i;j--){ if(pv)Add(pv,w[j].num,y-w[j].y); if(w[j].dir == 4)pv = w[j].num, y = w[j].y; } i=e; } Dijk(); long long xx,yy; for(i=1;i<=n;i++){ xx=ori[i].x,yy=ori[i].y; if(D[i]<=T){ if(ori[i].dir==1)yy+=T-D[i]; if(ori[i].dir==2)yy-=T-D[i]; if(ori[i].dir==3)xx+=T-D[i]; if(ori[i].dir==4)xx-=T-D[i]; } printf("%lld %lld\n",xx,yy); } }
|