HDU3768 Shopping (State compression DP + spfa) Traveling Salesman Problem

Source: Internet
Author: User

HDU3768 Shopping (State compression DP + spfa) Traveling Salesman Problem
ShoppingTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 577 Accepted Submission (s): 197



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 variable items requires going to parse different stores. you wowould 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

14 60 1 11 2 12 3 13 0 10 2 51 3 53123

Sample Output
4

Source University of Waterloo Local Contest 2010.07.10
Question: n points, m routes, and s (1 <= s <= 10) stores are located in n places, it is now required to start from to complete the minimum cost of all the stores. Problem-solving: first find the shortest circuit between the store and the store, including the starting point 0, and then the status is compressed dp.
#include
 
  #include
  
   #include
   
    using namespace std;#define mov(a) (1<<(a))const int N = 100005;const int inf = 99999999;struct EDG{    int v,c;};int dis[N],inq[N],mark[N],n,road[15][15],dp[mov(12)][12];vector
    
     mapt[N];void spfa(int s){    queue
     
      q;    for(int i=0;i<=n;i++)        dis[i]=inf,inq[i]=0;    dis[s]=0;    q.push(s);    while(!q.empty())    {        s=q.front(); q.pop();        inq[s]=0;        int k=mapt[s].size();        for(int i=0;i
      
       dis[s]+mapt[s][i].c) { dis[v]=dis[s]+mapt[s][i].c; if(inq[v]==0) inq[v]=1,q.push(v); } } }}int main(){ int t,m,s,a,b,store[15]; EDG ss; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=0;i
       
        =0) road[i][mark[j]]=dis[j]; } for(int sta=0;sta
        
         dp[sta][i]+road[i][j]) dp[sta|mov(j)][j]=dp[sta][i]+road[i][j]; } } int ans=inf; for(int i=0;i
         
        
       
      
     
    
   
  
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.