HDU 4640 Island and study-sister(状态压缩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 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);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 15:08:10

HDU 4640 Island and study-sister(状态压缩DP+路径压缩)经典 旅行商问题的相关文章

HDU 4640 Island and study-sister(状态压缩)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4640 题意:给出一个无向图,边有权值.三个人初始时呆在1号点.在其余n-1个点中有些点要求被遍历到.且除了1号点之外每个点最多只能被一个人遍历.求要求被遍历的点中最后被遍历的点的最小时刻. 思路:用f[st][i]表示一个人遍历完集合st最后停在i的最小时间,之后用f[st][i]得到f1[st],表示遍历完集合st的最小时间.然后得到dp[i][st]表示i个人遍历完st的最小时间. int g[

hdu 1208 Pascal&#39;s Travels (子状态继承dp)

Pascal's Travels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1774    Accepted Submission(s): 781 Problem Description An n x n game board is populated with integers, one nonnegative integer

hdu 2167 方格取数 【状压dp】(经典)

<题目链接> 题目大意: 给出一些数字组成的n*n阶矩阵,这些数字都在[10,99]内,并且这个矩阵的  3<=n<=15,从这个矩阵中随机取出一些数字,在取完某个数字后,该数字周围8个点都不能取,问:取得数字的最大和为多少? 解题分析: 由于对每一个数,有选和不选两种可能,分别对应状态压缩中的1和0,且 n<=15,1<<15不是非常大,因此就可以非常自然的想到状态压缩. 此题要与普通的状压dp不同的是,当某一行取某种方案时,如何求出这种取数的所有取得的数之和,

【HDU 4352】 XHXJ&#39;s LIS (数位DP+状态压缩+LIS)

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

hdu 4568(状态压缩dp)

题意:一张n*m的网格内每个点有话费,还有若干个宝藏,问一个人要走进去拿走所有宝藏在走出来的最小花费. 思路:看宝藏只有13个直接想到了状压dp[i][j]拿了哪几个前一个为j的最小花费,先bfs+优先队列预处理出最短路,然后记忆化搜索就可. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao

HDU 1565 方格取数(1) (状态压缩DP)

HDU 1565 方格取数(1) (状态压缩DP) ACM 题目地址: HDU 1565 方格取数(1) 题意: 中文. 分析: dp[i][j]表示前i行状态j的最优解. 先预处理出符合条件的数,17000+个(n在20以内). 不过感觉复杂度挺高的会T,但是却能A. 这题的正解应该是最小割,回头补下. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1565_dp.cpp * Create Date: 2014-09-19 23

hdu 3217 Health(状态压缩DP)

Health Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 527    Accepted Submission(s): 145 Problem Description Unfortunately YY gets ill, but he does not want to go to hospital. His girlfriend LM

HDU 3001 Travelling (三进制状态压缩 DP)

题意:有 n 个city,可以选择任一城市作为起点,每个城市不能访问超过2次, 城市之间有权值,问访问全部n个城市需要的最小权值. 思路:因为每个城市可以访问最多两次,所以用三进制表示访问的状态. 详细见代码注释!!!! #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #inclu

[ACM] HDU 1400 Mondriaan&#39;s Dream (状态压缩,长2宽1长方形铺满)

Mondriaan's Dream Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 783    Accepted Submission(s): 506 Problem Description Squares and rectangles fascinated the famous Dutch painter Piet Mondri