Shortest path dijs+ Priority queue Template

Source: Internet
Author: User

P3371 "template" single source shortest path (weakened version)
      • 1 7.5 K via
      • 56.1 K submit
    • topic provider hansbug  webmaster Group
    • Evaluation method Cloud evaluation
    • tags o2 optimization high performance
    • difficulty popularity/Enhancement-
    • Space-time limit 1000MS/128MB

Submit a puzzle

    • Tip: After you've collected a task plan, you can view it on the home page.
Recent discussions show recommended related topics show topic background

The test data for the random data, in the exam may appear in the construction of data let SPFA not pass, if necessary, please go to P4779.

Title Description

Title, given a direction graph, output the shortest path length from a point to all points.

Input output Format input format:

The first line contains three integers n, M, S, respectively, the number of points, the number of forward edges, the number of starting point.

The next M-line contains three integers of fi, Gi, Wi, respectively, indicating the starting point, target point and length of the forward edge of article I.

Output format:

A row that contains n spaces separated by integers, where the I integer indicates the shortest path length from point s to points I (if s=i, the shortest path length is 0, and if from point S cannot reach point I, the shortest path length is 2147483647)

Input and Output sample input sample #: Copy
4 6 11 2 22 3 22 4 11 3 53 4 31 4 4
Output Example # #: Replication
0 2 4 3

Time limit: 1000ms,128m

Data size:

For 20% of data:n<=5,m<=15;

For 40% of data:n<=100,m<=10000;

For 70% of data:n<=1000,m<=100000;

For 100% of data: n<=10000,m<=500000. Ensure that data is random.

#include <bits/stdc++.h>using namespacestd;#defineINF 0x3f3f3f3fusing namespacestd;intN, M;structnode{intto, cost;};structnode2{BOOLFriendoperator<(Node2 N1, Node2 n2) {returnn1.d >n2.d; }    intID, D;}; Vector<node>g[200000+Ten];BOOLfinish[200000+Ten];intdis[200000+Ten];voidMyinit () {memset (finish,0,sizeof(finish));  for(inti =0; I <200001; i++) {g[i].clear (); } memset (DIS, INF,sizeof(DIS));}voidDijkstraints) {Priority_queue<node2>Q; Dis[s]=0;    Node2 nn1; S; NN1.D =Dis[s];    Q.push (NN1);  while(!Q.empty ()) {Node2 ();        Q.pop (); intnow; if(Finish[now])Continue; ElseFinish[now] =1;  for(inti =0; I < g[now].size (); i++)        {            if(!finish[g[now][i].to] && g[now][i].cost + Dis[now] <Dis[g[now][i].to]) {Dis[g[now][i].to]= G[now][i].cost +Dis[now];            } Node2 nn3; G[now][i].to; NN3.D =Dis[g[now][i].to];        Q.push (NN3); }    }}intMain () {intI, J, X, Y, C, S, t;  while(SCANF ("%d%d%d", &n, &m,&s)! =EOF)        {Myinit ();  for(i =0; I < m; i++) {scanf ("%d%d%d", &x, &y, &c);            Node N1; y; N1.cost =C;        G[x].push_back (N1);        } Dijkstra (s);  for(t=1; t<=n;t++){        if(Dis[t] = =inf) cout<<"2147483647"; Elsecout<<Dis[t]; if(t==n) printf ("\ n"); Elseprintf" "); }    }    return 0;}

Shortest path dijs+ Priority queue Template

Related Article

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: 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.