bzoj4152: [AMPPZ2014]The Captain
水。。。
这个建边排序一下从一边连向一边
dij在这种稀疏图果然不够优秀啊。只是学了一发。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> using namespace std; struct node { int x,y,d,next; }a[810000];int len,last[210000]; void ins(int x,int y,int d) { len++; a[len].x=x;a[len].y=y;a[len].d=d; a[len].next=last[x];last[x]=len; } struct point { int x,y,id; }p[210000]; bool cmp1(point p1,point p2){return p1.x<p2.x;} bool cmp2(point p1,point p2){return p1.y<p2.y;} int d[210000]; struct dij { int x; friend bool operator <(dij n1,dij n2){return d[n1.x]>d[n2.x];} };priority_queue<dij>q; bool v[210000]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y),p[i].id=i; len=0;memset(last,0,sizeof(last)); sort(p+1,p+n+1,cmp1); for(int i=1;i<n;i++) ins(p[i].id,p[i+1].id,p[i+1].x-p[i].x), ins(p[i+1].id,p[i].id,p[i+1].x-p[i].x); sort(p+1,p+n+1,cmp2); for(int i=1;i<n;i++) ins(p[i].id,p[i+1].id,p[i+1].y-p[i].y), ins(p[i+1].id,p[i].id,p[i+1].y-p[i].y); memset(d,63,sizeof(d));d[1]=0; memset(v,false,sizeof(v));v[1]=true; dij tt;tt.x=1;q.push(tt); while(q.empty()==false) { int x=q.top().x;q.pop(); for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(d[y]>d[x]+a[k].d) { d[y]=d[x]+a[k].d; if(v[y]==false) { v[y]=true; tt.x=y;q.push(tt); } } } v[x]=false; } printf("%d\n",d[n]); return 0; }
pain and happy in the cruel world.