HDU3768 shopping (state compression DP+SPFA) Travel quotient problem

Source: Internet
Author: User

ShoppingTime limit:10000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 577 Accepted Submission (s): 197


Problem Descriptionyou has just moved into a new apartment and has 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 for you need.

Your City was organized as a set of intersections connected by roads. Your House and every store are located at some intersection. Your task is to find the shortest route, begins at Your House, visits all the stores, and RET Urns to your house.

Inputthe first line of input contains a single integer, the number of the test cases to follow. Each test case begins with a line containing-integers N and M, the number of intersections and roads in the city, RESP Ectively. Each of the these integers is between 1 and 100000, inclusive. The intersections is numbered from 0 to N-1. Your House was at the intersection numbered 0. M lines follow, each containing three integers x, Y, and D, indicating that the intersections X and Y is connected by a B Idirectional 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, inclus Ive. The subsequent S lines each contain one integer indicating the intersection in which each store is located. It is possible to reach all of the stores from your house.

Outputfor each test case, output a line containing a single integer, the length of the shortest possible shopping trip fro M 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

Sourceuniversity of Waterloo Local Contest 2010.07.10
Test instructions: Given n points, M-Road, S (1<=s<=10) stores in n places, now requires the minimum cost of all stores to depart from 0 points. Problem solving: First seek the shortest path between the out restaurant and the store, including the starting point 0, then the state compression DP.
#include <stdio.h> #include <queue> #include <vector>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];vector<edg>mapt[n];void) SPFA (int s) {Queue<int    >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<k;i++) {int v=mapt[s][i].v;                if (dis[v]>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<n;i++) mapt[i].clear (), mark[i]=-1; while (m--) {scanF ("%d%d%d", &AMP;A,&AMP;B,&AMP;SS.C); Ss.v=b;            Mapt[a].push_back (ss); Ss.v=a;        Mapt[b].push_back (ss);        } scanf ("%d", &s); store[0]=0; mark[0]=0;        s++;        for (int i=1;i<s;i++) {scanf ("%d", &store[i]); mark[store[i]]=i;            } for (int i=0;i<s;i++)//out restaurant The shortest path between the store {SPFA (store[i]);        for (int j=0;j<n;j++) if (mark[j]>=0) road[i][mark[j]]=dis[j];        } for (int Sta=0;sta<mov (s), sta++) for (int i=0;i<s;i++) Dp[sta][i]=inf;        dp[1][0]=0; for (int Sta=1;sta<mov (s), sta++) for (int. i=0;i<s;i++) if (Dp[sta][i]!=inf) {for (int J =1;j<s;j++) if ((Sta&mov (j)) ==0) {if (Dp[sta|mov (j)][j]>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< s;i++) if (Dp[mov (s) -1][i]+road[i][0]<ans) Ans=dp[mov (s) -1][i]+road[i][0];    printf ("%d\n", ans); }}

HDU3768 shopping (state compression DP+SPFA) Travel quotient problem

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.