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 ;
}

hdu 3768(spfa+暴力)的更多相关文章

  1. 【BZOJ】1295: [SCOI2009]最长距离(spfa+暴力)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1295 咳咳..此题我不会做啊..一开始认为是多源,可是有移除物品的操作,所以不行. 此题的思想很巧妙 ...

  2. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  3. HDU 5522 Numbers 暴力

    Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...

  4. hdu 5077 NAND(暴力打表)

    题目链接:hdu 5077 NAND 题目大意:Xiaoqiang要写一个编码程序,然后依据x1,x2,x3的值构造出8个字符.如今给定要求生成的8个字符.问 说Xiaoqiang最少要写多少行代码. ...

  5. hdu 5726 GCD 暴力倍增rmq

    GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...

  6. hdu 4291(矩阵+暴力求循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...

  7. hdu 4568(SPFA预处理+TSP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568 思路:先用spfa预处理出宝藏与宝藏之间的最短距离,宝藏到边界的最短距离,然后就是经典的求TSP ...

  8. [BZOJ 1295][SCOI2009]最长距离(SPFA+暴力)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1295 分析:很巧妙的一道spfa从搜索的角度是搜索在所有1中搜索删除哪T个1,对整个图询问,这 ...

  9. Shopping(hdu 3768)

    题意:给你一个无向图,求从0号点开始遍历所有的指定点再回到0号点的最短路径 #include<cstdio> #include<iostream> #include<qu ...

随机推荐

  1. 修改freemarker的ftl时,不重启tomcat的办法(使用了springMVC)

    一.在使用Freemarker 时,需要在spring-mvc.xml 配置文件中作如下配置: <!-- 配置freeMarker的模板路径 --> <bean id="f ...

  2. libevent显式调用事件处理

    ) { SearchAcceptListen2(p_ev_arg->listen_fd,,&notify_event,base); event_base_loop(base, EVLOO ...

  3. htm,html,xhtml,xml,xsl,dhtml,shtm和shtml的区分

    介绍一下htm,html,xhtml,xml,shtml的区分,以下内容来自百度后的知识整理. HTML和htm: HTML(Hypertext Markup Language)超文本传输语言,是ww ...

  4. 在vue-cli创建的项目里配置scss

    第一步,gitbash进入到项目目录 npm install node-sass --save-dev npm install sass-loader --save-dev 第二步:打开webpack ...

  5. 【bzoj2152】聪聪可可 树的点分治

    题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...

  6. P2161 [SHOI2009]会场预约

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  7. BZOJ 3224 Tyvj 1728 普通平衡树 | Splay 板子+SPlay详细讲解

    下面给出Splay的实现方法(复杂度证明什么的知道是 nlogn 就可以啦) 首先对于一颗可爱的二叉查找树,是不能保证最坏nlogn的复杂度(可以想象把一个升序序列插入) (二叉查找树保证左子树元素大 ...

  8. 近期对于windows服务的理解

    1.APP.config的作用   在开发环境下时,根目录下的APP.config里面会填写一些参数之类的.当生成之后,这些参数将会被自动生成在*.exe文件目录中.如图: 其中,.exe文件为Win ...

  9. HDU 3446 有贪心思想的01背包

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  10. [hdu 3949]线性基+高斯消元

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 一开始给做出来的线性基wa了很久,最后加了一步高斯消元就过了. 之所以可以这样做,证明如下. 首 ...