POJ 3255 (dijkstra optimization, shortest times)

Meaning of the questions: seek 1 - n shortest of times

analysis:

  Let's talk about Dijkstra optimization. For each current to find that a point visited in the shortest distance, to optimize the use of priority queues, avoid all the scanning, each update shortest distance of a point to join priority queue. Some people may ask, if a point has been processed, and that it stays in the queue how to do? When we put into the queue when a vertex number, and the shortest distance packaged, if the extraction point, which current is smaller than the shortest distance between the shortest distance of the point markers, indicating that the points have been taken to the shortest distance, no operation is performed . Or directly record whether a vis array to a certain point has been taken to the shortest distance; optimization is followed with all edges connected to each of the adjacent table stored in a point to facilitate processing.

  This question practices and the shortest path basically the same, the only difference is, we must retain the next short path in the case of obtaining the shortest path. Dijkstra determined for every point taken out, if the shortest distance to it is greater than the current time point is a short distance, the current point has the shortest distance and the times taken to a short distance, does not operate, or determination is performed twice: if less than the shortest side, is assigned to the shortest edge

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <string.h>
#include <vector>

#define ll long long
using namespace std;

const int inf = 0x3f3f3f3f;
ll d[5050], d2[5050];
struct edge{
    int u, v, w, next;
}e[200100];
int head[5050], visit[5050], cnt, n, r;
struct node
{
    int id;
    ll dist;
    node(int _id = 0, ll _dist = 0) : id(_id), dist(_dist){}
    bool operator < (const node & x) const
    {
        return dist > x.dist;
    }
};

void add(int u, int v, int w)
{
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].next = head[u];
    head[u] = cnt++;
}

void dij()
{
    for(int i = 0; i <= n; i++)
        d[i] = d2[i] = inf;
    priority_queue<node> q;
    while(!q.empty())
        q.pop();
    d[1] = 0;
    q.push(node(1, 0));
    node p;
    while(!q.empty())
    {
        p = q.top();
        q.pop();
        int u = p.id;
        ll dis = p.dist;
        if(d2[u] < dis)
            continue;
        for(int i = head[u]; i != -1; i = e[i].next)
        {
            int v = e[i].v;
            ll dd = dis + e[i].w;
            if(d[v] > dd)
            {
                swap(d[v], dd);
                q.push(node(v, d[v]));
            }
            if(d2[v] > dd && dd > d[v])
            {
                d2[v] = dd;
                q.push(node(v, d2[v]));
            }
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    int u, v, w;
    cnt = 0;
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &r);
    for(int i = 0; i < r; i++)
    {
        scanf("%d%d%d", &u, &v, &w);
        add(u, v, w);
        add(v, u, w);       
    }
    dij();
    printf("%lld\n", d2[n]);
    return 0;
}

  

And assigned to the shortest side views short sides; or if more than and less than the shortest time becomes short side, the short side is assigned times. After completion of the two were to join the queue

Guess you like

Origin www.cnblogs.com/zxybdnb/p/11576109.html