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