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. Linux TCP协议使用的变量

    Linux /proc/sys/net/ipv4/* 变量 TCP变量:somaxconn - INTEGER    listen()的backlog参数的上限,在用户态为SOMAXCONN.默认是1 ...

  2. SSH面试集锦——不看后悔哦!

    1.        谈谈你mvc的理解 MVC是Model-View-Controler的简称.即模型-视图-控制器.MVC是一种设计模式,它强制性的把应用程序的输入.处理和输出分开. MVC中的模型 ...

  3. 【bzoj2957】楼房重建 分块+二分查找

    题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子.为了简化问题,我们考虑这些事件发生在一个二 ...

  4. [CF1019A]Elections

    题目大意:有$n$个人,$m$个政党,每个人都想投一个政党,但可以用一定的钱让他选你想让他选的政党. 现在要$1$号政党获胜,获胜的条件是:票数严格大于其他所有政党.求最小代价 题解:暴力枚举其他政党 ...

  5. 洛谷P1339 热浪

    P1339 热浪 529通过 1.3K提交 题目提供者yeszy 标签图论福建省历届夏令营 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 求助...为什么是未知错误… 求修正,貌似死循环 第 ...

  6. power designer 绘制E-R 图

    总体概括:本篇主要先介绍E-R图的一些基本概念,然后介绍怎么绘制E-R图,特别是用power designer 的反向工程怎么把表中对字段的注释也展示出来. 1.E-R图的基本概念: E-R图就是en ...

  7. 解决在极光推送的时候会出现一个 JPush提示:缺少统计代码

    <span style="font-size:14px;"> @Override protected void onResume(){ super.onResume() ...

  8. hive的体系架构及安装

    1,什么是Hive? Hive是能够用类SQL的方式操作HDFS里面数据一个数据仓库的框架,这个类SQL我们称之为HQL(Hive Query Language) 2,什么是数据仓库? 存放数据的地方 ...

  9. DOM创建和删除节点

    一.创建节点 3步 1.创建空元素对象: var newElem=document.createElement("标签名"); 例如:var a=document.createEl ...

  10. python的tuple()

    描述 Python 元组 tuple() 函数将列表转换为元组. 语法 tuple()方法语法: tuple( seq ) 参数 seq -- 要转换为元组的序列. 返回值 返回元组. 实例 以下实例 ...