Disney's FastPass
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2336 Accepted Submission(s):
644
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.
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
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
题意:游戏园里有N个区域,有M条边连接这N个区域,有K个要访问的景点。对于每个景点告诉你这个景点所在的区域,要访问这个景点需要等待一定时间,如果没有FastPass,等待时间有Ti,否则等待时间为FTi,接下来的Ni,表示有Ni个区域可以得到这个景点的FastPass,问从区域1出发,再回到区域1所需要的最少时间。
第一次写状态压缩,参照别人的代码。首先是floyd预处理出任意两点之间的最短距离。dp[state1][state2][u]表示在该状态state1(已经访问过的景点)、state2(手中有的景点的票)、目前所在的位置时所花费的时间的最小值,于是答案就是dp[(1<<k)-1][state][1]了(0<=state<(1<<n))。
附上代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <cmath> 6 #define INF 0x3f3f3f3f 7 #define M 8 #define N 60 9 using namespace std; 10 int n,m,k,ans; 11 int map[N][N]; 12 int p[N],t[N],ft[N],pass[N]; 13 int dp[1<<9][1<<9][N]; 14 15 void floyd() 16 { 17 int i,j,k; 18 for(k=1; k<=n; k++) 19 for(i=1; i<=n; i++) 20 for(j=1; j<=n; j++) 21 if(map[i][j]>map[i][k]+map[k][j]) 22 map[i][j]=map[i][k]+map[k][j]; 23 } 24 25 void Get_Dp() 26 { 27 int i,j,u,v; 28 for(i=0; i<=(1<<k); i++) 29 for(j=0; j<=(1<<k); j++) 30 for(u=1; u<=n; u++) 31 dp[i][j][u]=INF; 32 dp[0][0][1]=0; 33 for(int state1=0; state1<(1<<k); state1++) 34 { 35 for(int state2=0; state2<(1<<k); state2++) 36 { 37 for(u=1; u<=n; u++) 38 { 39 if(dp[state1][state2][u]<INF) 40 { 41 for(i=0; i<k; i++) 42 { 43 if(!(state1&(1<<i))) ///单独取state1的每一位数字 44 { 45 int cost=map[u][p[i]]; 46 if((state2&(1<<i))) cost+=ft[i]; 47 else cost+=t[i]; 48 dp[state1|(1<<i)][state2|pass[p[i]]][p[i]]=min(dp[state1|(1<<i)][state2|pass[p[i]]][p[i]],dp[state1][state2][u]+cost); 49 } 50 } 51 for(v=1; v<=n; v++) 52 { 53 dp[state1][state2|pass[v]][v]=min(dp[state1][state2|pass[v]][v],dp[state1][state2][u]+map[u][v]); 54 } 55 } 56 } 57 } 58 } 59 ans=INF; 60 for(i=0; i<(1<<k); i++) 61 ans=min(ans,dp[(1<<k)-1][i][1]); 62 } 63 64 int main() 65 { 66 int i,j,T,cas,num,v; 67 scanf("%d",&T); 68 for(cas=1; cas<=T; cas++) 69 { 70 scanf("%d%d%d",&n,&m,&k); 71 for(i=1; i<=n; i++) 72 for(j=1; j<=n; j++) 73 if(i==j) map[i][j]=0; 74 else map[i][j]=INF; 75 int a,b,c; 76 while(m--) 77 { 78 scanf("%d%d%d",&a,&b,&c); 79 if(map[a][b]>c) 80 map[a][b]=c,map[b][a]=c; 81 } 82 floyd(); 83 memset(pass,0,sizeof(pass)); 84 for(i=0; i<k; i++) 85 { 86 scanf("%d%d%d%d",&p[i],&t[i],&ft[i],&num); 87 while(num--) 88 { 89 scanf("%d",&v); 90 pass[v]|=(1<<i); ///没有进位的加法 91 } 92 } 93 Get_Dp(); 94 printf("Case #%d: %d ",cas,ans); 95 } 96 return 0; 97 }