BZOJ 4152: [AMPPZ2014] The Captain, bzojamppz2014

Source: Internet
Author: User

BZOJ 4152: [AMPPZ2014] The Captain, bzojamppz2014

Time Limit: 20 Sec Memory Limit: 256 MB
Submit: 1550 Solved: 619
[Submit] [Status] [Discuss] Description given n points on the plane, the cost from (x1, y1) to (x2, y2) is defined as min (| x1-x2 |, | y1-y2 | ), calculate the minimum fee from.

 

The first line of Input contains a positive integer n (2 <= n <= 200000), indicating the number of points. Next n rows, each row contains two integers x [I], y [I] (0 <= x [I], y [I] <= 10 ^ 9 ), represent the coordinates of each vertex in sequence.

 

Output is an integer, that is, the minimum cost.

 

Sample Input5
2 2
1 1
4 5
7 1
6 7 Sample Output2
HINT Source

Thanks for Claris upload

 

An interesting question

The fee between the two points is min (| x1-x2 |, | y1-y2 |), which is equivalent to connecting the edge for x and y respectively.

For the three points 1, 2, 3, if x1 <= x2 <= x3, it is not difficult to find | x1-x3 | this side does not need to be connected.

Then we sort all vertices by x, and each vertex is connected to an edge of an adjacent vertex with the absolute value of x difference.

Sort all vertices by y.

Then run the shortest path directly.

Remember to open long

 

 

#include<cstdio>#include<algorithm>#include<queue>#include<cstring>#define Pair pair<int,int>#define F first#define S second#define int long long const int MAXN=1e6+10;using namespace std;inline int read(){    char c=getchar();int x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}    return x*f;}int N;struct node{    int id,x,y;}Point[MAXN];int comp(const node &a,const node &b){return a.x<b.x;}int comp2(const node &a,const node &b){return a.y<b.y;}int dis[MAXN],vis[MAXN];struct E{    int u,v,w,nxt;}edge[MAXN];int head[MAXN],num=1;inline void AddEdge(int x,int y,int z){    edge[num].u=x;edge[num].v=y;edge[num].w=z;edge[num].nxt=head[x];    head[x]=num++;}void Dijstra(){    memset(dis,0xf,sizeof(dis));dis[1]=0;    priority_queue<Pair>q;    q.push(make_pair(0,1));    while(q.size()!=0)    {        while(vis[q.top().S]&&q.size()>0) q.pop();        Pair p=q.top();        vis[p.S]=1;        for(int i=head[p.S];i!=-1;i=edge[i].nxt)            if(dis[edge[i].v]>dis[p.S]+edge[i].w)                dis[edge[i].v]=dis[p.S]+edge[i].w,                q.push(make_pair(-dis[edge[i].v],edge[i].v));    }    printf("%lld",dis[N]);}main(){    #ifdef WIN32    freopen("a.in","r",stdin);    #else    #endif    memset(head,-1,sizeof(head));    N=read();    for(int i=1;i<=N;i++)         Point[i].id=i,Point[i].x=read(),Point[i].y=read();    sort(Point+1,Point+N+1,comp);    for(int i=1;i<=N-1;i++)        AddEdge(Point[i].id,Point[i+1].id,Point[i+1].x-Point[i].x),        AddEdge(Point[i+1].id,Point[i].id,Point[i+1].x-Point[i].x);    sort(Point+1,Point+N+1,comp2);    for(int i=1;i<=N-1;i++)         AddEdge(Point[i].id,Point[i+1].id,Point[i+1].y-Point[i].y),        AddEdge(Point[i+1].id,Point[i].id,Point[i+1].y-Point[i].y);    Dijstra();    return 0;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.