hdu 4640 Island and study-sister(压缩DP+最短路,5级)

第4场多校联合训练解题报告——详见杭电ACM微博~

Island and study-sister

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 236    Accepted Submission(s): 70


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
 

Recommend
zhuyuanchen520

思路:先找出最短路,然后状态压缩DP一下。

坑:不能全部初始化,会超


#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
#define ll(z) (1<<z)
using namespace std;
const int mm=18;
const int oo=0x3f3f3f3f;
int dis[mm][ll(mm)],dp[2][ll(mm)];
int head[mm],edge,target;
bool vis[mm][ll(mm)];
class Edge
{
  public:int v,next,w;
}e[mm*mm*2];
int cas,n,m;
void data()
{
  clr(head,-1);edge=0;
}
void add(int u,int v,int w)
{
  e[edge].v=v;e[edge].w=w;e[edge].next=head[u];head[u]=edge++;
  e[edge].v=u;e[edge].w=w;e[edge].next=head[v];head[v]=edge++;
}
void spfa()
{
  queue<pair<int,int > >Q;
 // clr(dis,0x3f);clr(vis,0);
  FOR(i,0,ll(n)-1)FOR(j,0,n)dis[j][i]=oo,vis[j][i]=0;
  Q.push(make_pair(0,1) );dis[0][1]=0;
  int u,v,state,snew;
  pair<int,int>z;
  while(!Q.empty())
  {
    z=Q.front();Q.pop();
    u=z.first;state=z.second;vis[u][state]=0;
    for(int i=head[u];~i;i=e[i].next)
    {
      v=e[i].v;snew=state|ll(v);
      if(dis[v][snew]>dis[u][state]+e[i].w)
      {
        dis[v][snew]=dis[u][state]+e[i].w;
        if(!vis[v][snew])
        {Q.push(make_pair(v,snew));vis[v][snew]=1;}
      }
    }
  }
 // clr(dp,0x3f);
  FOR(i,2,ll(n)-1)
  {dp[0][i>>1]=oo;
   FOR(j,1,n)
   {
    dp[0][i>>1]=min(dp[0][i>>1],dis[j][i|1] );
    //printf("ddis= %d %d\n",dp[0][i],i);
   }
  }
 // FOR(i,0,ll(n)-1)cout<<dp[0][i]<<endl;
  dp[0][0]=0;///important
}
void DP()
{ n--;
  FOR(i,0,ll(n)-1)
  {dp[1][i]=oo;
   for(int j=i;j>0;j=(j-1)&i)
   {
    if(dp[0][i^j]!=oo && dp[0][j]!=oo )
    {
      dp[1][i]=min(dp[1][i],max(dp[0][i^j],dp[0][j]) );
    }
   }
  }
  //FOR(i,0,ll(n)-1)
 // cout<<dp[1][i]<<endl;
  int ans=oo;
  FOR(i,0,ll(n)-1)
  for(int j=i;j>0;j=(j-1)&i)
  { if((i&target)==target)
    if(dp[0][i^j]!=oo && dp[1][j]!=oo )
    { //printf("pp= %d %d\n",dp[0][i^j],dp[1][j]);
      ans=min(ans,max(dp[0][i^j],dp[1][j]) );
    }
  }
 // FOR(i,0,ll(n)-1)//cout<<dp[2][i]<<endl;
 // if((i&target)==target)
 // ans=min(ans,dp[2][i]);
  if(ans==oo)printf("-1\n");
  else printf("%d\n",ans);
}
int main()
{ int K,a,b,c;
  //freopen("1009.in","r",stdin);
  while(~scanf("%d",&cas))
  {
    FOR(ca,1,cas)
    {
      printf("Case %d: ",ca);
      data();
      scanf("%d%d",&n,&m);
      FOR(i,1,m)
      {
        scanf("%d%d%d",&a,&b,&c);--a;--b;add(a,b,c);
      }
      scanf("%d",&K);
      target=0;
      FOR(i,1,K)
      {
        scanf("%d",&a);a-=2;target|=ll(a);
      }
     // printf("tt=%d\n",target);
      spfa();DP();
    }
  }
  return 0;
}




你可能感兴趣的:(hdu 4640 Island and study-sister(压缩DP+最短路,5级))