4152: [AMPPZ2014]The Captain

4152: [AMPPZ2014]The Captain

Time Limit: 20 Sec   Memory Limit: 256 MB
Submit: 849   Solved: 324
[ 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

[ Submit][ Status][ Discuss]

两点之间的距离为切比雪夫距离,直接构图边数O(n^2),显然无法接受
可以先将所有点按照x坐标升序排序,相邻点建边
然后按照y坐标升序排序,相邻点建边,在这张图上跑最短路就行了
在这张图上如果距离跑远了,总是会被另外一维约束。。不知道怎么解释,反正直观感受吧~
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
 
const int maxn = 2E5 + 20;
typedef long long LL;
const LL INF = 1E16;
 
struct E{
    int to,w; E(){}
    E(int to,int w): to(to),w(w){}
};
 
struct Point{
    int Num,x,y; Point(){}
    Point(int Num,int x,int y): Num(Num),x(x),y(y){}
}p[maxn];
 
struct data{
    int Num; LL d; data(){}
    data(int Num,LL d): Num(Num),d(d){}
    bool operator < (const data &b) const {return d > b.d;}
};
typedef __gnu_pbds::priority_queue<data,less<data>,__gnu_pbds::pairing_heap_tag> Heap;
 
LL dis[maxn];
int n;
bool inq[maxn];
 
vector <E> v[maxn];
Heap Q;
Heap::point_iterator id[maxn];
 
bool cmpx(const Point &A,const Point &B) {return A.x < B.x;}
bool cmpy(const Point &A,const Point &B) {return A.y < B.y;}
 
int getint()
{
    char ch = getchar(); int ret = 0;
    while (ch < '0' || '9' < ch) ch = getchar();
    while ('0' <= ch && ch <= '9')
        ret = ret*10 + ch - '0',ch = getchar();
    return ret;
}
 
int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
    #endif
     
    n = getint();
    for (int i = 1; i <= n; i++)
    {
        int x = getint(),y = getint();
        p[i] = Point(i,x,y);
    }
    sort(p + 1,p + n + 1,cmpx);
    for (int i = 1; i < n; i++)
    {
        v[p[i].Num].push_back(E(p[i+1].Num,p[i+1].x - p[i].x));
        v[p[i+1].Num].push_back(E(p[i].Num,p[i+1].x - p[i].x));
    }
    sort(p + 1,p + n + 1,cmpy);
    for (int i = 1; i < n; i++)
    {
        v[p[i].Num].push_back(E(p[i+1].Num,p[i+1].y - p[i].y));
        v[p[i+1].Num].push_back(E(p[i].Num,p[i+1].y - p[i].y));
    }
     
    id[1] = Q.push(data(1,0)); inq[1] = 1;
    for (int i = 2; i <= n; i++) dis[i] = INF;
    while (!Q.empty())
    {
        int k = Q.top().Num; Q.pop();
        for (int i = 0; i < v[k].size(); i++)
        {
            E e = v[k][i];
            if (dis[e.to] > dis[k] + 1LL * e.w)
            {
                dis[e.to] = dis[k] + 1LL * e.w;
                if (!inq[e.to]) id[e.to] = Q.push(data(e.to,dis[e.to]));
                else Q.modify(id[e.to],data(e.to,dis[e.to]));
            }
        }
    }
    cout << dis[n] << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据以下要求编写一个python程序1. Description Ship of Fools is a simple classic dice game. It is played with five standard 6-faced dice by two or more players. - The goal of the game is to gather a 6, a 5 and a 4 (ship, captain and crew) in the mentioned order. - The sum of the two remaining dice (cargo) is preferred as high as possible. The player with the highest cargo score wins the round. Example: - In the first round turn, if a player rolls 6 4 3 3 1 (note we five dice at the beginning), the player can bank the 6 (ship), but the rest needs to be re-rolled since there is no 5. - In the second round turn, if the player rolls 6 5 4 4 (four dice, since the 6 from last turn is banked), the player can bank the 5 (captain) and the 4 (crew). The player has three choices for the remaining 6 and 4. The player can bank both and score 10 points, or re-roll one or two of the dice and hope for a higher score. - In the second round turn, if the player instead rolled 4 4 3 1, all dice needs to be re-rolled since there is no 5.程序需要包含一下几个类.The division of responsibility between the different classes is as follows. - Die: Responsible for handling randomly generated integer values between 1 and 6. - DiceCup: Handles five objects (dice) of class Die. Has the ability to bank and release dice individually. Can also roll dice that are not banked. - ShipOfFoolsGame: Responsible for the game logic and has the ability to play a round of the game resulting in a score. Also has a property that tells what accumulated score results in a winning state, for example 21. - Player: Responsible for the score of the individual player. Has the ability, given a game logic, play a round of a game. The gained score is accumulated in the attribute score. - PlayRoom: Responsible for handling a number of players and a game. Every round the room lets each player play, and afterwards check if any player has reached the winning score.
最新发布
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值