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 |
Would you help me correct my code? Thank you!#include<iostream> #include<cmath> #include<iomanip> using namespace std; #define Type int const int maxint = 30000; int Dijkstra(int n, int v, int m, Type **c, int rank[]) { bool s[maxint]; int i,j,u, temp, *dist, **pre,min; dist = new int [n+1]; pre = new int *[n+1]; for(i=0; i<=n; i++) pre[i] = new int [2]; Type newdist; for(i=1; i<=n; i++) { dist[i]=c[v][i]; //第1点到第i点的距离 s[i]=false; pre[i][0]=pre[i][1]=rank[i]; //pre[i][0],pre[i][1]分别存储第1点到第i点的最高等级 和最低等级 } dist[v]=0; s[v]=true; for(i=1; i < n; i++) { temp = maxint; u = v; for(j=1; j<=n;j++) if(!s[j] && (dist[j]<temp)) { u = j; temp = dist[j]; } s[u]=true; for(j=1; j<=n;j++) if(!s[j] && (c[u][j]< maxint) && abs(pre[u][0]-rank[j])<=m && abs(pre[u][1]-rank[j])<=m) { newdist=dist[u]+c[u][j]; if(newdist < dist[j]) { dist[j] = newdist; pre[j][0] = pre[u][0]<rank[j]?pre[u][0]:rank[j]; pre[j][1] = pre[u][1]>rank[j]?pre[u][1]:rank[j]; } } } min = dist[1]+c[0][1]; for(i=2; i<=n; i++) if(min>dist[i]+c[0][i]) min=dist[i]+c[0][i]; return min; } int main() { int m,n,value,num,*rank,i,j,a,b; int **c; cin>>m>>n; c = new int *[n+1]; rank = new int [n+1]; for(i=0; i<=n;i++) c[i] = new int [n+1]; for(i=0; i<=n;i++) for(j=0; j<=n;j++) c[i][j] = maxint; for(i=1; i<=n;i++) { cin>>value>>rank[i]>>num; c[0][i]=value; for(j=0; j<num; j++) { cin>>a>>b; c[i][a]=b; c[a][i]=b; } } cout<<Dijkstra(n, 1, m, c, rank)<<endl; delete []rank; for(i=0; i<=n;i++) delete []c[i]; delete c; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator