hdu 4571 Travel in time

5737 ワード

Travel in time
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1008 Accepted Submission(s): 204
Problem Description
  Bob gets tired of playing games, leaves Alice, and travels to Changsha alone. Yuelu Mountain, Orange Island, Window of the World, the Provincial Museum etc...are scenic spots Bob wants to visit. However, his time is very limited, he can’t visit them all.
  Assuming that there are N scenic spots in Changsha, Bob defines a satisfaction value Si to each spot. If he visits this spot, his total satisfaction value will plus Si. Bob hopes that within the limited time T, he can start at spot S, visit some spots selectively, and finally stop at spot E, so that the total satisfaction value can be as large as possible. It's obvious that visiting the spot will also cost some time, suppose that it takes C
i units of time to visit spot i ( 0 <= i < N ).
  Always remember, Bob can choose to pass by a spot without visiting it (including S and E), maybe he just want to walk shorter distance for saving time.
  Bob also has a special need which is that he will only visit the spot whose satisfaction value is
strictly larger than that of which he visited last time. For example, if he has visited a spot whose satisfaction value is 50, he would only visit spot whose satisfaction value is 51 or more then. The paths between the spots are bi-directional, of course.
Input
  The first line is an integer W, which is the number of testing cases, and the W sets of data are following.
  The first line of each test data contains five integers: N M T S E. N represents the number of spots, 1 < N < 100; M represents the number of paths, 0 < M < 1000; T represents the time limitation, 0 < T <= 300; S means the spot Bob starts from. E indicates the end spot. (0 <= S, E < N)
  The second line of the test data contains N integers C
i ( 0 <= C
i <= T ), which means the cost of time if Bob visits the spot i.
  The third line also has N integers, which means the satisfaction value Si that can be obtained by visiting the spot i ( 0 <= S
i < 100 ).
  The next M lines, each line contains three integers u v L, means there is a bi-directional path between spot u and v and it takes L units of time to walk from u to v or from v to u. (0 <= u, v < N, 0 <= L <= T)
Output
  Output case number in the first line (formatted as the sample output).
  The second line contains an integer, which is the greatest satisfaction value.
If Bob can’t reach spot E in T units of time, you should output just a “0” (without quotation marks).
Sample Input

   
   
   
   
1 4 4 22 0 3 1 1 1 1 5 7 9 12 0 1 10 1 3 10 0 2 10 2 3 10

Sample Output

   
   
   
   
Case #1: 21
, , , , , , 0 1 , !
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#define MAXN 115
#define MAXM 315
#define inf 10000000
using namespace std;
struct node {
    int id,val,cost;
    bool operator <(node a)const
    {
        return val<a.val;
    }
}p[MAXN];
int dp[MAXN][MAXM],dis[MAXN][MAXN];
int fmin(int a,int b){if(a<b)return a;return b;}
int fmax(int a,int b){if(a>b)return a;return b;}
bool cmp(node a,node b)
{
    return a<b;
}
int main ()
{
    int tcase,tt=1,t,n,m,s,e,i,j,k;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d%d%d%d",&n,&m,&t,&s,&e);
        s++,e++;
        memset(dp,-1,sizeof(dp));
        for(i=0;i<=n;i++)
        {
             for(j=0;j<=n;j++)
            {
                dis[i][j]=inf;
            }
            dis[i][i]=0;
        }
        for(i=1;i<=n;i++)
            scanf("%d",&p[i].cost),p[i].id=i;
        for(i=1;i<=n;i++)
            scanf("%d",&p[i].val);
        for(i=1;i<=m;i++)
        {
            int ss,ee,temp;
            scanf("%d%d%d",&ss,&ee,&temp);
            ss++,ee++;
            dis[ee][ss]=dis[ss][ee]=fmin(dis[ss][ee],temp);
        }
        for(k=1;k<=n;k++)
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                {
                    dis[i][j]=fmin(dis[i][j],dis[i][k]+dis[k][j]);
                }
        p[0]=p[s];
        sort(p+1,p+n+1,cmp);
        for(i=0;i<=n;i++)
        {
            j=p[i].cost+dis[p[i].id][s];
            if(j<=t)
            dp[i][j]=p[i].val;
        }
        for(i=1;i<=n;i++)
            for(j=0;j<i;j++)
            {
                int id=p[i].id,jd=p[j].id;
                if(p[i].val>p[j].val)//         ,       
                for(k=t;k>=dis[id][jd]+p[i].cost&&k>=0;k--)
                {
                    if(dp[j][k-dis[id][jd]-p[i].cost]!=-1)
                        dp[i][k]=fmax(dp[i][k],dp[j][k-dis[id][jd]-p[i].cost]+p[i].val);
                }
            }
        int maxx=0;
        for(i=0;i<=n;i++)
            for(j=0;j<=t;j++)
            {
                if(j+dis[p[i].id][e]<=t)
                maxx=fmax(maxx,dp[i][j]);
            }
        printf("Case #%d:
%d
",tt++,maxx); } return 0; }