zoukankan      html  css  js  c++  java
  • poj2387

    最短路

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <queue>
    using namespace std;

    #define V 1005
    #define E 5005
    #define inf 0x3f3f3f3f;

    int cost[E], dist[V];
    int e, pnt[E], nxt[E], head[V], prev[V], vis[V];
    int n, m;

    struct qnode
    {
    int v, c;
    qnode(
    int vv = 0, int cc = 0) :
    v(vv), c(cc)
    {
    }
    bool operator <(const qnode &r) const
    {
    return c > r.c;
    }
    };

    void dijkstra(int n, const int src)
    {
    qnode mv;
    int i, j, k, pre;
    priority_queue
    <qnode> que;
    vis[src]
    = 1;
    dist[src]
    = 0;
    que.push(qnode(src,
    0));
    for (pre = src, i = 1; i < n; i++)
    {
    for (j = head[pre]; j != -1; j = nxt[j])
    {
    k
    = pnt[j];
    if (vis[k] == 0 && dist[pre] + cost[j] < dist[k])
    {
    dist[k]
    = dist[pre] + cost[j];
    que.push(qnode(pnt[j], dist[k]));
    prev[k]
    = pre;
    }
    }
    while (!que.empty() && vis[que.top().v] == 1)
    que.pop();
    if (que.empty())
    break;
    mv
    = que.top();
    que.pop();
    vis[pre
    = mv.v] = 1;
    }
    }

    inline
    void addedge(int u, int v, int c)
    {
    pnt[e]
    = v;
    cost[e]
    = c;
    nxt[e]
    = head[u];
    head[u]
    = e++;
    }

    void init(int nv, int ne)
    {
    int i, u, v;
    int c;
    e
    = 0;
    memset(head,
    -1, sizeof(head));
    memset(vis,
    0, sizeof(vis));
    memset(prev,
    -1, sizeof(prev));
    for (i = 0; i < nv; i++)
    dist[i]
    = inf;
    for (i = 0; i < ne; ++i)
    {
    scanf(
    "%d%d%d", &u, &v, &c);
    u
    --, v--;
    addedge(u, v, c);
    addedge(v, u, c);
    }
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    scanf("%d%d", &m, &n);
    init(n, m);
    dijkstra(n,
    0);
    printf(
    "%d\n", dist[n - 1]);
    return 0;
    }
  • 相关阅读:
    在普通类中调用service
    layui util 工具时间戳转换
    最大值
    药房管理
    线段树2
    线段树1
    Dijkstra
    最大值最小化
    图的M 着色问题
    取余运算
  • 原文地址:https://www.cnblogs.com/rainydays/p/2056293.html
Copyright © 2011-2022 走看看