HDU6181(次短路两种写法)

题意:求起点1到终点n的次短路。

方法一:用两次dijkstra分别求出起点到所有点的最短距离d1[1...n]和所有点到终点的最短距离d2[1...n]。然后枚举每条边uv,如果d1[u] + uv + d2[v] 不等于最短路,且小于当前答案,更新即可。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
struct edg{
    int v, nxt;
    ll d;
}path[maxn * 2];
int pre[maxn], tot;
ll d1[maxn], d2[maxn];
void add(int u, int v, ll w) {
    path[tot].v = v;
    path[tot].d = w;
    path[tot].nxt = pre[u];
    pre[u] = tot++;
}
struct node{
    int v;
    ll dist;
    bool operator < (const node &b) const {
        return dist > b.dist;
    }
};
int n, m;
bool vis[maxn];
void dijk(int s, ll d[]) {
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; ++i) {
        d[i] = 0x3f3f3f3f3f3f3f3f;//不要用 LONG_LONG_MAX !!!!
    }
    d[s] = 0;
    priority_queue<node> que;
    node p, q;
    p.dist = 0;
    p.v = s;
    que.push(p);
    while (!que.empty()) {
        p = que.top();
        que.pop();
        if (vis[p.v]) {
            continue;
        }
        vis[p.v] = true;
        for (int i = pre[p.v]; ~i; i = path[i].nxt) {
            if (!vis[path[i].v] && d[p.v] + path[i].d < d[path[i].v]) {
                d[path[i].v] = d[p.v] + path[i].d;
                q.v = path[i].v;
                q.dist = d[path[i].v];
                que.push(q);
            }
        }
    }
}
int main(){
    int t, u, v;
    ll w;
    scanf("%d", &t);
    while (t--) {
        tot = 0;
        memset(pre, -1, sizeof(pre));
        scanf("%d%d", &n, &m);
        while (m--) {
            scanf("%d%d%lld", &u, &v, &w);
            add(u, v, w);
            add(v, u, w);
        }
        dijk(1, d1);
        dijk(n, d2);
        ll mn = d1[n];
        ll ans = LONG_LONG_MAX;
        for (int i = 1;  i <= n; ++i) {
            for (int j = pre[i]; ~j; j = path[j].nxt) {
                ll now = d1[i] + path[j].d + d2[path[j].v];
                if (now > mn && now < ans) {
                    ans = now;
                }
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

方法二:做一次dijkstra,dist数组改为两维,dist[i][0]记录到i的最短路,dist[i][1]记录到i的次短路。松弛的时候分别考虑最短和次短两种情况更新。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 100005;
struct edg{
    int v, nxt;
    ll d;
}path[maxn * 2];
int pre[maxn], tot;
ll dist[maxn][2];
void add(int u, int v, ll w) {
    path[tot].v = v;
    path[tot].d = w;
    path[tot].nxt = pre[u];
    pre[u] = tot++;
}
struct node{
    int v, flag; //0表示最短路,1表示次短路
    ll dist;
    bool operator < (const node &b) const {
        return dist > b.dist;
    }
};
int n, m;
bool vis[maxn][2];
void dijk(int s) {
    for (int i = 1; i <= n; ++i) {
        vis[i][0] = vis[i][1] = false;
        dist[i][0] = dist[i][1] = 0x3f3f3f3f3f3f3f3f;//不要用 LONG_LONG_MAX !!!!
    }
    dist[s][0] = 0;
    priority_queue<node> que;
    node p, q;
    p.dist = 0;
    p.v = s;
    p.flag = 0;
    que.push(p);
    while (!que.empty()) {
        p = que.top();
        que.pop();
        if (vis[p.v][p.flag]) {
            continue;
        }
        vis[p.v][p.flag] = true;
        for (int i = pre[p.v]; ~i; i = path[i].nxt) {
            if (!vis[path[i].v][0] && p.dist + path[i].d < dist[path[i].v][0]) {
                if (dist[path[i].v][0] != 0x3f3f3f3f3f3f3f3f) {
                    dist[path[i].v][1] = dist[path[i].v][0];
                    q.dist = dist[path[i].v][0];
                    q.flag = 1;
                    q.v = path[i].v;
                    que.push(q);
                }
                dist[path[i].v][0] = p.dist + path[i].d;
                q.dist = dist[path[i].v][0];
                q.flag = 0;
                q.v = path[i].v;
                que.push(q);
            } else if (!vis[path[i].v][1] && p.dist + path[i].d < dist[path[i].v][1]) {
                dist[path[i].v][1] = p.dist + path[i].d;
                q.dist = dist[path[i].v][1];
                q.flag = 1;
                q.v = path[i].v;
                que.push(q);
            }
        }
    }
}
int main(){
    int t, u, v;
    ll w;
    scanf("%d", &t);
    while (t--) {
        tot = 0;
        memset(pre, -1, sizeof(pre));
        scanf("%d%d", &n, &m);
        while (m--) {
            scanf("%d%d%lld", &u, &v, &w);
            add(u, v, w);
            add(v, u, w);
        }
        dijk(1);
        printf("%lld\n", dist[n][1]);
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值