Travel (HDU 4284 pressure DP)

Source: Internet
Author: User

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&LT;N;++J) Dp[i][j]=-inf; for (int i=0;i<n;++i) if (MONEY&GT;=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&LT;&LT;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", &AMP;T[I].IN,&AMP;T[I].C,&AMP;T[I].D);    Solve (); }return 0;}

  

Travel (HDU 4284 pressure DP)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.