忘记优先队列初始是大根堆了。。。。
调了半天。。。
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<ctime> 5 #include<cstdlib> 6 #include<iostream> 7 #include<algorithm> 8 #include<queue> 9 #include<vector> 10 #define clr(a,x) memset(a,x,sizeof(a)) 11 #define rep(i,l,r) for(int i=l;i<r;i++) 12 typedef long long ll; 13 using namespace std; 14 int read() 15 { 16 char c=getchar(); 17 int ans=0,f=1; 18 while(!isdigit(c)){ 19 if(c==‘-‘) f=-1; 20 c=getchar(); 21 } 22 while(isdigit(c)){ 23 ans=ans*10+c-‘0‘; 24 c=getchar(); 25 } 26 return ans*f; 27 } 28 struct node{ 29 int d,num; 30 inline bool operator <(const node&A)const{ 31 return d>A.d; 32 } 33 }; 34 struct node1{ 35 int x,y,p; 36 inline bool operator <(const node1&A)const{ 37 return x<A.x; 38 } 39 }; 40 struct node2{ 41 int x,y,p; 42 inline bool operator <(const node2&A)const{ 43 return y<A.y; 44 } 45 }; 46 struct edge{ 47 int v,to; 48 }; 49 const int maxn=200009,inf=1000000009; 50 int n; 51 priority_queue<node>q; 52 vector<edge>e[maxn]; 53 bool p[maxn]; 54 int d[maxn]; 55 node1 a[maxn]; 56 node2 b[maxn]; 57 void dijkstra() 58 { 59 clr(p,0); 60 node start; 61 start.d=0,start.num=1; 62 q.push(start); 63 rep(i,1,n+1) d[i]=inf; 64 d[1]=0; 65 while(!q.empty()){ 66 node now=q.top(); 67 q.pop(); 68 if(!p[now.num]){ 69 p[now.num]=1; 70 rep(i,0,e[now.num].size()){ 71 if(now.d+e[now.num][i].v<d[e[now.num][i].to]){ 72 d[e[now.num][i].to]=now.d+e[now.num][i].v; 73 node next; 74 next.d=d[e[now.num][i].to],next.num=e[now.num][i].to; 75 q.push(next); 76 } 77 } 78 } 79 } 80 } 81 int main() 82 { 83 clr(d,0),clr(a,0); 84 n=read(); 85 rep(i,1,n+1){ 86 a[i].x=b[i].x=read(),a[i].y=b[i].y=read(); 87 a[i].p=b[i].p=i; 88 } 89 sort(a+1,a+n+1); 90 sort(b+1,b+n+1); 91 rep(i,1,n){ 92 edge ed; 93 ed.v=a[i+1].x-a[i].x,ed.to=a[i+1].p; 94 e[a[i].p].push_back(ed); 95 ed.to=a[i].p; 96 e[a[i+1].p].push_back(ed); 97 ed.v=b[i+1].y-b[i].y,ed.to=b[i+1].p; 98 e[b[i].p].push_back(ed); 99 ed.to=b[i].p; 100 e[b[i+1].p].push_back(ed); 101 } 102 dijkstra(); 103 printf("%d\n",d[n]); 104 return 0; 105 }
4152: [AMPPZ2014]The Captain
Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 283 Solved: 106
[Submit][Status][Discuss]
Description
给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。
Input
第一行包含一个正整数n(2<=n<=200000),表示点数。
接下来n行,每行包含两个整数x[i],y[i](0<=x[i],y[i]<=10^9),依次表示每个点的坐标。
Output
一个整数,即最小费用。
Sample Input
5
2 2
1 1
4 5
7 1
6 7
Sample Output
2
HINT
Source
时间: 2024-11-06 03:49:23