Poj1502:mpi Maelstrom

Source: Internet
Author: User

The Dijstra algorithm of Euonymus fruit is applied, and the adjacency table is used to store the graph.

Episode: While (scanf ("%d", &n) commit memory overrun, change to while (scanf ("%d", &n)! =eof) on AC, don't know why

Dijstra algorithm application: The known fixed point is the input, input all other points in the graph to the minimum distance of the fixed point.

Specific practices:

A. Initially, S contains only the source point, that is, the distance of S={v},v is 0. U contains other vertices other than V, that is: u={remaining vertices}, if V and u in vertex u have edges, then <u,v> normal right value, if U is not V's out edge adjacency point, then <u,v> weight is ∞.

B. Select the smallest vertex k from U, and add K to S (the selected distance is the shortest path length of V to K).

C. Change the distance of each vertex in the U with K as the new consideration; If the distance from the source point v to the vertex u (through vertex K) is shorter than the original distance (without the vertex K), the distance value of the vertex U is modified, the distance of the vertex k of the modified distance value and the right of the edge are added.

D. Repeat steps b and c until all vertices are contained in S.

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 Const intMax_size=101;8 Const intMaxint=9999999;9typedefstructarcnode{Ten     intnode; One     intLen; AArcnode *Next; - }arcnode; -typedefstructheadnode{ theArcnode *Firstarc; - }adjlist[max_size]; -typedefstructcmap{ - adjlist map; +     intNodenum, Arcnum; - }cmap; + intN; A intst,ed; at intDis[max_size]; - BOOLVis[max_size]; - voidInitiate (CMAP *c) { -      for(intI=1; i<=c->nodenum;i++){ -C->map[i].firstarc=NULL; -     } in } - voidAddedge (CMap *c,int  from,intTo,intLen) { to     if( from==to)return; +Arcnode *arc= (Arcnode *) malloc (sizeof(Arcnode)); -Arc->node=to ; thearc->len=Len; *arc->next=c->map[ from].firstarc; $c->map[ from].firstarc=arc;Panax Notoginseng      -Arcnode *arcc= (Arcnode *) malloc (sizeof(Arcnode)); theArcc->node= from; +arcc->len=Len; AArcc->next=c->Map[to].firstarc; theC->map[to].firstarc=ARCC; +  - } $ /* $ void print (CMap *c) { - for (int i=1;i<=c->nodenum;i++) { - Arcnode *p=c->map[i].firstarc; the While (p) { - printf ("(%d)%d", p->len,p->node);Wuyi p=p->next;  the         } - printf ("\ n");  Wu     } - } About */ $ voidDijstra (CMAP *c) { -     intans=-1; -Arcnode *p=c->Map[st].firstarc; -memset (Dis,maxint,sizeof(DIS)); Amemset (Vis,false,sizeof(Vis)); +      while(p) { theDis[p->node]=p->Len; -P=p->Next; $     } thedis[st]=0; thevis[st]=true; the      the      for(intI=1; i<=c->nodenum-1; i++){ -         intmin_dist=9999999; in         intu; the          for(intj=1; j<=c->nodenum;j++){ the             if(!vis[j]&&dis[j]<min_dist) { Aboutmin_dist=Dis[j]; theu=J; the             } the         } +vis[u]=true; -Arcnode *p=c->Map[u].firstarc; the          while(p) {Bayi             intLen=p->Len; the             intTemp=p->node; the             if(!vis[temp]&&min_dist+len<Dis[temp]) { -dis[temp]=min_dist+Len; -             } theP=p->Next; the         } the  the     } -      for(intI=2; i<=c->nodenum;i++){ the         if(Dis[i]>ans) ans=Dis[i]; the     } theprintf"%d\n", ans);94 } the intMain () { the      while(SCANF ("%d", &n)! =EOF) { theCMap *c= (CMAP *) malloc (sizeof(CMAP));98c->nodenum=n; c->arcnum=n*n/2; About initiate (c); -st=1;101         Chartmp[ -];102          for(intI=1; i<=n-1; i++){103              for(intj=1; j<=i;j++){104scanf"%s", TMP); the                 if(tmp[0]=='x') Addedge (c,i+1, j,maxint);106                 ElseAddedge (c,i+1, J,atoi (TMP));107             }108         }109         //print (c);  the Dijstra (c);111     } the     return 0;113}
View Code

Poj1502:mpi Maelstrom

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.