Island and study-sister
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 790 Accepted Submission(s): 273
Problem Description
Members of ACM/ICPC teams of Fuzhou University always stay in the laboratory in their free time. One day the team members of OOXX are doing their daily training and suddenly received k calls from study-sisters asking for their help.
You can regard the campus of Fuzhou University is consist of n beautiful islands and connected by m undirection bridges and the study-sisters are in some of these islands waiting for them. As the members of OOXX are all warm-hearted, they don’t want these
study-sisters waiting too long and just want the time the girl whom waiting for the longest be as short as possible. You can assume that they begin to move immediately after they received the calls. They start from the laboratory and they can go anywhere freely
by the bridges.
But due to some mysterious reason, each island can be visited only by one member except the laboratory. This means that even if two guys come to an island in two different times is forbidden. Now your task is calculating how long these study-sisters will wait.
Note that there are three students in team OOXX.
Input
The first line contains only one integer T (T<=150), which is the number of test cases. Each test case contains two integer n (1<= n <=17), m (m <= n*n), means that Fuzhou university is consist of n islands and there are m undirection
bridges connect them. Then comes m lines, each line contains three integer x, y, s, means that there is a bridge connect island x and island y and it takes s unit of time to go through this bridge. The numbers start from 1 to n and the number of the laboratory
is always 1. Then comes a number k (k>=1) indicate that there are k study-sisters waiting for help. The next line are k integers xi (2<=xi<=n) describe where these study-sisters are. You can assume that all the study-sisters are in different islands.
Output
For each test case, output the case number first, then output the time the girl whom wait for the longest wait. You should make this number as small as possible. If any one of the study-sisters can’t get help, just output -1.
Sample Input
4 2 0 1 2 2 1 1 2 1 1 2 4 3 1 2 1 2 3 2 2 4 2 2 3 4 4 3 1 2 2 1 3 3 1 4 4 3 2 3 4
Sample Output
Case 1: -1 Case 2: 1 Case 3: 7 Case 4: 4
Source
2013 Multi-University Training Contest 4
题意:给一个无向图(n<=17),m条边,有三人从1点出发,要去k个城市,由三个人走,每个人经过的城市不能相同除了1点,有的人可以停在1点不走,问三人经过的所有城市点必须走过那k个城市,三人花费最小中的最大值是多少。
解题:先路径压缩求到过的城市的状态花费最少,求出所有可能的状态花费,这是一个人的花费。再用01背包的方法,求出三人城市总状态的最小花费(一定要注意:都是从1点开始出发的状态)。最后求出满足条件的答案。
#include<string.h> #include<stdio.h> #include<queue> using namespace std; const int inf = 1<<29 ; const int N = 17; struct EDG{ int to,next,cost; }edg[N*N]; int eid,head[N]; queue<int>id[1<<N]; int dp[1<<N][N],vist[1<<N][N],mincost[1<<N],f[1<<N]; void addEdg(int u,int v,int c){ edg[eid].to=v; edg[eid].cost=c; edg[eid].next=head[u]; head[u]=eid++; } int main(){ int T,t=0,n,m,a,b,c,mapt[N][N]; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); eid=0; memset(head,-1,sizeof(head)); memset(vist,0,sizeof(vist)); for(int st=1; st<(1<<n); st++) for(int i=0; i<n; i++) dp[st][i]=inf; for(int i=0; i<n; i++) for(int j=0; j<n; j++) mapt[i][j]=inf; while(m--){ scanf("%d%d%d",&a,&b,&c); a--; b--; if(mapt[a][b]>c) mapt[a][b]=mapt[b][a]=c; } for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(mapt[i][j]!=inf) addEdg(i,j,mapt[i][j]); int flag=0; scanf("%d",&m); while(m--){ scanf("%d",&a); a--; flag|=1<<a; } //路径压缩DP dp[1][0]=0; id[1].push(0); vist[1][0]=1; for(int st=1; st<(1<<n); st++) { while(!id[st].empty()){ int u=id[st].front(); id[st].pop(); vist[st][u]=0; for(int i=head[u]; i!=-1; i=edg[i].next){ int v=edg[i].to; if(dp[st|(1<<v)][v]>dp[st][u]+edg[i].cost){ dp[st|(1<<v)][v] = dp[st][u]+edg[i].cost; if(vist[st|(1<<v)][v]==0) vist[st|(1<<v)][v]=1,id[st|(1<<v)].push(v); } } } } for(int st=1; st<(1<<n); st++)//找出一个最小花费到达的城市状态 { mincost[st]=inf; bool in=0; for(int i=0; (1<<i)<=st; i++) if((st&(1<<i))&&dp[st][i]!=inf) { if(mincost[st]>dp[st][i]) mincost[st]=dp[st][i]; } f[st]=mincost[st]; } //用01背包的方法 for(int k=1; k<3; k++) for(int st=(1<<n)-1; st>0; st--) if(st&1) //必须有1点 for(int s1=st; s1>0; s1=(s1-1)&st){ int s2=st^s1; s2|=1; //保证了从1点出发 int tmp=f[s1|1]; if(tmp<mincost[s2])tmp=mincost[s2]; if(f[st]>tmp)f[st]=tmp; } int ans=inf ; for(int st=1; st<(1<<n); st++) if((st&flag)==flag){ if(ans>f[st]) ans=f[st]; } if(ans==inf)ans=-1; printf("Case %d: %d\n",++t,ans); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。