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
 
题意:在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 = ;
const LL N = ;
struct Edge{
LL v,next;
LL w;
}edge[*N];
struct City{
LL id,idx;
}c[];
LL head[N];
LL tot,n,m,Q;
bool vis[N];
LL low[N];
LL dis[][];
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,-,sizeof(head));
tot = ;
for(LL i=;i<;i++){
for(LL j=;j<;j++){
dis[i][j] = INF;
}
}
}
void spfa(LL pos){
for(LL i=;i<n;i++){
low[i] = INF;
vis[i] = false;
}
low[pos] = ;
queue<LL>q;
q.push(pos);
while(!q.empty()){
LL u = q.front();
q.pop();
vis[u] = false; ///!!!!!!!!!!!!!!!!!!
for(LL k=head[u];k!=-;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[];
void dfs(LL u,LL step,LL ans){
vis1[u] = true;
if(step==Q){
MIN = min(MIN,ans+dis[u][]);
return;
}
for(LL i=;i<=Q;i++){
if(!vis1[i]&&dis[u][i]<INF){
dfs(i,step+,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=;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[].id = ;
c[].idx = ;
for(LL i=;i<=Q;i++){
scanf("%lld",&c[i].id);
c[i].idx = i;
}
for(LL i=;i<=Q;i++){
spfa(c[i].id);
for(LL j=;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(,,);
printf("%lld\n",MIN);
}
return ;
}

最新文章

  1. 微信小程序之基础简介
  2. 蘑菇街TeamTalk编译连接过程中遇到的问题及解决方法(iOS)
  3. meteor报错之:MongoDB had an unspecified uncaught exception.
  4. python画柱状图并且输出到html文件
  5. 转: 从Mysql某一表中随机读取n条数据的SQL查询语句
  6. JavaScript 获取当月天数
  7. 网址测速JS
  8. RenderPartial RenderAction Partial Action
  9. python IOError: invalid mode (&#39;r&#39;) or filename
  10. ios数据存储——数据库:SQlite3以及第三方库FMDB
  11. 原生js实现删除class和添加class
  12. Dom捕捉事件和冒泡事件-原理与demo测试
  13. [daily] 内存越界的分析与定位
  14. 【嵌入式】使用Cross Toolchain构建交叉工具链
  15. rpc框架thrift
  16. Java GUI画圆。
  17. 大数据学习系列之—HBASE
  18. live555源码分析
  19. 网络连接和初始HTTP请求
  20. C/C++ 错误笔记-在给结构体中的指针赋值时,要注意该指针是否已指向内存空间

热门文章

  1. postman工具中如何提取接口的返回值
  2. 【bzoj1999】[Noip2007]Core树网的核 树的直径+双指针法+单调队列
  3. 51nod 1819 黑白树V2(树链剖分)
  4. 【题解】ZJOI2017仙人掌
  5. 洛谷 P2218 [HAOI2007]覆盖问题 解题报告
  6. git使用笔记(六)github
  7. [HNOI2003]消防局的设立 (贪心)
  8. nodejs npm insttall 带不带-g这个参数的区别
  9. Java中文乱码问题(转)
  10. ubuntu12.04回归到经典的gnome界面