P2865 [USACO06NOV] Roadblocks G(洛谷)(次短路)

P2865 [USACO06NOV] Roadblocks G(洛谷)(次短路)_第1张图片

P2865 [USACO06NOV] Roadblocks G(洛谷)(次短路)_第2张图片 开一个二维数组 dis[N][2] 分别记录最短路和次短路即可。dijkstra和spfa均可,推荐spfa。

//dijkstra

#include
using namespace std;
const int N = 1e5+5;
typedef long long ll;
typedef pairPII;
int n, m, k;
int T;
priority_queue, greater>q;
struct node
{
    int e, w;
};
vectorv[N];
int dis[N][2];
//bool st[N];      不能加st数组去判断是否遍历过,会导致个别情况无法再次去更新次短路,//不容易想到,因此推荐spfa

void dijkstra()
{
    memset(dis, 0x3f, sizeof dis);
    dis[1][0] = 0;
    q.push({0, 1});
    while(q.size())
    {
        PII t = q.top();
        int t1 = t.first, t2 = t.second;
        q.pop();
        //if(st[t2]) continue;
        //st[t2] = true;

        for(int i = 0; i < v[t2].size(); i ++)
        {
            int f = 0;
            int z = v[t2][i].e;
            //更新最短路,在更新最短路之前,同时让次短路更新为未更新之前的最短路
            if(dis[z][0] > dis[t2][0] + v[t2][i].w)
            {
                dis[z][1] = dis[z][0];
                dis[z][0] = dis[t2][0] + v[t2][i].w;
                f = 1;
            }
            //如果次短路可更新,且最短路不会被更新(当前路径比最短路长,比未更新前的次短路短)
            if(dis[z][1] > dis[t2][0] + v[t2][i].w&&dis[z][0] < dis[t2][0] + v[t2][i].w)
            {
                dis[z][1] = dis[t2][0] + v[t2][i].w;
                f = 1;
            }
            //用子节点的次短路更新当前节点的次短路(因为子节点已确定是次短路,所更新的父节点的次短路也一定比最短路短)
            if(dis[z][1] > dis[t2][1] + v[t2][i].w)
            {
                dis[z][1] = dis[t2][1] + v[t2][i].w;
                f = 1;
            }
            if(f) q.push({dis[z][0], z});      //无论是何种情况,都让最短路入队列,来更新次短路
        }
    }
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> n >> m;
    while(m --)
    {
        int a, b, c;
        cin >> a >> b >> c;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }

    dijkstra();

    cout << dis[n][1] <<"\n";
    return 0;
}

//spfa

#include
using namespace std;
const int N = 1e5+5;
typedef long long ll;

int n, m, k;
int T;
struct node
{
    int e, w;
};
vectorv[N];
int dis[N][2];
bool st[N];
queueq;

void spfa()
{
    memset(dis, 0x3f, sizeof dis);
    dis[n][0] = 0;
    q.push(n);
    st[n] = true;

    while(q.size())
    {
        int t = q.front();
        q.pop();
        st[t] = false;

        for(int i = 0; i < v[t].size(); i ++)
        {
            int z = v[t][i].e;
            if(dis[z][0] > dis[t][0] + v[t][i].w)
            {
                dis[z][1] = dis[z][0];

                dis[z][0] = dis[t][0] + v[t][i].w;
                if(!st[z])
                {
                    q.push(z);
                    st[z] = true;
                }
            }

            if(dis[z][1] > dis[t][0] + v[t][i].w&&dis[z][0] < dis[t][0] + v[t][i].w)
            {
                dis[z][1] = dis[t][0] + v[t][i].w;
                if(!st[z])
                {
                    q.push(z);
                    st[z] = true;
                }
            }

            if(dis[z][1] > dis[t][1] + v[t][i].w)
            {
                dis[z][1] = dis[t][1] + v[t][i].w;
                if(!st[z])
                {
                    q.push(z);
                    st[z] = true;
                }
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(false), cin.tie(0);

    cin >> n >> m;
    while(m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }

    spfa();

    cout << dis[1][1] << "\n";
    return 0;
}

你可能感兴趣的:(算法)