Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Dj+heap,heap改成在线形式就不对了?(而不是一来就设定n个node,赋值无穷大那种)程序哪里有BUG啊?? #include <stdio.h> #include <memory.h> #include <stdlib.h> #define MaxN 1001 #define INF 0x7FFFFFFF struct HEAPtype { int h2n,vop; } heap[MaxN]; int n2h[MaxN],hn; int Better(int ax,int bx) { if (ax<bx) return 1; return 0; } int HeapSwap(int ix,int jx) { int temp; int vx,ux; temp=heap[ix].vop; heap[ix].vop=heap[jx].vop; heap[jx].vop=temp; vx=heap[ix].h2n; ux=heap[jx].h2n; temp=n2h[vx]; n2h[vx]=n2h[ux]; n2h[ux]=temp; temp=heap[ix].h2n; heap[ix].h2n=heap[jx].h2n; heap[jx].h2n=temp; return 0; } int Heapfy(int ix) { int jx; if (hn==0) return 0; /*up adjust*/ jx=0; while (ix/2>=1 && Better(heap[ix].vop,heap[ix/2].vop)) { HeapSwap(ix,ix/2); ix=ix/2; jx=1; } if (jx) return 1; /*else,down adjust*/ jx=ix; do { ix=jx; if (ix*2<=hn && Better(heap[ix*2].vop,heap[ix].vop)) jx=ix*2; if (ix*2+1<=hn && Better(heap[ix*2+1].vop,heap[ix].vop)) jx=ix*2+1; if (ix!=jx) HeapSwap(ix,jx); } while (ix!=jx); return 0; } int Relax(int ix,int wt,int jx) { if (Better(heap[ix].vop+wt,heap[jx].vop)) { heap[jx].vop=heap[ix].vop+wt; Heapfy(jx); } return 0; } struct NODE { int ID,EW; struct NODE * Next; }; struct NODE al[MaxN],*pn; int dest; int i,j; int n,m; int u,v; int wtx; int Dijkstra(int sv,int tv) { /*Initialize Heap*/ memset(heap,0,sizeof(heap)); memset(n2h,-1,sizeof(n2h)); hn=1; n2h[sv]=hn; heap[n2h[sv]].vop=0; heap[n2h[sv]].h2n=sv; while (hn) { if (heap[1].h2n==tv) return heap[1].vop; u=heap[1].h2n; heap[0].vop=heap[1].vop; HeapSwap(1,hn); n2h[u]=0; hn--; Heapfy(1); pn=al[u].Next; while (pn!=NULL) { if (n2h[pn->ID]==-1) { hn++; n2h[pn->ID]=hn; heap[n2h[pn->ID]].vop=INF; heap[n2h[pn->ID]].h2n=pn->ID; } if (n2h[pn->ID]) Relax(0,pn->EW,n2h[pn->ID]); pn=pn->Next; } } return 0; } int main() { while (scanf("%d%d",&m,&n)!=EOF) { memset(al,NULL,sizeof(al)); for (i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&wtx); pn=(struct NODE*)malloc(sizeof(struct NODE)); pn->ID=v; pn->EW=wtx; pn->Next=al[u].Next; al[u].Next=pn; pn=(struct NODE*)malloc(sizeof(struct NODE)); pn->ID=u; pn->EW=wtx; pn->Next=al[v].Next; al[v].Next=pn; } dest=Dijkstra(n,1); printf("%d",dest); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator