bzoj4152 [AMPPZ2014]The Captain
最短路裸题
据说SPFA要T,用堆优dijkstra就好
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 const long long int inf=10000000000000000; 7 struct dui 8 { 9 long long int a;int b; 10 dui(long long int a=0,int b=0):a(a),b(b){} 11 }; 12 struct dian 13 { 14 int x,y,id; 15 }a[200020]; 16 int zhi[800080],next[800080],head[200020]; 17 long long int v[800080],dis[200020]; 18 int n,ed; 19 bool bo[200020]; 20 bool operator < (const dui &aa,const dui &bb)//重载<符号 21 { 22 return aa.a>bb.a; 23 } 24 priority_queue<dui>h; 25 bool com1(const dian &aa,const dian &bb) 26 { 27 return aa.x<bb.x; 28 } 29 bool com2(const dian &aa,const dian &bb) 30 { 31 return aa.y<bb.y; 32 } 33 void add(int x,int y,int z) 34 { 35 next[++ed]=head[x],head[x]=ed,zhi[ed]=y,v[ed]=z; 36 next[++ed]=head[y],head[y]=ed,zhi[ed]=x,v[ed]=z; 37 } 38 void hh() 39 { 40 for(int i=2;i<=n;i++)dis[i]=inf; 41 h.push(dui(0,1)); 42 while(!h.empty()) 43 { 44 int x=h.top().b;h.pop(); 45 if(bo[x])continue;bo[x]=1; 46 for(int i=head[x];i;i=next[i]) 47 if(dis[zhi[i]]>dis[x]+v[i]) 48 { 49 dis[zhi[i]]=dis[x]+v[i]; 50 h.push(dui(dis[zhi[i]],zhi[i])); 51 } 52 } 53 } 54 int getint() 55 { 56 int ret=0,f=1; 57 char ch=getchar(); 58 while(ch<'0'||ch>'9') 59 { 60 if(ch=='-')f=0; 61 ch=getchar(); 62 } 63 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 64 return f==0?-ret:ret; 65 } 66 int main() 67 { 68 n=getint(); 69 for(int i=1;i<=n;i++) 70 { 71 a[i].x=getint(),a[i].y=getint(); 72 a[i].id=i; 73 } 74 sort(a+1,a+n+1,com1); 75 for(int i=1;i<n;i++)add(a[i].id,a[i+1].id,a[i+1].x-a[i].x); 76 sort(a+1,a+n+1,com2); 77 for(int i=1;i<n;i++)add(a[i].id,a[i+1].id,a[i+1].y-a[i].y); 78 hh(); 79 printf("%lld\n",dis[n]); 80 return 0; 81 }