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. Python-爬取"我去图书馆"座位编码

    原文地址:http://fanjiajia.cn/2018/11/22/Python-%E7%88%AC%E5%8F%96%E2%80%9D%E6%88%91%E5%8E%BB%E5%9B%BE%E4 ...

  2. Jquery 跨域请求JSON数据问题

    制作网站时,我们有时候为了方便快捷会调用别人写好的API接口,或者是调用一些免费的API接口获得JSON数据.比如天气,农历,网站备案信息查询等. 但是,这些API接口都是别人自己服务器上的,我们要调 ...

  3. PAT 1045 快速排序

    https://pintia.cn/problem-sets/994805260223102976/problems/994805278589960192 著名的快速排序算法里有一个经典的划分过程:我 ...

  4. 手动修改PHP页面返回状态码

    <?php //比如当前页面要返回404状态码 header("HTTP/1.1 404 Not Found"); header("Status: 404 Not ...

  5. 除了基本类型,其余类型基本上大部分new出来 java.sql 包下面要不需要new

    除了基本类型,其余类型基本上大部分new出来 java.sql 包下面要不需要new

  6. [bzoj5285] [HNOI2018]寻宝游戏

    Description 某大学每年都会有一次Mystery Hunt的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得这一年出题的机会. 作为新生的你,对这个活动非常感兴趣. ...

  7. 12.25模拟赛T1

    可以区间dp,但是复杂度太高. 所以应该是贪心,怎么贪心呢? 这种题目,最好还是手玩找一些规律. 可以发现,由于保证可以m次填完,所以颜色之间没有相互包含关系. 比较像分治的模型. 所以考虑拿到一个区 ...

  8. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  9. oracle查看字符集和修改字符集

    oracle查看字符集和修改字符集 : 查看数据库服务器的字符集: select userenv('language') from dual ; 登陆用dba: 停掉数据库 : shutdown im ...

  10. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿 ...