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 |
我的dijk算法,请多指教呵呵#include<iostream> #include<memory> using namespace std; #define max 1000000000 long c[105][105]; long dist[105]; long dis(int n,int m,long *dj) { int i,j,k; long min=max; for(k=0;k<=m;k++) { int u; bool * s = new bool [n+1]; for(i=1;i<=n;i++) { if(dj[1]-m+k>dj[i]||dj[1]+k<dj[i]) { dist[i]=max; s[i]=true; } else { dist[i]=c[0][i]; s[i]=false; } } for(i=1;i<=n;i++) { long temp=max; u=1; 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]<max) { long newdist=dist[u]+c[u][j]; if(newdist<dist[j]) dist[j]=newdist; } } delete [] s; if(min>dist[1]) min=dist[1]; } return min; } int main() { int m,n; cin>>m>>n; long* dj = new long [n+1]; int i,j; for(i=0;i<=n;i++) for(j=0;j<=n;j++) c[i][j]=max; for(i=1;i<=n;i++) { int p,k; cin>>p>>dj[i]>>k; c[0][i]=p; for(j=1;j<=k;j++) { int temp1; long temp2; cin>>temp1>>temp2; c[temp1][i]=temp2; } } long min=dis(n,m,dj); cout<<min<<endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator