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 |
为什么WA一开始数组开大了,结果CE,后来改为动态分配数组还是WA,请大牛帮忙看看我的代码,到底问题出在哪里。 #include <iostream> #include <cstdio> using namespace std; int n; __int64 **g,*d,*q; int *h,*p; int qsize; void Makequeue ()//初始化优先队列 { for (int i = 2; i <= n; i++) { q[i] = 1000000001;//对应的d值,根据d值来建优先队列(最小堆) h[i] = i;//其中的i表示堆的编号,h[i]的值为图的顶点编号 p[i] = i;//其中的i为图的顶点编号,p[i]的值为对应的堆的编号 } q[1] = 0; h[1] = 1; p[1] = 1; qsize = n; } inline bool isEmpty() { if (qsize == 0) return true; else return false; } void down () //从堆的根处向下调整最小堆 { int i,j,min,t; i = 1; while (1) { j = 2 * i; if (j < qsize) { if (q[j] > q[j + 1]) min = j + 1; else min = j; } else if (j == qsize) min = j; else break; if (q[min] < q[i]) { t = q[i];//交换d值 q[i] = q[min]; q[min] = t; t = h[i];//交换对应的顶点编号 h[i] = h[min]; h[min] = t; p[h[i]] = min;//重置顶点号与该顶点在堆中的编号 p[h[min]] = i; i = min; } else break; } } void up (int x)//x表示在图中编号为x的顶点,从p[x]处往上调整最小堆 { int t; int i; i = p[x]; while (i > 1) { if (q[i] < q[i / 2]) { t = q[i];//交换d值 q[i] = q[i / 2]; q[i / 2] = t; t = h[i];//交换对应的顶点编号 h[i] = h[i / 2]; h[i / 2] = t; p[h[i]] = i / 2;//重置顶点号与该顶点在堆中的编号 p[h[i / 2]] = i; i = i / 2; } else break; } } int extractMin ()//取得堆的根元素所对应的顶点编号 { int t; t = h[1];//将堆根元素的顶点编号与堆最后一个元素的顶点编号交换 h[1] = h[qsize]; h[qsize] = t; p[h[1]] = qsize;////重置顶点号与该顶点在堆中的编号 p[h[qsize]] = 1; qsize--;//堆中元素减1 down (); return t;//返回堆的根元素所对应顶点编号 } void decrease (int x, int dx)//将顶点x的值减去dx,并调整堆 { q[p[x]] = q[p[x]] - dx; up (x); } void Dijkstra ()//dijkstra 算法求最短路径 { int i,u,dx; for (i = 2; i <= n; i++) d[i] = 1000000001; d[1] = 0; Makequeue (); while (!isEmpty()) { u = extractMin (); for (i = 1; i <= n; i++) { if (d[i] > d[u] + g[u][i]) { dx = d[i] - d[u] - g[u][i]; d[i] = d[u] + g[u][i]; decrease (i,dx); } } } } int main () { // freopen ("1511.txt","r",stdin); int t,i,j,m,dd,s; __int64 sum1,temp,c; scanf ("%d",&t); while (t--) { scanf ("%d%d",&n,&m); g = new __int64* [n + 1]; for (i = 0; i <= n; i++) g[i] = new __int64 [n + 1]; d = new __int64 [n + 1]; h = new int [n + 1]; q = new __int64 [n + 1]; p = new int [n + 1]; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) { g[i][j] = 1000000001; } for (i = 1; i <= n; i++) { g[i][i] = 0; } for (i = 0; i < m; i++) { scanf ("%d%d%I64d",&s,&dd,&c); g[s][dd] = c; } Dijkstra (); sum1 = 0; for (i = 2; i <= n; i++) sum1 = sum1 + d[i]; for (i = 1; i <= n; i++) for (j = i; j <= n; j++)//将边反向 { temp = g[i][j]; g[i][j] = g[j][i]; g[j][i] = temp; } Dijkstra (); for (i = 2; i <= n; i++) sum1 = sum1 + d[i]; printf ("%I64d\n",sum1); free (d); free (h); free (p); free (q); for (i = 0; i <= n; i++) free (g[i]); free (g); } return 0; } AC了的大牛能否发一份带优先队列优化的Dijkstra算法的代码至 lironghuascut@yahoo.com.cn中,感激不尽。 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator