码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ4152: [AMPPZ2014]The Captain

时间:2018-03-01 21:51:51      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:odi   php   mpp   应该   queue   using   type   memset   struct   

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
 
 
 
哈哈哈哈哈模拟模拟n<=200000
这就是裸的最短路问题啊,但是n有200000
那么两两建边就是200000*200000/2
那就一定得优化
再想:他这个题目很怪,min(|X1-X2|,|Y1-Y2|)???
那tm不就从这下文章了吗
再想,的确,只有X坐标或Y坐标相邻的能用啊,这应该不用证明吧
然后SPFA,可惜,大千世界,无(sang)奇(xin)不(bing)有(kuang)
它把我SPFA卡掉了!!卡掉了!!
因为我不会其他的了,所以要得跑dijstra
至于这个东西,我看了实现之后感叹:真贪啊,不给出题人一点活路
 
代码如下:
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
struct node{
    int x,y,d,next;
}a[2100000];int len,last[2100000];
typedef __gnu_pbds::priority_queue<pair<ll,int> > heap;
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;
}
const ll Maxn=1ll<<51;
struct coordinate{
    int x,y,id;
}b[210000];
bool X_coordinate(coordinate a,coordinate b)
{
    if(a.x!=b.x)return a.x<b.x;
    return a.id<b.id;
}
bool Y_coordinate(coordinate a,coordinate b)
{
    if(a.y!=b.y)return a.y<b.y;
    return a.id<b.id;
}
ll d[210000];
bool v[210000];
int head,tail,list[210000];
int n;
heap::point_iterator id[1000005];
void dijkstra()
{
    heap q;
    memset(d,127,sizeof(d));
    id[1]=q.push(make_pair(0,1));
    d[1]=0;
    while(!q.empty())
    {
        int x=q.top().second;
        q.pop();
        if(x==n)break;
        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(id[y]!=0)q.modify(id[y],make_pair(-d[y],y));
                else id[y]=q.push(make_pair(-d[y],y));
            }
        }
    }
    printf("%lld\n",d[n]);
}
int main()
{
    scanf("%d",&n);
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&b[i].x,&b[i].y);
        b[i].id=i;
    }
    sort(b+1,b+1+n,X_coordinate);
    for(int i=1;i<n;i++)ins(b[i].id,b[i+1].id,min(abs(b[i].x-b[i+1].x),abs(b[i].y-b[i+1].y)));
    for(int i=1;i<n;i++)ins(b[i+1].id,b[i].id,min(abs(b[i].x-b[i+1].x),abs(b[i].y-b[i+1].y)));
    sort(b+1,b+1+n,Y_coordinate);
    for(int i=1;i<n;i++)ins(b[i].id,b[i+1].id,min(abs(b[i].x-b[i+1].x),abs(b[i].y-b[i+1].y)));
    for(int i=1;i<n;i++)ins(b[i+1].id,b[i].id,min(abs(b[i].x-b[i+1].x),abs(b[i].y-b[i+1].y)));
    dijkstra();
    return 0;
}

by_lmy

BZOJ4152: [AMPPZ2014]The Captain

标签:odi   php   mpp   应该   queue   using   type   memset   struct   

原文地址:https://www.cnblogs.com/MT-LI/p/8490066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!