hdu4571

来源:互联网 发布:cx域名不值钱吗 编辑:程序博客网 时间:2024/05/22 12:45

题目链接 hdu4571
problem G

Travel in time

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2787 Accepted Submission(s): 622

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 Ci 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 Ci ( 0 <= Ci <= 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 <= Si < 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

Source
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现
题目大意,有n个景点,m条路径(双向路径),每个景点有一个时间花费time[i],以及一个满意值val[i],如果第i个景点要游玩的话,就必须花费time[i]的时间,同时会得到val[i],的满意值,m条路径,每条路径有一个权值,表示走这条路径需要花费的时间,每个景点可以游玩也可以不游玩,如果游玩,有一个要求,就是该景点的val值要比你上一个游玩景点val值绝对大,相等不可以,然后有一个起点s,一个终点e,起点终点也可以选择玩或不玩,问从s走到e可以得到的最大val值是多少。
每个经典可以选择玩或不玩,所以可以用01背包做,但是时间有限,我只搞明白了spfa做,我用我的思路来讲一下spfa。
先用floyd跑一遍图,这样,就得到每个景点之间游玩的最短路径了,
然后建新图,建一个超级原点S和超级汇点E,S->s 路径权值为time[s],意味着走这条路必然游玩s,然后S再与所有与s相连的点建边,S->i ,路径权值为G[s][i]+time[i],如果走这条路径,必然花费走路时间以及游玩i点的时间,然后在跑了floyd的图上,把所有路径加入到新图中,因为游玩有一个条件就是val[now]>val[last],所以我们建图时,i->j 必定是val[j]>val[i],权值等于G[i][j],然后所有与e连接的点,与E建立新边,i->E权值为G[i][e],因为我们之前对原图进行了floyd(),所以我们现在的新图可以保证每个点游玩,每个点不游玩的路径都有,那么我们从S跑spfa到E,用dp[vex][time]来记录游玩vex点,花费了time时间所得到的最大满意值,最后找一下到e点和到E点所有时间值内满意值最大的输出。
代码如下

#include <cmath>#include <queue>#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int N=105;const int M=20005;const int K=305;const int inf=0x3f3f3f3f;int tim[N], val[N] ;int G[N][N] ;struct kdq{    int to, cost, next ;} ed[M] ;int head[N], num ;struct node{    int v,c;    node()    {        ;    }    node(int a,int b)    {        v=a;        c=b;    }};void add(int u, int v,int c){    ed[num].to = v ;    ed[num].cost = c ;    ed[num].next = head[u] ;    head[u] = num ++ ;}int vis[N][K] ;int dp[N][K] ;int main(){    int T ;    int n, m, s, e, t ;    cin >> T ;    for (int cas = 1 ; cas <= T ; cas ++ )    {        printf("Case #%d:\n",cas) ;        memset(head,-1,sizeof(head));        num = 0 ;        memset(val,0,sizeof(val));        memset(tim,0,sizeof(tim));        for (int i = 0 ; i < N ; i ++ )        {            for (int j = 0 ; j < N ; j ++ )                G[i][j] = inf ;            G[i][i] = 0 ;        }        scanf("%d%d%d%d%d",&n,&m,&t,&s,&e);        for (int i = 0 ; i < n ; i ++ )            scanf("%d",&tim[i]) ;        for (int i = 0 ; i < n ; i ++ )            scanf("%d",&val[i]) ;        int x, y, z ;        for(int i=0; i<m; i++)        {            scanf("%d%d%d",&x,&y,&z) ;            G[x][y] = G[y][x] = min(G[x][y], z) ;        }        ///floyd        for (int k = 0 ; k < n ; k ++ )            for (int i = 0 ; i < n ; i ++ )                for (int j = 0 ; j < n ; j ++ )                    G[i][j] = min(G[i][j], G[i][k] + G[k][j]) ;      ///建新图        for (int i = 0 ; i < n ; i ++ )        {            for (int j = i + 1 ; j < n ; j ++ )            {                if(G[i][j] != inf)                {                    if(val[i] > val[j])                        add(j, i, G[i][j] + tim[i]);                    else if(val[j] > val[i])                        add(i, j, G[i][j] + tim[j]);                }            }        }        ///超级原点n=S  超级汇点 n+1=E        add(n, s, tim[s]) ;        for (int i = 0 ; i < n ; i ++ )        {            if(i != s && G[i][s] != inf)            {                add(n, i, tim[i] + G[i][s]) ;            }            if(i != e && G[i][e] != inf)            {                add(i, n + 1, G[i][e]) ;            }        }        memset(vis,0,sizeof(vis));        memset(dp,0,sizeof(dp));        vis[n][0] = 1 ;        queue<node>que;        que.push(node(n,0));        int u,c,v,tc;        while(!que.empty())        {            node now=que.front();            que.pop();            u = now.v;            c = now.c ;            vis[u][c] = 0 ;            for (int i = head[u] ; i!=-1 ; i = ed[i].next )            {                v = ed[i].to ;                tc = ed[i].cost+ c ;                if(tc > t)                    continue ;                if(dp[v][tc] < dp[u][c] + val[v])                {                    dp[v][tc] = dp[u][c] + val[v] ;                    if(!vis[v][tc])                    {                        vis[v][tc] = 1 ;                        que.push(node(v,tc));                    }                }            }        }        int ans = 0 ;        for (int i = 0 ; i <= t ; i ++ )        {            ans = max(ans, dp[e][i]) ;            ans = max(ans, dp[n + 1][i]) ;        }        printf("%d\n",ans) ;    }    return 0 ;}