Test instructions: To the grid of N Cities M Road, pp in the City 1 have a certain amount of money, want to visit this N cities (including 1), to a city to a certain amount of money, can work in the city, but the premise of working permit (get a certain cost), no work permit can not be worked in the city, but can walk, a city , asked whether PP can visit n cities back to the city 1.
Analysis: The problem is thought of killing Monsters (survival (ZOJ 2297-like pressure DP)
That problem, is also the money if less than 0 hanging, the last to seek the remaining maximum money, first to find the shortest and
Hie with the Pie (POJ 3311 pressure DP)
The pizza delivery is similar.
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <complex> #include <cassert> #include <utility> #include <cstring> #include <cstdlib># Include <iostream> #include <algorithm>using namespace std;typedef pair<int,int> pii;typedef long Long ll; #define Lson l,m,rt<<1#define Pi ACOs ( -1.0) #define Rson m+1,r,rt<<11#define all 1,n,1#define read fre Open ("In.txt", "R", stdin) #define N 1<<16#define INF 99999999const int mod = 1000000007;int n,m,money,tn;int dp[n][ 20],cost[120][120];struct city{int in,c,d;} T[20];void Floyd () {for (int k=1;k<=tn;++k) for (int. I=1;i<=tn;++i) for (int. j=1;j<=tn;++j) cost I [J]=min (Cost[i][j],cost[i][k]+cost[k][j]);} void Solve () {int cas= (1<<n)-1; for (int i=0;i<=cas;++i) for (int j=0;J<N;++J) Dp[i][j]=-inf; for (int i=0;i<n;++i) if (MONEY>=COST[1][T[I].IN]+T[I].D) dp[1<<i][i]=money-cost[1][t[i].in]-t[i]. D+T[I].C; for (int i=1;i<=cas;++i) for (int j=0;j<n;++j) {if (dp[i][j]==-inf| |! (i& (1<<J))) Continue for (int k=0;k<n;++k) {if (j==k) continue; int c=cost[t[j].in][t[k].in]+t[k].d; if (! ( i& (1<<k)) &&dp[i][j]>=c) {int tot=dp[i][j]-c+t[k].c; Dp[i| ( 1<<k)][k]=max (dp[i| ( 1<<k)][k],tot); }}} int f=0; for (int i=0;i<n;++i) if (dp[cas][i]>=cost[1][t[i].in]) {//cout<<dp[cas][i]<<endl; f=1; Break } if (f) printf ("yes\n"); else printf ("no\n");} int main () {int te; scanf ("%d", &te); while (te--) {scanf ("%d%d%d", &tn,&m,&money); for (int i=0;i<=tn;++i) for (int j=0;j<=tn;++j) {if (i==j) cost[i][j]=0; else Cost[i][j]=inf; } int u,v,c; for (int i=0;i<m;++i) {scanf ("%d%d%d", &u,&v,&c); Cost[u][v]=cost[v][u]=min (COST[U][V],C); } Floyd (); scanf ("%d", &n); for (int i=0;i<n;++i) scanf ("%d%d%d", &T[I].IN,&T[I].C,&T[I].D); Solve (); }return 0;}
Travel (HDU 4284 pressure DP)