poj 1062
题目链接:http://poj.org/problem?id=1062
用dfs找最短路。
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<cmath> 5 using namespace std; 6 const int inf=0x3f3f3f3f; 7 int mp[120][120],dif[120],mi[120],mx[120]; 8 int m,n,out; 9 int my_min(int x,int y){ return x<y?x:y; } 10 bool cheak(int x){ return x>m?true:false; } 11 bool cheak2(int x,int y){ 12 if(dif[x]<=mi[y]) 13 if((mi[y]-dif[x]<=m)&&(mx[y]-dif[x]<=m)) 14 return true; 15 if(dif[x]>=mx[y]) 16 if((dif[x]-mx[y]<=m)&&(dif[x]-mi[y]<=m)) 17 return true; 18 if(dif[x]<=mx[y]&&dif[x]>=mi[y]) 19 return true; 20 return false; 21 } 22 void dfs(int ans, int next) 23 { 24 if(ans>=out) 25 return ; 26 for(int i=1;i<=n;i++){ 27 if(mp[next][i]!=inf&&next==i){ 28 if(out>ans+mp[next][i]&&cheak2(i,next)) 29 out=ans+mp[next][i]; 30 } 31 if(mp[next][i]!=inf&&next!=i){ 32 if(cheak2(i,next)){ 33 int temp=mi[i],temp2=mx[i]; 34 if(dif[i]<=mi[next]){ 35 mi[i]=dif[i]; 36 mx[i]=mx[next]; 37 } 38 else if(dif[i]>=mx[next]){ 39 mx[i]=dif[i]; 40 mi[i]=mi[next]; 41 } 42 else if(dif[i]<=mx[next]&&dif[i]>=mi[next]){ 43 mx[i]=mx[next]; 44 mi[i]=mi[next]; 45 } 46 dfs(ans+mp[next][i],i); 47 mi[i]=temp; 48 mx[i]=temp2; 49 } 50 } 51 } 52 } 53 54 int main() 55 { 56 scanf("%d%d",&m,&n); 57 memset( mp, inf, sizeof mp); 58 for(int i=1;i<=n;i++){ 59 int p,l,x; 60 scanf("%d%d%d",&p,&l,&x); 61 mp[i][i]=p; 62 dif[i]=mi[i]=mx[i]=l; 63 bool flag=cheak(sqrt(dif[i]-dif[1])); 64 for(int j=1;j<=x;j++){ 65 int t,v; 66 scanf("%d%d",&t,&v); 67 if(flag) 68 continue; 69 mp[i][t]=my_min(mp[i][t],v); 70 } 71 } 72 out=mp[1][1]; 73 dfs(0,1); 74 printf("%d\n",out); 75 return 0; 76 }