hdu 3768(spfa+暴力)
2016-07-21 17:33
246 查看
Shopping
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 758 Accepted Submission(s): 254
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
4
[align=left]Source[/align]
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 ; LL tot,n,m,Q; bool vis ; LL low ; 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; }
相关文章推荐
- Android data目录读写文件
- 用户体验要素
- NS2的整体实现
- Android学习之文件存储读取
- ubuntu 各个目录的作用
- ABAP/HANA Connectivity via Secondary Database Connection
- MAT使用的几张图例技巧
- JavaScript中的字符串使用
- Mac上编译boost
- poython正则表达式
- Android开发,建立具有键值对(key-value)关系的spinner
- js 日期格式化
- 动态规划入门step-by-step
- iOS开发微信分享图片32K限制解决方案
- 数据库中sum和count的区别
- CentOS7安装Memcached和PHP扩展
- PHP函数in_array、array_key_exists和isset效率分析
- 设置ViewPager不可左右滑动
- 退火算法
- 日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个。以下为4个嫌疑犯的供词