Shopping
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 758 Accepted Submission(s): 254
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
题意:在100000个点里面选择 <=10个点,然后判断从 0到每个点然后回到 0 所需的最小距离。
题解:坑惨了,spfa的vis数组每次进队列时要赋值为 false....然后就是 spfa+暴力了..
#include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <algorithm> #include <math.h> using namespace std; typedef long long LL; const LL INF = 999999999999; const LL N = 100005; struct Edge{ LL v,next; LL w; }edge[2*N]; struct City{ LL id,idx; }c[20]; LL head[N]; LL tot,n,m,Q; bool vis[N]; LL low[N]; LL dis[15][15]; LL MIN ; void addEdge(LL u,LL v,LL w,LL &k){ edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++; } void init(){ memset(head,-1,sizeof(head)); tot = 0; for(LL i=0;i<15;i++){ for(LL j=0;j<15;j++){ dis[i][j] = INF; } } } void spfa(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); } } } } } bool vis1[20]; void dfs(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; } } } int main(){ int tcase; 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; }