CodeForces - 1422D Returning Home (建图 + 最短路)

Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city.

Let's represent the city as an area of n×nn×n square blocks. Yura needs to move from the block with coordinates (sx,sy)(sx,sy) to the block with coordinates (fx,fy)(fx,fy). In one minute Yura can move to any neighboring by side block; in other words, he can move in four directions. Also, there are mm instant-movement locations in the city. Their coordinates are known to you and Yura. Yura can move to an instant-movement location in no time if he is located in a block with the same coordinate xx or with the same coordinate yy as the location.

Help Yura to find the smallest time needed to get home.

Input

The first line contains two integers nn and mm — the size of the city and the number of instant-movement locations (1≤n≤1091≤n≤109, 0≤m≤1050≤m≤105).

The next line contains four integers sxsx sysy fxfx fyfy — the coordinates of Yura's initial position and the coordinates of his home (1≤sx,sy,fx,fy≤n1≤sx,sy,fx,fy≤n).

Each of the next mm lines contains two integers xixi yiyi — coordinates of the ii-th instant-movement location (1≤xi,yi≤n1≤xi,yi≤n).

Output

In the only line print the minimum time required to get home.

Examples

Input

5 3
1 1 5 5
1 2
4 1
3 3

Output

5

Input

84 5
67 59 41 2
39 56
7 2
15 3
74 18
22 7

Output

42

Note

In the first example Yura needs to reach (5,5)(5,5) from (1,1)(1,1). He can do that in 55 minutes by first using the second instant-movement location (because its yy coordinate is equal to Yura's yy coordinate), and then walking (4,1)→(4,2)→(4,3)→(5,3)→(5,4)→(5,5)(4,1)→(4,2)→(4,3)→(5,3)→(5,4)→(5,5).

题意:

有一个n * n的网格,一个人要从(sx, sy)去往(fx, fy),网格中有一些点是特殊点,与特殊点在同一行或同一列上的点,可以直接到达特殊点,花费为0,从任何一个点都可以上下左右移动,每移动一格花费1,问最少花费多少。

思路:

把特殊点所在的行和列当作点

(1)特殊点向它们所在的行和列连双向边,花费都为0(特殊点到列为0因为本来就在该行该列,列到特殊点为0因为可以直接到特殊点);

(2)起点向它所在的行列连边

(3)出现的行之间连双向边,花费为两行之间的距离。假如出现了x个行数,共连x - 1条边(两两连边没有必要并且会炸掉你的边集数组导致RE)

遍历一遍特殊点,答案就是min(起点到特殊点的最短路 + 从特殊点走到终点)

(当然还可能从起点直接走到终点,别忘了取min

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 3e5 + 7;
const ll M = 8e5 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;

ll head[N];
bool vis[N];
ll dis[N];
ll n, m, tot;

struct node {
    ll x, y;
}s[N];

struct Node {
    ll u, v, l, next;
}edge[M];

struct A{
    ll pos, cost;
    bool operator < (const A &a)const {
        return cost > a.cost;
    }
};

void init() {
    tot = 1;
    memset(head, -1, sizeof(head));
}

void addedge(ll x, ll y, ll z) {
    edge[tot].u = x;
    edge[tot].v = y;
    edge[tot].l = z;
    edge[tot].next = head[x];
    head[x] = tot++;
}

void dijk(ll src) {
    memset(dis, inf, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    priority_queue<A>q;
    dis[src] = 0;
    A now;
    now.cost = 0;
    now.pos = src;
    q.push(now);
    while(!q.empty()) {
        now = q.top();
        q.pop();
        if(vis[now.pos])
            continue;
        vis[now.pos] = 1;
        for(ll i = head[now.pos]; ~i; i = edge[i].next) {
            ll to = edge[i].v;
            if(!vis[to] && dis[to] > edge[i].l + dis[edge[i].u]) {
                dis[to] = dis[edge[i].u] + edge[i].l;
                now.cost = dis[to];
                now.pos = to;
                q.push(now);
            }
        }
    }
}

ll stx[N], sty[N], totx, toty;

int main() {
    init();
    totx = 0, toty = 0;
    scanf("%lld%lld", &n, &m);
    for(ll i = 1; i <= m + 2; ++i) {
        scanf("%lld%lld", &s[i].x, &s[i].y);
        stx[++totx] = s[i].x;
        sty[++toty] = s[i].y;
    }
    sort(stx + 1, stx + totx + 1);
    totx = unique(stx + 1, stx + totx + 1) - stx - 1;
    sort(sty + 1, sty + toty + 1);
    toty = unique(sty + 1, sty + toty + 1) - sty - 1;
    n = m + 2;
    map<ll, ll>mx, my;
    for(ll i = 1; i <= totx; ++i) {
        mx[stx[i]] = ++n;
        if(i > 1) {
            ll x = stx[i], y = stx[i - 1];
            addedge(mx[x], mx[y], abs(x - y));
            addedge(mx[y], mx[x], abs(x - y));
        }
    }
    for(ll i = 1; i <= toty; ++i) {
        my[sty[i]] = ++n;
        ll x = sty[i], y = sty[i - 1];
        addedge(my[x], my[y], abs(x - y));
        addedge(my[y], my[x], abs(x - y));
    }
    addedge(1, mx[s[1].x], 0);
    addedge(1, my[s[1].y], 0);
    for(ll i = 3; i <= m + 2; ++i) {
        addedge(i, mx[s[i].x], 0);
        addedge(mx[s[i].x], i, 0);
        addedge(i, my[s[i].y], 0);
        addedge(my[s[i].y], i, 0);
    }
    dijk(1);
    ll minn = inf;
    for(ll i = 3; i <= m + 2; ++i) {
        minn = min(minn, dis[i] + abs(s[2].x - s[i].x) + abs(s[2].y - s[i].y));
    }
    minn = min(minn, abs(s[2].x - s[1].x) + abs(s[2].y - s[1].y));
    printf("%lld\n", minn);
    mx.clear(), my.clear();
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值