hdu 3768(spfa+暴力)
Shopping
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 758 Accepted Submission(s): 254
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.
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.
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.
4 6
0 1 1
1 2 1
2 3 1
3 0 1
0 2 5
1 3 5
3
1
2
3
#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+暴力)的更多相关文章
- 【BZOJ】1295: [SCOI2009]最长距离(spfa+暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1295 咳咳..此题我不会做啊..一开始认为是多源,可是有移除物品的操作,所以不行. 此题的思想很巧妙 ...
- HDU 5510 Bazinga 暴力匹配加剪枝
Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...
- HDU 5522 Numbers 暴力
Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5522 ...
- hdu 5077 NAND(暴力打表)
题目链接:hdu 5077 NAND 题目大意:Xiaoqiang要写一个编码程序,然后依据x1,x2,x3的值构造出8个字符.如今给定要求生成的8个字符.问 说Xiaoqiang最少要写多少行代码. ...
- hdu 5726 GCD 暴力倍增rmq
GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...
- hdu 4291(矩阵+暴力求循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...
- hdu 4568(SPFA预处理+TSP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568 思路:先用spfa预处理出宝藏与宝藏之间的最短距离,宝藏到边界的最短距离,然后就是经典的求TSP ...
- [BZOJ 1295][SCOI2009]最长距离(SPFA+暴力)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1295 分析:很巧妙的一道spfa从搜索的角度是搜索在所有1中搜索删除哪T个1,对整个图询问,这 ...
- Shopping(hdu 3768)
题意:给你一个无向图,求从0号点开始遍历所有的指定点再回到0号点的最短路径 #include<cstdio> #include<iostream> #include<qu ...
随机推荐
- html页面简单制作示例
内有表格布局,具体见 链接: https://pan.baidu.com/s/1V7IcxQ5M-iXVdlzuf8bo-A 密码: 8dp8
- 软工实践Beta冲刺(7/7)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...
- 《学习OpenCV》课后习题解答9
题目:(P126) 创建一个程序,使其读入并显示一幅图像.当用户鼠标点击图像时,获取图像对应像素的颜色值(BGR),并在图像上点击鼠标处用文本将颜色值显示出来. 解答: 本题关键是会用cvGet2D获 ...
- 【历史】- 一段关于 Unix、Linux 和 Windows 的暗黑史
“SCO在言语上变得越来越好斗,而且还拒绝展示有关诉讼的任何证据,一切都似乎在表明,SCO只不过是在那里拉虎皮做大旗地狂言乱语.但是,微软决不会轻易放弃这么可以一个利用这些狂言乱语的好机会.”2003 ...
- 【C++ troubleshooting】A case about decltype
template <typename iter_t> bool next_permutation(iter_t beg, iter_t end) { // if (beg == end | ...
- BZOJ3132 上帝造题的七分钟 【二维树状数组】
题目 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的 ...
- oracle大数据匹配处理C#
忙碌了几天写出来的oracle存储过程在作业中执行. 写的oracle存储过程如果有什么不好的地方大家指点指点. oracle存储过程其中使用到游标嵌套.if.if嵌套.数据插入表.select插入表 ...
- JavaScript使用数组拼接字符串性能如何?
传统上,字符串连接一直是js中性能最低的操作之一. view source print? 1 var text="Hello"; 2 text+=" World!&q ...
- bzoj2424 [HAOI2010]订货 dp+单调性
[HAOI2010]订货 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1311 Solved: 884[Submit][Status][Discu ...
- input 单选按钮radio 取消选中(转载)
input单选按钮: 在radio按钮中添加属性tag 0代表未被选中 HTML代码: <input name="rdo1" value="AA" ty ...