hdu 3768 Shopping(DFS+最短路)

Shopping

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


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
题意:有有0~n-1个地点,0号点是自己家,其他点是超市,现在给出初始的连通图

再选出k个超市(不超过10个),问从家里到这些超市再回家家里最少需要多久

思路:虽然一共可能有100000个超市,不过要去的超市只有不超过10个,那么如果我们可以知道10个超市和家里这11个地点两两间的最短距离,那么就可以直接DFS暴力或者状压枚举所有可能性~  从中找一个最短的即可

由于此题点和边都是100000,所以肯定用不了常规的dijkstra,我们就用SPFA,复杂度是KE,K一般<=2可以承受

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 100050
#define M 15
#define INF 9999999999
struct Edge
{
    int u,v,w,next;
} edge[3*N];
int cnt,head[N];
int dis[M][M],d[N];
int c[M],s,n;
int vis[N],ans;
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
    edge[cnt].u=u,edge[cnt].v=v;
    edge[cnt].w=w,edge[cnt].next=head[u];
    head[u]=cnt++;
}
void spfa(int id)
{
    queue<int>que;
    memset(vis,0,sizeof(vis));
    for(int i=0; i<n; i++)
        d[i]= i==id?0:INF;
    que.push(id);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].w;
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                if(!vis[v])
                {
                    que.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
void dfs(int u,int step,int now)
{
    if(step==s)
    {
        ans=min(ans,now+dis[u][0]);
        return;
    }
    for(int i=0;i<=s;i++)
    {
        if(i==u) continue;
        if(!vis[i]&&dis[u][i]<INF)
        {
            vis[i]=1;
            dfs(i,step+1,now+dis[u][i]);
            vis[i]=0;
        }
    }
}
int main()
{
    int T;
    int m,u,v,w;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d %d",&n,&m);
        while(m--)
        {
            scanf("%d %d %d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        scanf("%d",&s);
        c[0]=0;
        for(int i=1; i<=s; i++)
            scanf("%d",&c[i]);
        for(int i=0; i<=s; i++)
        {
            spfa(c[i]);
            for(int j=0; j<=s; j++)
                dis[i][j]=d[c[j]];
        }
        memset(vis,0,sizeof(vis));
        ans=INF;
        vis[0]=1;
        dfs(0,0,0);
        printf("%d\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值