BZOJ4152 [AMPPZ2014]The Captain[最短路]

你们都说这题简单可是我想了好久,可能还是太菜,泪拉了下来

由于这题网上全部是一句话题解,我并不能理解个中原因,向hkk神仙请教被以“很显然的做法”驳回。。神仙做题全都是显然吗?

最后自己yy出了一种新做法应该叫新的科学严谨的理解方法,不知道是不是一种正确思路

首先尝试优化建边,对于一个点对,他们连一条$\min(\Delta x,\Delta y)$的边,有没有什么办法用走另外一些边代替这个值?

如果以所有点构建一张网格图,由题意发现$x$相同的点在一竖列上随便移动不花费,相邻的跨过列有花费$\Delta x$,$y$同理。

这样,任意一条连边,$\min$里的$\Delta x$一定可以表示成连续跨过若干个列,每个列花费一个代价。$\Delta y$表示跨过若干行每跨一次花费总和。

这样,这个边就可以被标示,然后用题解通行的连边方法,发现同列or同行之间连$0$边,相邻行or列只要选一个代表点连边,这样走最短路。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #define dbg(x) cerr << #x << " = " << x <<endl
 8 using namespace std;
 9 typedef long long ll;
10 typedef double db;
11 typedef pair<int,int> pii;
12 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
13 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
14 template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,1):0;}
15 template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,1):0;}
16 template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
17 template<typename T>inline T read(T&x){
18     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
19     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
20 }
21 const int N=200000+7;
22 struct thxorz{int to,nxt,w;}G[N<<3];
23 int Head[N],tot;
24 inline void Addedge(int x,int y,int z){
25     G[++tot].to=y,G[tot].nxt=Head[x],Head[x]=tot,G[tot].w=z;
26     G[++tot].to=x,G[tot].nxt=Head[y],Head[y]=tot,G[tot].w=z;
27 }
28 int n;
29 struct stothx{int x,y,id;}A[N];
30 inline bool cmp1(stothx a,stothx b){return a.x<b.x;}
31 inline bool cmp2(stothx a,stothx b){return a.y<b.y;}
32 priority_queue<pii,vector<pii>,greater<pii> > pq;
33 int dis[N];
34 #define y G[j].to
35 inline void dij(){
36     memset(dis,0x3f,sizeof dis);pq.push(make_pair(dis[1]=0,1));
37     while(!pq.empty()){
38         int x=pq.top().second,d=pq.top().first;pq.pop();
39         if(d^dis[x])continue;
40         if(x==n)return;
41         for(register int j=Head[x];j;j=G[j].nxt)if(MIN(dis[y],d+G[j].w))pq.push(make_pair(dis[y],y));
42     }
43 }
44 #undef y
45 int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
46     read(n);
47     for(register int i=1;i<=n;++i)read(A[i].x),read(A[i].y),A[i].id=i;
48     sort(A+1,A+n+1,cmp1);
49     for(register int i=1;i<n;++i)Addedge(A[i].id,A[i+1].id,A[i+1].x-A[i].x);
50     sort(A+1,A+n+1,cmp2);
51     for(register int i=1;i<n;++i)Addedge(A[i].id,A[i+1].id,A[i+1].y-A[i].y);
52     dij();
53     printf("%d\n",dis[n]);
54     return 0;
55 }
View Code

 

posted @ 2019-09-29 20:13  Ametsuji_akiya  阅读(194)  评论(0编辑  收藏  举报