http://acm.hdu.edu.cn/showproblem.php?pid=3339
最短路+01背包
以耗油量为V 以pow为价值
View Code
1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 using namespace std; 5 #define INF 0x3f3f3f 6 int w[101][101],p[101],pi[101],f[10001]; 7 int main() 8 { 9 int i,j,k,n,m,t,a,b,c,v; 10 scanf("%d",&t); 11 while(t--) 12 { 13 memset(f,0,sizeof(f)); 14 scanf("%d%d",&n,&m); 15 for(i =0 ; i <= n ; i++) 16 for(j = 0 ; j <= n ; j++) 17 w[i][j] = INF; 18 for(i = 1; i <= m ;i++) 19 { 20 scanf("%d%d%d",&a,&b,&c); 21 if(w[a][b]>c) 22 { 23 w[a][b] = c; 24 w[b][a] = c; 25 } 26 } 27 int s = 0,v =0 ; 28 for(i = 1; i <= n ; i++) 29 { 30 scanf("%d",&pi[i]); 31 s+=pi[i]; 32 } 33 for(i = 0; i <= n ; i++) 34 { 35 w[i][i] = 0; 36 } 37 for(i = 0; i <= n ; i++) 38 for(j = 0 ; j <= n; j++) 39 for(k = 0 ; k <= n ; k++) 40 if(w[j][k]>w[j][i]+w[i][k]) 41 { 42 w[j][k] = w[j][i]+w[i][k]; 43 } 44 for(i = 1 ; i <= n; i++) 45 { 46 p[i] = w[0][i]; 47 if(p[i]!=INF) 48 v+=p[i]; 49 } 50 int flag = 0,x; 51 for(i = 1; i <= n ; i++) 52 for(j = v ; j>=p[i] ; j--) 53 { 54 if(f[j]<pi[i]+f[j-p[i]]) 55 f[j] = pi[i]+f[j-p[i]]; 56 } 57 for(j = 0 ; j <= v ; j++) 58 { 59 if(f[j]>s/2) 60 { 61 x = j; 62 flag = 1; 63 break; 64 } 65 } 66 if(flag) 67 printf("%d\n",x); 68 else 69 printf("impossible\n"); 70 } 71 return 0; 72 }