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 |
dfs 16ms不知道最短路咋做的,先dfs过了再说吧! #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<stdlib.h> #include<algorithm> using namespace std; struct P { int v,cl,m; }node[105]; int map[105][105]; int s[105]; int m,n; int low,top; int dfs(int i) { if(s[i])return 100000000; s[i]=1; if(top<node[i].cl) top=node[i].cl; if(low>node[i].cl) low=node[i].cl; int minn,tmp(0),j,k; minn=node[i].v; for(k=1;k<105;k++) if(map[i][k]&&abs(top-node[k].cl)<=m&&abs(low-node[k].cl)<=m) { tmp=dfs(k); if(minn>tmp+map[i][k]) minn=tmp+map[i][k]; } //cout<<i<<" "<<minn<<endl; s[i]=0; return minn; } int main() { int i,j,a,b; scanf("%d%d",&m,&n); memset(map,0,sizeof(map)); memset(s,0,sizeof(s)); for(i=1;i<=n;i++) { scanf("%d%d%d",&node[i].v,&node[i].cl,&node[i].m) ; for(j=0;j<node[i].m;j++) { scanf("%d%d",&a,&b); map[i][a]=b; } } top=-1;low=1000; printf("%d\n",dfs(1)); return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator