HDU 3768 (spfa+ violence)

Source: Internet
Author: User

Shopping

Time limit:10000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 758 Accepted Submission (s): 254


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 Input14 60 1 11 2 12 3 13 0 10 2 51 3 53123

Sample Output4

Source University of Waterloo Local Contest 2010.07.10 Test instructions: Select <=10 points within 100,000 points, then determine the minimum distance required from 0 to each point and then back to 0. Puzzle: The pit is miserable, the SPFA vis array is assigned a value of false each time it enters the queue .... And then it was spfa+ violence.
#include <iostream>#include<cstdio>#include<string.h>#include<queue>#include<algorithm>#include<math.h>using namespaceStd;typedefLong LongLL;ConstLL INF =999999999999;ConstLL N =100005;structedge{LL V,next; LL W;} edge[2*N];structcity{LL Id,idx;} c[ -]; LL Head[n]; LL tot,n,m,q;BOOLVis[n]; LL Low[n]; LL dis[ the][ the]; LL MIN;voidAddedge (LL u,ll v,ll W,ll &k) {EDGE[K].V= V,EDGE[K].W = W,edge[k].next = head[u],head[u]=k++;}voidinit () {memset (head,-1,sizeof(head)); Tot=0;  for(LL i=0;i< the; i++){         for(LL j=0;j< the; j + +) {Dis[i][j]=INF; }    }}voidSPFA (LL pos) { for(LL i=0; i<n;i++) {Low[i]=INF; Vis[i]=false; } Low[pos]=0; Queue<LL>Q;    Q.push (POS);  while(!Q.empty ()) {LL U=Q.front ();        Q.pop (); Vis[u]=false;///!!!!!!!!!!!!!!!!!!         for(LL k=head[u];k!=-1; k=Edge[k].next) {LL W= Edge[k].w,v =edge[k].v; if(low[v]>low[u]+W) {Low[v]= low[u]+W; if(!Vis[v]) {Vis[v]=true;                Q.push (v); }            }        }    }}BOOLvis1[ -];voiddfs (LL u,ll step,ll ans) {Vis1[u]=true; if(step==Q) {MIN= Min (min,ans+dis[u][0]); return; }     for(LL i=0; i<=q;i++){        if(!vis1[i]&&dis[u][i]<INF) {DFS (I,step+1, ans+Dis[u][i]); Vis1[i]=false; }    }}intMain () {inttcase; scanf ("%d",&tcase);  while(tcase--) {init (); scanf ("%lld%lld",&n,&m);  for(LL i=1; i<=m;i++) {LL u,v,w; scanf ("%lld%lld%lld",&u,&v,&W);            Addedge (U,v,w,tot);        Addedge (V,u,w,tot); } scanf ("%lld",&Q); c[0].id =0; c[0].idx =0;  for(LL i=1; i<=q;i++) {scanf ("%lld",&c[i].id); C[i].idx=i; }         for(LL i=0; i<=q;i++) {SPFA (c[i].id);  for(LL j=0; j<=q;j++) {Dis[c[i].idx][c[j].idx]=Low[c[j].id]; }        }       /*for (ll i=0;i<=q;i++) {for (ll j=0;j<=q;j++) {printf ("%lld", Dis[i][j]);        } printf ("\ n"); }*/MIN=INF; memset (Vis1,false,sizeof(VIS1)); DFS (0,0,0); printf ("%lld\n", MIN); }    return 0;}

HDU 3768 (spfa+ violence)

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.