HDU 1535 Invitation Cards (Reverse composition +dijkstra Priority queue optimization)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1535

Main topic: The shortest path from 1 to any point and the shortest point to the starting point 1

Problem-solving ideas: a positive composition to find the shortest way, and then the direction of the map to change, reverse composition and then find the shortest way, the last two times accumulated and on the line.

Considering the amount of data to the point, Dijkstra need to use priority queue optimization.

First recall Dijkstra common practice is:

Clear all points of the label
set d[0]=0, other D[i]=inf
loop n times {
    in all unlabeled nodes, select the node with the lowest D value x to the
    node x mark
    for all edges starting from X (x, y), update d[y]=min{d [Y],d[x]+w (x, y)}
}    

Dijkstra is primarily optimized for the insertion (update) of numeric values and the removal of a minimum of two operations, so take into account the heap.

Dijkstra Priority Queue Optimization idea: In the STL with priority_queue< node > Q instead of the heap, each vertex current minimum distance with the heap to maintain, in the shortest distance to update, the corresponding element to the direction of the root to meet the nature of the heap. The minimum value that is removed from the heap each time is the next vertex to be used, so that the elements in the heap have O (V), and the update and fetch operations have O (E), so the complexity of the algorithm is O (ELOGV)

The specific code is implemented as follows:

#include <iostream> #include <cstring> #include <queue> #define INF 0x3f3f3f3f using namespace std;
const int maxn=1000005;
    struct node{int v,len;
    BOOL operator < (const node&a) const{return len>a.len; 
}
};
int N,M,DIS[MAXN],VIS[MAXN];
vector<node>e[maxn]; 
vector<node>e1[maxn];
    void Dijkstra (vector<node> g[])//dijkstra priority queue Optimization {memset (vis,0,sizeof (VIS));
    for (int i=0;i<=n;i++) Dis[i]=inf;
    dis[1]=0;
    priority_queue<node>q;
    Q.push (node{1,0}); while (!
        Q.empty ()) {node now=q.top ();
        Q.pop ();
        int v=now.v;
        if (Vis[v]) continue;
        Vis[v]=1;
            for (int i=0;i<g[v].size (); i++) {int u=g[v][i].v,len=g[v][i].len;
                if (!vis[u]&&dis[u]>dis[v]+len) {Dis[u]=dis[v]+len;
            Q.push (Node{u,dis[u]}); }}}} int main () {int t,a,b,C
    cin>>t;
        while (t--) {cin>>n>>m;
            for (int i=0;i<=n;i++) {e[i].clear ();
        E1[i].clear ();
            } while (m--) {cin>>a>>b>>c;   E[a].push_back (Node{b,c});    Forward graph E1[b].push_back (node{a,c});
        Reverse build side} int ans=0;
        Dijkstra (e);
        for (int i=1;i<=n;i++) ans+=dis[i];
        Dijkstra (E1);
        for (int i=1;i<=n;i++) ans+=dis[i];
    cout<<ans<<endl;
} return 0;  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.