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 |
我是用Dijkstra最短路径做的,测了很多数据都没问题,大家能否帮我看看?#include <iostream> #define MAXN 100 using namespace std; int m,n; struct OBJECT { int p,l,x; }Obj[MAXN+1]; struct LIMIT { int up,down; }Lim[MAXN+1]; int Dist[MAXN+1],Cost[MAXN+1][MAXN+1],Pre[MAXN+1]; bool S[MAXN+1]; bool Trade(int a,int b); int main() { cin>>m>>n; int i,j,a,b; for (i=1; i<=n; i++) for (j=1; j<=n; j++) Cost[i][j] = INT_MAX; for (i=1; i<=n; i++) { cin>>Obj[i].p>>Obj[i].l>>Obj[i].x; for (j=0; j<Obj[i].x; j++) { cin>>a>>b; Cost[i][a] = Cost[a][i] = b; } } //for (i=1; i<=n; i++) {for (j=1; j<=n; j++) cout<<Cost[i][j]<<' ';cout<<endl;} memset(S,0,sizeof(S)); for (i=1; i<=n; i++) { Dist[i] = INT_MAX; Lim[i].up = Obj[i].l + m; Lim[i].down = Obj[i].l - m;} Dist[1] = 0; S[1] = true; for (i=1; i<=n; i++) Pre[i] = -1; int v,d; for (i=2; i<=n; i++) if (Trade(1,i)) { Dist[i] = Cost[1][i]; Pre[i] = 1;} //for (i=1; i<=n; i++) cout<<Dist[i]<<' ';cout<<endl; for (i=2; i<=n; i++) { v = 1; d = INT_MAX; for (j=1; j<=n; j++) if (!S[j] && Dist[j] < d) { v = j; d = Dist[j]; } if (v == 1) break;//cout<<v<<endl; S[v] = true; Lim[v].up = Lim[Pre[v]].up < Lim[v].up ? Lim[Pre[v]].up : Lim[v].up; Lim[v].down = Lim[Pre[v]].down > Lim[v].down ? Lim[Pre[v]].down : Lim[v].down; //cout<<Lim[v].up<<' '<<Lim[v].down<<' '<<endl; //cout<<d<<endl; for (j=1; j<=n; j++) { if (!S[j] && Cost[v][j] != INT_MAX && (d + Cost[v][j] < Dist[j]) && Trade(v,j)) { Dist[j] = d + Cost[v][j]; Pre[j] = v; } } } for (i=1; i<=n; i++) if (Dist[i] != INT_MAX) Dist[i] += Obj[i].p; //for (i=1; i<=n; i++) cout<<Dist[i]<<' ';cout<<endl; int min = INT_MAX; for (i=1; i<=n; i++) if (Dist[i] < min) min = Dist[i]; cout<<min<<endl; return 0; } bool Trade(int a,int b) { return (Obj[b].l <= Lim[a].up && Obj[b].l >= Lim[a].down) ; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator