HDU4114 Disney's FastPass(floyd+状态压缩DP)旅游问题升级(难)

Disney's FastPass

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2117    Accepted Submission(s): 564


Problem Description

Disney's FastPass is a virtual queuing system created by the Walt Disney Company. First introduced in 1999 (thugh the idea of a ride reservation system was first introduced in world fairs), Fast-Pass allows guests to avoid long lines at the attractions on which the system is installed, freeing them to enjoy other attractions during their wait. The service is available at no additional charge to all park guests.
--- wikipedia



Disneyland is a large theme park with plenties of entertainment facilities, also with a large number of tourists. Normally, you need to wait for a long time before geting the chance to enjoy any of the attractions. The FastPass is a system allowing you to pick up FastPass-tickets in some specific position, and use them at the corresponding facility to avoid long lines. With the help of the FastPass System, one can arrange his/her trip more efficiently.
You are given the map of the whole park, and there are some attractions that you are interested in. How to visit all the interested attractions within the shortest time?
 

Input
The first line contains an integer T(1<=T<=25), indicating the number of test cases.
Each test case contains several lines.
The first line contains three integers N,M,K(1 <= N <= 50; 0 <= M <= N(N - 1)/2; 0 <= K <= 8), indicating the number of locations(starting with 1, and 1 is the only gate of the park where the trip must be started and ended), the number of roads and the number of interested attractions.
The following M lines each contains three integers A,B,D(1 <= A,B <= N; 0 <= D <= 10^4) which means it takes D minutes to travel between location A and location B.
The following K lines each contains several integers P i, T i, FT i,N i, F i,1, F i,2 ... F i,Ni-1, F iNi ,(1 <= P i,N i, F i,j <=N, 0 <= FT i <= T i <= 10^4), which means the ith interested araction is placed at location Pi and there are Ni locations F i,1; F i,2 ... F i,N i where you can get the FastPass for the ith attraction. If you come to the ith attraction with its FastPass, you need to wait for only FTi minutes, otherwise you need to wait for Ti minutes.
You can assume that all the locations are connected and there is at most one road between any two locations.
Note that there might be several attrractions at one location.
 

Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is the minimum time of the trip.
 

Sample Input
  
  
2 4 5 2 1 2 8 2 3 4 3 4 19 4 1 6 2 4 7 2 25 18 1 3 4 12 6 1 3 4 6 2 1 2 5 1 4 4 3 1 1 3 2 1 3 4 1 2 4 10 2 8 3 1 4 4 8 3 1 2
 

Sample Output
  
  
Case #1: 53 Case #2: 14
 

Source

题意:给定一个带权无向图(n<=50)和图中若干个景点(k<=8)(每个顶点可包含多个景点),..现在要你‘游’这些景点,‘游’过这些景点的条件是你在这个点等上了一定的时间,如果你有fastpass,也就是经过某些点,那么只需要等FT的时间,否则要等T的时间,问从1号点出发完成任务再回到1号点的最少时间。

下面是看了别的人解题报告:

思路,状态压缩dp,f[i][mark][j]表示当前在j这个点,已经‘游’过的点的2进制状态为i,手上有fastpass的2进制为mark的最少时间

则f[i][mark][j]   ==> f[i | (1<<p)][new_mark][loc[p]] // p是下一个要去的景点号,loc[p]是他的位置,new_mark是经过loc[p]后新的关于fastpass的2进制状态

同理f[i]mark][j][ => f[i][new_mark][j']  // 往另外一个点走,不是最‘游’那些点,可以认为有意图地去是拿fastpass。如果mark==new_mark, 这个式子有没有都不影响结果。


#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;
const int N =55;
const int inf = 999999999;
#define mov(a) (1<<(a))

int mapt[N][N],dp[1<<9][1<<9][N],n
int P[N],T[N],FT[N],pass[N],k;

void init()
{
    for(int j=0; j<mov(k); j++)
    for(int e=0; e<mov(k); e++)
    for(int i=0; i<=n; i++)
        dp[j][e][i]=inf;
    for(int i=0; i<=n; i++)
    {
        pass[i]=0;
        for(int j=0; j<=n; j++)
            mapt[i][j]=inf;
        mapt[i][i]=0;
    }
}
void floyd()
{
    for(int e=1; e<=n; e++)
    for(int i=1; i<=n; i++)
    if(e!=i)
    {
        for(int j=1; j<=n; j++)
        if(i!=j&&e!=j&&mapt[i][j]>mapt[i][e]+mapt[e][j])
          mapt[i][j]=mapt[i][e]+mapt[e][j];
    }
}
int DP()
{
    dp[0][0][1]=0;
    for(int sta=0; sta<mov(k); sta++)
    for(int spa=0; spa<mov(k); spa++)
    for(int i=1; i<=n; i++)
    if(dp[sta][spa][i]!=inf)
    {
        for(int e=0; e<k; e++)
        if((sta&mov(e))==0)
        {
            int cost=((spa|pass[P[e]])&mov(e))>0?FT[e]:T[e];
            if(dp[sta|mov(e)][spa|pass[P[e]]][P[e]]>dp[sta][spa][i]+mapt[i][P[e]]+cost)
                dp[sta|mov(e)][spa|pass[P[e]]][P[e]]=dp[sta][spa][i]+mapt[i][P[e]]+cost;
        }
        //去找旅游景点卷
        for(int j=1; j<=n; j++)
        if(pass[j]>0&&dp[sta][spa|pass[j]][j]>dp[sta][spa][i]+mapt[i][j])
        dp[sta][spa|pass[j]][j]=dp[sta][spa][i]+mapt[i][j];
    }
    
    //找答案
    int ans=inf;
    for(int spa=0; spa<mov(k); spa++)
        for(int i=1;i<=n;i++)
        if(ans>dp[mov(k)-1][spa][i]+mapt[i][1])
            ans=dp[mov(k)-1][spa][i]+mapt[i][1];
    return ans;
}
int main()
{
    int t,cas=0,m,a,b,c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        init();
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(mapt[a][b]>c)
                mapt[a][b]=mapt[b][a]=c;
        }
        for(int i=0;i<k;i++)
        {
            scanf("%d%d%d%d",&P[i],&T[i],&FT[i],&m);
            while(m--)
            {
                scanf("%d",&a);
                pass[a]|=1<<i;
            }
        }

        floyd();
        printf("Case #%d: %d\n",++cas,DP());
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值