DP--HDU4571(Floyd+dp)

题意:http://acm.hdu.edu.cn/showproblem.php?pid=4571

n个景点,m条路,总时间t,起始城市s,终点城市e,接下来给出浏览各个城市的时间,然后给出各个城市浏览后的满意程度。然后是m条路的信息。要求选择浏览方式,使得总的满意程度最大,经过一个城市可以选择不去浏览(包括s,e),注意题目要求当前浏览城市的满意度一定要比前一个浏览城市的满意度高,并且最终要回到城市e

思路:

首先floyd预处理两点之间最短路

然后dp[i][j][k]表示下一个拿 i 物品,拿完剩下 k 元,当前值是j的最大值。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\Users\13606\Desktop\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef unsigned long long ull;
 42 typedef long long ll;
 43 typedef double db;
 44 const db E=2.718281828;
 45 const db PI=acos(-1.0);
 46 const ll INF=(1LL<<60);
 47 const int inf=(1<<28);
 48 const db ESP=1e-9;
 49 const int mod=(int)1e9+7;
 50 const int N=(int)110;
 51 
 52 int ti[N],vi[N];
 53 int mp[N][N];
 54 
 55 void Floyd(int n,int s,int e)
 56 {
 57     for(int i=1;i<=n+2;++i)
 58         mp[i][i]=0;
 59     mp[n+1][e]=mp[e][n+1]=mp[n+2][s]=mp[s][n+2]=0;
 60     for(int k=1;k<=n+2;k++)
 61         for(int i=1;i<=n+2;i++)
 62             for(int j=1;j<=n+2;j++)
 63                 if(mp[i][j]>mp[i][k]+mp[k][j])
 64                     mp[i][j]=mp[i][k]+mp[k][j];
 65 }
 66 
 67 int dp[N][N][305];
 68 
 69 int go(int pre,int u,int remain,int n)
 70 {
 71     if(dp[u][pre][remain]!=-inf)return dp[u][pre][remain];
 72     int& ans=dp[u][pre][remain];
 73     for(int i=1;i<=n+1;++i)
 74     {
 75         if((i==n+1||vi[i]>pre)&&remain-ti[i]-mp[u][i]>=0)
 76             ans=max(ans,go(vi[i],i,remain-ti[i]-mp[u][i],n)+vi[i]);
 77     }
 78     return ans;
 79 }
 80 
 81 int tot=0;
 82 
 83 void solve()
 84 {
 85     int n,m,t,s,e;
 86     sc("%d%d%d%d%d",&n,&m,&t,&s,&e);s++;e++;
 87     for(int i=1;i<=n;++i)sc("%d",&ti[i]);ti[n+1]=ti[n+2]=0;
 88     for(int i=1;i<=n;++i)sc("%d",&vi[i]);
 89 
 90     for(int i=0;i<=n+2;++i)
 91         for(int j=0;j<=n+2;++j)
 92             mp[i][j]=inf;
 93     for(int i=0;i<=n+2;++i)
 94         for(int j=0;j<=100;++j)
 95             for(int k=0;k<=t;++k)
 96                 dp[i][j][k]=-inf;
 97     for(int i=0;i<=100;++i)
 98         for(int j=0;j<=t;++j)
 99             dp[n+1][i][j]=0;
100 
101     for(int i=1;i<=m;++i)
102     {
103         int u,v,dis;
104         sc("%d%d%d",&u,&v,&dis);
105         u++;v++;
106         mp[u][v]=mp[v][u]=min(mp[u][v],dis);
107     }
108     Floyd(n,s,e);
109     pr("Case #%d:
%d
",++tot,go(0,n+2,t,n));
110 }
111 
112 int main()
113 {
114     int T;
115     sc("%d",&T);
116     while(T--)solve();
117     return 0;
118 }
119 
120 /**************************************************************************************/
原文地址:https://www.cnblogs.com/--HPY-7m/p/12421971.html