HDU4114-floyd+ 状压dp

来源:互联网 发布:b2b多用户商城源码 编辑:程序博客网 时间:2024/05/12 10:03

Disney's FastPass

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


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 Pi, Ti, FTi,Ni, Fi,1, Fi,2 ... Fi,Ni-1, FiNi ,(1 <= Pi,Ni, Fi,j <=N, 0 <= FTi <= Ti <= 10^4), which means the ith interested araction is placed at location Pi and there are Ni locations Fi,1; Fi,2 ... Fi,Ni 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
24 5 21 2 82 3 43 4 194 1 62 4 72 25 18 1 34 12 6 1 34 6 21 2 51 4 43 1 13 2 13 4 12 4 102 8 3 1 44 8 3 1 2
 

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

Source
2011 Asia ChengDu Regional Contest

题目大意

                         给你n个景点和m条路,k个你喜欢的景点,参观该景点需要等待一些时间,如果没有票就得等T有票就得等FT

                 而景区的票可以在其他景点获得,问你参观完所有喜欢的景点并且返回起始点1的最短路程


题目思路:

                        我们看下数据范围k只有8,所以我们很容易想到的是状压,然后就要去想怎样得到状态转移以及构造dp,如果

               我们考虑状态的话,很好想到是,他访问过景点的状态,他身上拥有的门票的状态以及他当前所在的点,因此我们可

               一用dp[i][s1][s2]来表示当前在i点门票状态为s1,访问过的景点的状态为s2,所以我们要求的答案就是dp[1][s1][(1<<k)-1]

               然后我们再来考虑状态转移,如果当前访问的点为i,然后他接下可以去那些点,显然其他所有的点都可以(有路),而如

               过到达了需要访问的景点的话可以选择访问或不访问,这就是状态转移,我们可以从i点出发,去枚举其他可以访问的点,                  

               并且去更新访问点的状态,当然我们每一次走的都是最短路,所以我们可以用floyd预处理下!


AC代码:

  

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int maxn = 55;const int inf = 0x3f3f3f3f;class dpFloyd{public:    int n,m,k;    int dis[maxn][maxn],dp[maxn][1<<8][1<<8],pos[maxn],T[maxn],FT[maxn],pass[maxn];    void floyd(){                                                     //floyd预处理        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][j],dis[i][k]+dis[k][j]);    }    void sove(){        int ans = inf;        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];                    //cout<<now<<endl;                    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 &nex = dp[pos[j]][s1|pass[pos[j]]][s2^(1<<j)]; //枚举他剩下需要访问的景点                        int add = T[j];                        if(s1&(1<<j))add=FT[j];                        add+=dis[i][pos[j]];                        nex=min(nex,add+now);                    }                    for(int j=1;j<=n;j++){                        int &nex = dp[j][s1|pass[j]][s2];                   //枚举其他的点但不访问                        int add = dis[i][j];                        nex = min(nex,add+now);                    }                }            }        }        printf("%d\n",ans);    }    void init(){        int t;scanf("%d",&t);        int tt = 1;        while(t--){            memset(dis,0x3f,sizeof(dis));            memset(dp,0x3f,sizeof(dp));            memset(pass,0,sizeof(pos));            scanf("%d%d%d",&n,&m,&k);            for(int i=1;i<=n;i++)dis[i][i]=0;            while(m--){                int a,b,c;scanf("%d%d%d",&a,&b,&c);                dis[a][b]=dis[b][a]=c;            }            for(int i=0;i<k;i++){               int j;scanf("%d%d%d%d",&pos[i],&T[i],&FT[i],&j);               while(j--){                    int id;scanf("%d",&id);                    pass[id]|=(1<<i);        //记录当前点的门票状态               }            }            printf("Case #%d: ",tt++);            floyd();            sove();        }   }}df;int main(){    df.init();    return 0;}

                   





0 0
原创粉丝点击