hdu 4114 Disney's FastPass 状压dp

Disney's FastPass

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=4114

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


hdu 4114 Disney's FastPass 状压dp


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

HINT

 

题意

 游戏园里有N个区域,有M条边连接这N个区域,有K个要访问的景点。对于每个景点告诉你这个景点所在的区域,要访问这个景点需要等待一定时间,如果没有 FastPass,等待时间有Ti,否则等待时间为FTi,接下来的Ni,表示有Ni个区域可以得到这个景点的FastPass,问从区域1出发,再回到 区域1所需要的最少时间。

题解:

状态压缩dp

http://blog.csdn.net/shiqi_614/article/details/11265439

代码:

 

//qscqesze

#include <cstdio>

#include <cmath>

#include <cstring>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <set>

#include <vector>

#include <sstream>

#include <queue>

#include <typeinfo>

#include <fstream>

#include <map>

#include <stack>

typedef long long ll;

using namespace std;

//freopen("D.in","r",stdin);

//freopen("D.out","w",stdout);

#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)

#define maxn 100

#define mod 10007

#define eps 1e-9

int Num;

char CH[20];

//const int inf=0x7fffffff;   //нчоч╢С

const int inf=0x3f3f3f3f;

/*



inline void P(int x)

{

    Num=0;if(!x){putchar('0');puts("");return;}

    while(x>0)CH[++Num]=x%10,x/=10;

    while(Num)putchar(CH[Num--]+48);

    puts("");

}

*/

inline ll read()

{

    ll x=0,f=1;char ch=getchar();

    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}

    return x*f;

}

inline void P(int x)

{

    Num=0;if(!x){putchar('0');puts("");return;}

    while(x>0)CH[++Num]=x%10,x/=10;

    while(Num)putchar(CH[Num--]+48);

    puts("");

}

//**************************************************************************************



int dis[maxn][maxn];

int dp[maxn][1<<8][1<<8];

int pas[1<<8];

int ans;

int t[maxn],ft[maxn],pos[maxn];

int n,m,k;

void init()

{

    ans=inf;

    memset(dp,0x3f,sizeof(dp));  

    memset(dis,0x3f,sizeof(dis));  

    memset(pas,0,sizeof(pas));    

}

void flyod()

{

    for(int k=1;k<=n;k++)

    {

        for(int i=1;i<=n;i++)

        {

            if(i!=k)

            for(int j=1;j<=n;j++)

            {

                if(j!=i&&j!=k)

                dis[i][j]=min(dis[i][k]+dis[k][j],dis[i][j]);

            }

        }

    }

}

void solve()  

{  

    dp[1][0][0]=0;  

    for(int s1=0;s1<(1<<k);s1++)  

    {  

        for(int s2=0;s2<(1<<k);s2++)  

        {  

            for(int i=1;i<=n;i++)  

            {  

                int now=dp[i][s1][s2];  

  

                if(now==inf) continue;  

  

                if(s2==((1<<k)-1)) ans=min(ans,now+dis[i][1]);  

  

                for(int j=0;j<k;j++) if((s2&(1<<j))==0)  

                {  

                    int &nxt=dp[pos[j]][ s1|pas[ pos[j] ] ][s2^(1<<j)];  

  

                    int add=dis[i][pos[j]];  

                    if(s1&(1<<j)) add+=ft[j];  

                    else add+=t[j];  

  

                    nxt=min(nxt,now+add);  

                }  

                for(int j=1;j<=n;j++)  

                {  

                    int &nxt=dp[j][s1|pas[j]][s2];  

                    int add=dis[i][j];  

                    nxt=min(nxt,now+add);  

                }  

            }  

        }  

    }  

    //return ans;  

}  

int main()

{

    //freopen("test.txt","r",stdin);

    int tt=read();

    for(int cas=1;cas<=tt;cas++)

    {

        init();

  

        scanf("%d%d%d",&n,&m,&k);  

        for(int i=0;i<n;i++) dis[i][i]=0;  

        for(int i=0;i<m;i++)  

        {  

            int a,b,c; scanf("%d%d%d",&a,&b,&c);  

            dis[a][b]=dis[b][a]=c;  

        }  

  

        flyod();  

  

        for(int i=0;i<k;i++)  

        {  

            scanf("%d%d%d",&pos[i],&t[i],&ft[i]);  

            int num; scanf("%d",&num);  

            for(int j=0;j<num;j++)  

            {  

                int tmp; scanf("%d",&tmp);  

                pas[tmp]|=(1<<i);  

            }  

        }  

        solve();

        printf("Case #%d: %d\n",cas,ans);

    }

}

 

你可能感兴趣的:(HDU)