hdu 3768 Shopping【SPFA+状压DP】

Shopping

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 748    Accepted Submission(s): 250

Problem Description

You have just moved into a new apartment and have a long list of items you need to buy. Unfortunately, to buy this many items requires going to many different stores. You would like to minimize the amount of driving necessary to buy all the items you need.

Your city is organized as a set of intersections connected by roads. Your house and every store is located at some intersection. Your task is to find the shortest route that begins at your house, visits all the stores that you need to shop at, and returns to your house.

 

 

Input

The first line of input contains a single integer, the number of test cases to follow. Each test case begins with a line containing two integers N and M, the number of intersections and roads in the city, respectively. Each of these integers is between 1 and 100000, inclusive. The intersections are numbered from 0 to N-1. Your house is at the intersection numbered 0. M lines follow, each containing three integers X, Y, and D, indicating that the intersections X and Y are connected by a bidirectional road of length D. The following line contains a single integer S, the number of stores you need to visit, which is between 1 and ten, inclusive. The subsequent S lines each contain one integer indicating the intersection at which each store is located. It is possible to reach all of the stores from your house.

 

 

Output

For each test case, output a line containing a single integer, the length of the shortest possible shopping trip from your house, visiting all the stores, and returning to your house.

 

 

Sample Input

1

4 6

0 1 1

1 2 1

2 3 1

3 0 1

0 2 5

1 3 5

3

1

2

3

 

 

Sample Output

4

 

 

Source

University of Waterloo Local Contest 2010.07.10

 

 




题目大意:一共n个点m条无向边,k个需要去的点,求从源点0出发,找到一条最短路径,使得这条路径上经过K个要去的点之后,再回到源点0.


思路:

1、突破口在这个K上,题目保证了K最大也就10个地方,那么我们对于这K个点都求一遍最短路,然后记录在map【10】【10】中,得到一个这K个点互相想通的图。


2、然后我们可以暴力来求这条路径,因为毕竟K也就10个。不过那样会很慢,我们这里采用状压dp的方法来解决这个问题。

建设dp【i】【j】数组,表示i状态要终点是j的最短距离。构建思路是这样的:


要从i状态变成q状态,无非要从i状态中找一个点j,然后在q状态中找到一个点k,从j走到k。

辣么状态转移方程不难推出:
dp【q】【k】=min(dp【q】【k】,dp【i】【j】+dis【j】【k】)其中q一定是从i+(1 <<k)来的。

整个状态转移方程其实就是在表示从i状态中找一个点j,再选一个i状态中没有走过的点k,从j走到k。


辣么要从i状态变成q状态之前呢,要满足这样两个条件:


1、i状态里边有j这个点,才能把j这个点确定下来,辣么我们要怎样确定i状态有没有j这个点呢?根据位运算里边&运算符的操作规则:同1为1,其余为0的特性,我们就可以用

i&(1<<j)判断其值是否为0即可,如果为0,表示i状态中没有走过j(也就是i状态中没有j这个点),如果不为0,那么就表示有。

2、i状态里边没有k这个点,才能把k这个点确定下来,判断方法同上。

注意的点:位运算优先级很难记,不妨在有位运算的地方都加上括号。



AC代码:


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int head[1000000];
struct EdgeNode
{
    int to;
    int w;
    int next;
}e[10000000];
int dp[1<<12][12];
int dis[100015];
int vis[100015];
int pos[15];
int map[15][15];
int n,m,cont,kk;
void SPFA(int ss,int sss)
{
    memset(vis,0,sizeof(vis));
    for(int i=0;i<n;i++)dis[i]=0x3f3f3f3f;
    dis[ss]=0;
    vis[ss]=1;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        s.pop();vis[u]=0;
        for(int j=head[u];j!=-1;j=e[j].next)
        {
            int v=e[j].to;
            int w=e[j].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    int tot=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=kk;j++)
        {
            if(pos[j]==i)
            {
                map[sss][j]=dis[i];
            }
        }
    }
}
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        memset(dp,0x3f3f3f3f,sizeof(dp));
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int x,y,w;
            scanf("%d%d%d",&x,&y,&w);
            add(x,y,w);
            add(y,x,w);
        }
        scanf("%d",&kk);
        pos[0]=0;
        for(int i=1;i<=kk;i++)
        {
            scanf("%d",&pos[i]);
        }
        for(int i=0;i<=kk;i++)
        {
            SPFA(pos[i],i);
        }
        int end=(1<<(kk+1));
        dp[1][0]=0;
        for(int i=0;i<end;i++)
        {
            for(int j=0;j<=kk;j++)
            {
                if (!(i&(1<<j))) continue;
                for(int k=0;k<=kk;k++)
                {
                    if (i&(1<<k)) continue;
                    int q=(i|(1<<k));
                    dp[q][k]=min(dp[q][k],dp[i][j]+map[j][k]);
                }
            }
        }
        int output=0x3f3f3f3f;
        for(int i=1;i<=kk;i++)
        {
            output=min(output,map[i][0]+dp[end-1][i]);
        }
        printf("%d\n",output);
    }
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值