spfa最短路 [URAL-1930]

本文介绍了一种解决特定最短路径问题的方法,该问题涉及在一个受到地震影响的世界中找到一条从一个城市到另一个城市的路径,使车辆在上坡和下坡时所需的齿轮变换次数最少。使用邻接表和SPFA算法进行实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:
给一个图,其中包含上坡和下坡,需要变换几次档才能从一点走到另一点。


分析:
这题是个半裸的最短路,之所以说他是半裸,是因为权值不是直接告诉你的。那么就可以开一个数组,存上一次到达这个点是是上坡档还是下坡档。
如果这条路的方向和之前的方向不同,那么权值为1,否则为0.
我是用的spfa+邻接表做的


题目:

The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are still useful, but even they became too steep because of soil movements.
Fortunately, engineer Ivan has a car, which can go well uphill and downhill. But there are different gear-modes for movement up and down, so during the driving you have to change gear-modes all the time. Also engineer Ivan has a good friend –– geologist Orlov. Together they are able to invent a plan for world saving. But, unfortunately, geologist Orlov lives in another town.
Ivan wants to save the world, but gear-box in his car started to wear out, so he doesn’t know, how long he will be able to use it. Please help Ivan to save the world. Find a route to the Orlov's town, such that Ivan will have to change gear-modes as few times as possible. In the beginning of the way Ivan can turn on any of gear-modes and you don't have to count this action as a changing of gear-mode.

Input
There are two positive integer numbers n and m in the first line, the number of towns and roads between them respectively (2 ≤ n ≤ 10 000; 1 ≤ m ≤ 100 000). Next m lines contain two numbers each — numbers of towns, which are connected by road. Moreover, the first is the town, which is situated below, from which you should go uphill by this road. Every road can be used for traveling in any of two directions. There is at most one road between any two cities. In the last line there are numbers of two cities, in which Ivan and geologist Orlov live, respectively. Although the majority of roads were destroyed, Ivan knows exactly, that the way to geologist Orlov’s city exists.
Output
Output the smallest number of gear-modes changes on the way to Orlov’s city.
Example
input output

3 2
1 2
3 2
1 3

1

3 3
1 2
2 3
3 1
1 3

0

代码:

#include <bits/stdc++.h>

using namespace std;

struct edgenode
{
    int pos;
    int dir;
    int next;
}eg[200000];

int dis[10010];
bool vst[10010];
int st, ed, vertex, edge, w, cnt;
int head[10010];
bool nowdir[10010];

int spfa()
{
    queue <int> que;
    int tp;
    dis[st] = 0;
    for(int i=head[st]; i!=-1; i=eg[i].next)
    {
        int pos = eg[i].pos;
        int dir = eg[i].dir;
        nowdir[pos] = dir;
        dis[pos] = 0;
        vst[pos] = true;
        que.push(pos);
    }
    while(! que.empty())
    {
        tp = que.front();
        que.pop();
        vst[tp] = false;
        for(int p=head[tp]; p!=-1; p=eg[p].next)
        {
            int pos = eg[p].pos;
            int weight = eg[p].dir ^ nowdir[tp];
            if(dis[pos] > dis[tp] + weight)
            {
                dis[pos] = dis[tp] + weight;
                nowdir[pos] = eg[p].dir;//改变距离以后 nowdir为tp到当前点的dir
                if(! vst[pos])
                {
                    vst[pos] = true;
                    que.push(pos);
                }
            }
        }
    }
    return dis[ed];
}

void Init()
{
    cnt = 0;
    memset(vst, false, sizeof(vst));
    memset(head, -1, sizeof(head));
    memset(dis, 63, sizeof(dis));
}

int main()
{
    scanf("%d %d", &vertex, &edge);
    Init();
    for(int i=0; i<=edge-1; i++)
    {
        scanf("%d %d", &st, &ed);
        eg[cnt].pos = ed;
        eg[cnt].dir = 1;
        eg[cnt].next = head[st];
        head[st] = cnt ++;
        eg[cnt].pos = st;
        eg[cnt].dir = 0;
        eg[cnt].next = head[ed];
        head[ed] = cnt ++;
    }
    scanf("%d %d", &st, &ed);
    cout << spfa() << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值