poj1125 stockbroker Grapevine Shortest path Dijkstral + priority queue

Source: Internet
Author: User

poj1125 stockbroker Grapevine Shortest way dijkstral + priority queue////a template bar with commemorative # include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <queue>using namespace Std;typedef pair<int,int    > p;const int max_n = 108;const int INF = 0x3f3f3f3f;struct node {int to;    int W;    int next; Node () {} node (int to,int w,int next): To, W (W), Next (next) {}};node edges[max_n * max_n];int head[max_n];int    Num;int n,m;bool vis[max_n];void add_edge (int u,int v,int w) {edges[num].to = v;    EDGES[NUM].W = W;    Edges[num].next = Head[u]; Head[u] = num++;}    void input () {memset (head,-1,sizeof (head));    num = 0;    int v,w;        for (int i=1;i<=n;i++) {scanf ("%d", &m);            for (int j=1;j<=m;j++) {scanf ("%d%d", &v,&w);        Add_edge (I,V,W);    }}}int Dijkstra (int s) {int d[max_n];    Memset (d,inf,sizeof (d));    memset (vis,0,sizeof (VIS));    D[s] = 0;    Vis[s] = 1; Priority_queue<p,vector<p>,greater<p> > que;    Que.push (P (0,s));        while (!que.empty ()) {P x = Que.top ();        Que.pop ();        int u = x.second;        if (X.first > D[u]) continue;            for (int j = head[u]; j!=-1;j=edges[j].next) {int v = edges[j].to;            int w = EDGES[J].W;                if (D[v] > D[u] + W) {D[v] = D[u] + W;            Que.push (P (d[v],v));    }}} int mx = 0;        for (int i=1;i<=n;i++) {if (d[i]==inf) {return-1;        } else if (D[i] > mx) {mx = d[i]; }} return MX;}    void Solve () {int k =-1;    int mx = INF;        for (int i=1;i<=n;i++) {int x = Dijkstra (i);                if (x!=-1) {if (mx > x) {k = i;            mx = x; }}} printf ("%d%d\n", k,mx);}    int main () {//freopen ("1.txt", "R", stdin);     while (scanf ("%d", &n)!=eof && N) {input ();   Solve (); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

poj1125 stockbroker Grapevine Shortest path Dijkstral + priority queue

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.