POJ 1062 昂贵的聘礼
搞笑的题目。。。DFS过的。。。
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 using namespace std; 6 #define N 10000000 7 int map[101][101],o[101],n,m,val[101],z[101]; 8 int dfs(int x,int a,int b) 9 { 10 int i,ans = val[x],t; 11 if(b - a > n) return N; 12 for(i = 1;i <= m;i ++) 13 { 14 if(map[x][i] < N&&!z[i]) 15 { 16 z[i] = 1;//注意标记和回溯,同一件物品,可能搜好几次。 17 t = dfs(i,min(a,o[i]),max(b,o[i])); 18 if(ans > map[x][i] + t) 19 { 20 ans = map[x][i] + t; 21 } 22 z[i] = 0; 23 } 24 } 25 return ans; 26 } 27 int main() 28 { 29 int i,j,p,l,x,t,v; 30 scanf("%d%d",&n,&m); 31 for(i = 1;i <= m;i ++) 32 for(j = 1;j <= m;j ++) 33 { 34 map[i][j] = N; 35 } 36 for(i = 1;i <= m;i ++) 37 { 38 scanf("%d%d%d",&p,&l,&x); 39 o[i] = l; 40 val[i] = p; 41 for(j = 1;j <= x;j ++) 42 { 43 scanf("%d%d",&t,&v); 44 if(map[i][t] > v) 45 { 46 map[i][t] = v; 47 } 48 } 49 } 50 z[1] = 1; 51 printf("%d\n",dfs(1,o[1],o[1])); 52 return 0; 53 }