HDU 4640 Island and study-sister(狀態壓縮DP+路徑壓縮)經典 旅行商問題

來源:互聯網
上載者:User

標籤:圖論   演算法   dp   壓縮   

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 DescriptionMembers 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. 
InputThe 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. 
OutputFor 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
42 0122 11 2 1124 31 2 12 3 22 4 223 44 31 2 21 3 31 4 432 3 4
 
Sample Output
Case 1: -1Case 2: 1Case 3: 7Case 4: 4
 
Source2013 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);    }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

HDU 4640 Island and study-sister(狀態壓縮DP+路徑壓縮)經典 旅行商問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.