最短路

1、HDU 2680 Choose the best route

参考:http://blog.csdn.net/niushuai666/article/details/6794343

解题思路:

1、增加源点 0 到 w 个点的距离为 0

2、反向建图,求从终点到 w 个点的最短距离

注意:重边取最小

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9

typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const int maxn = 1010;
int n, m, s, edge;
int dis[maxn], G[maxn][maxn];

int dijkstra(int _s);

int main() {
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // __AiR_H
    int p, q, t, w;
    while (scanf("%d %d %d", &n, &m, &s) != EOF) {
        for (int i = 0; i <= n; ++i) {
            for (int j = 0; j <= n; ++j) { G[i][j] = INF; }
        }
        while (m--) {
            scanf("%d %d %d", &p, &q, &t);
            G[p][q] = min(G[p][q], t);
        }
        scanf("%d", &w);
        int ans = INF;
        while (w--) { scanf("%d", &t); G[0][t] = 0; }
        ans = min(ans, dijkstra(0));
        if (ans != INF) { printf("%d\n", ans); }
        else { printf("-1\n"); }
    }
#ifdef __AiR_H
    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H+
    return 0;
}

int dijkstra(int _s) {
    for (int i = 1; i <= n; ++i) { dis[i] = INF; } dis[_s] = 0;
    priority_queue<pii, vector<pii>, greater<pii> > q;
    q.push(make_pair(dis[_s], _s));
    while (!q.empty()) {
        pii t = q.top(); q.pop();
        int u = t.second;
        if (t.first != dis[u]) { continue; }
        for (int i = 0; i <= n; ++i) {
            if (dis[i] <= dis[u] + G[u][i]) { continue; }
            dis[i] = dis[u] + G[u][i];
            q.push(make_pair(dis[i], i));
        }
    }
    return dis[s];
}


2、POJ 3255 Roadblocks

题意:求次短路

参考:《挑战程序设计竞赛》P108

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9

typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 10;

int N, R, A, B, D, edge = 0;
int head[maxn * 2], Next[maxn * 2], to[maxn * 2], val[maxn * 2], dis[5010], dis_t[5010];

void add_edge(int u, int v, int _val);
void dijkatra(void);

int main() {
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // __AiR_H
    scanf("%d %d", &N, &R);
    memset(head, -1, sizeof(head));
    while (R--) {
        scanf("%d %d %d", &A, &B, &D);
        add_edge(A, B, D); add_edge(B, A, D);
    }
    dijkatra();
    printf("%d\n", dis_t[N]);
#ifdef __AiR_H
    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
    return 0;
}

void add_edge(int u, int v, int _val) {
    to[edge] = v; Next[edge] = head[u]; val[edge] = _val; head[u] = edge++;
}

void dijkatra(void) {
    priority_queue<pii, vector<pii>, greater<pii> > q;
    dis[1] = 0; dis_t[1] = INF;
    for (int i = 2; i <= N; ++i) { dis[i] = INF; dis_t[i] = INF; }
    pii q_t; int t;
    q.push(make_pair(0, 1));
    while (!q.empty()) {
        q_t = q.top(); q.pop();
        if (q_t.first > dis_t[q_t.second]) { continue; }
        for (int i = head[q_t.second]; i != -1; i = Next[i]) {
            t = q_t.first + val[i];
            if (dis[to[i]] > t) {
                swap(dis[to[i]], t); q.push(make_pair(dis[to[i]], to[i]));
            }
            if (dis_t[to[i]] > t && dis[to[i]] < t) {
                dis_t[to[i]] = t; q.push(make_pair(dis_t[to[i]], to[i]));
            }
        }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值