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