Shopping

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 632 Accepted Submission(s): 209

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

University of Waterloo Local Contest 2010.07.10

思路:给你一个无向图,求从0号点开始遍历所有的指定点再回到0号点的最短路径,指定点只有10个,所以先spfa后dfs即可;

#include <iostream>
#include <set>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std; const int MAX=101000; const int INF = 0x3f3f3f3f; typedef struct node
{
int v;
int w;
int next;
}Edge;
int Head[MAX];
Edge Line[MAX*2];
int top;
int n,m,s,Map[15][15];
int dis[12][MAX],num[15];
bool vis[MAX];
bool vist[15];
void AddEdge(int u,int v,int w)//由于点的个数比较多,所以采用领接表的形式
{
Line[top].v=v;
Line[top].w=w;
Line[top].next=Head[u];
Head[u]=top++;
}
void spfa(int s,int site)//spfa计算出要去的商店之间的距离
{
memset(vis,false,sizeof(false));
for(int i=0;i<=n;i++)
{
dis[site][i]=INF;
}
dis[site][s]=0;
vis[s]=true;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=Head[u];i!=-1;i=Line[i].next)
{
if(dis[site][Line[i].v]>dis[site][u]+Line[i].w)
{
dis[site][Line[i].v]=dis[site][u]+Line[i].w;
if(!vis[Line[i].v])
{
Q.push(Line[i].v);
vis[Line[i].v]=true;
}
}
}
vis[u]=false;
}
}
int dfs(int st,int sum,int num)//搜索出最小的距离
{
vist[st]=true;
if(st==0)
{
if(num==s+1)
{
return sum;
}
else
{
return INF;
}
}
int ans=INF;
for(int i=0;i<=s;i++)
{
if(!vist[i]||i==0)
{
ans=min(ans,dfs(i,sum+Map[st][i],num+1));
}
}
vist[st]=false;
return ans;
} int main()
{
int T;
int u,v,w;
scanf("%d",&T);
while(T--)
{
memset(Head,-1,sizeof(Head));
scanf("%d %d",&n,&m);
top=0;
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&u,&v,&w);
AddEdge(u,v,w);
AddEdge(v,u,w);
}
num[0]=0;
spfa(0,0);
scanf("%d",&s);
for(int i=1;i<=s;i++)
{
scanf("%d",&num[i]);
spfa(num[i],i);
}
memset(Map,0,sizeof(Map));
for(int i=0;i<=s;i++)//离散化,由于要去的点比较少,可以将要去的点重新进行建图
{
for(int j=0;j<=s;j++)
{
Map[i][j]=dis[i][num[j]];
}
}
int ans=INF;
for(int i=1;i<=s;i++)
{
memset(vist,false,sizeof(vist));
vist[0]=true;
ans=min(ans,dfs(i,Map[0][i],1));
}
printf("%d\n",ans);
}
return 0;
}

Shopping(SPFA+DFS HDU3768)的更多相关文章

  1. 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)

    题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...

  2. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  3. 【PAT甲级】1018 Public Bike Management (30 分)(SPFA,DFS)

    题意: 输入四个正整数C,N,S,M(c<=100,n<=500),分别表示每个自行车站的最大容量,车站个数,此次行动的终点站以及接下来的M行输入即通路.接下来输入一行N个正整数表示每个自 ...

  4. POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9703   Accepted: 3299 ...

  5. PAT天梯赛练习题 L3-011. 直捣黄龙(多关键字SPFA+DFS)

    L3-011. 直捣黄龙 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题是一部战争大片 —— 你需要从己方大本营出发,一路 ...

  6. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  7. POJ 3411 Paid Roads(SPFA || DFS)

    题目链接 题意 : 要从1城市到n城市,求最短路是多少,从a城市到达b城市的路程,如果你到过c城市,则需要走p,否则走r长. 思路 : 因为可以来回走,所以不能用单纯的最短路,可以用二维SPFA,状态 ...

  8. hdu1428之spfa+dfs

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)

    Key Vertex Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

随机推荐

  1. web app 自适应 弹性布局之rem

    关于rem,主要参考文档 1.腾讯ISUX (http://isux.tencent.com/web-app-rem.html) 2.http://www.w3cplus.com/css3/defin ...

  2. Singlton设计模式

    单例定义: 确保一个类只有一个实例,并提供全局访问点. 适用场景: 1.) 当系统中某个类必须仅有一个实例对象,同时访问该系统的所有访问者必须访问同一个实例对象时,且该对象实例自身占用资源又不大时. ...

  3. 自动备份sqlexpress 数据库脚本

    Create PROCEDURE [dbo].[usp_BackupDatabase] @databaseName sysname,@backupPath nvarchar(255), @backup ...

  4. Java的数据转换

    Java的数据类型分为三大类,即布尔型.字符型和数值型,其中数值型又分为整型和浮点型.相对于数据类型,Java的变量类型为布尔型boolean;字符型char:整型byte.short.int.lon ...

  5. Iterator和ListIterator主要区别(转)

    Iterator和ListIterator主要区别有: 一.ListIterator有add()方法,可以向List中添加对象,而Iterator不能. 二.ListIterator和Iterator ...

  6. 源码安装zabbix

    源码安装zabbix 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.     欢迎加入:高级运维工程师之路 598432640 前言:参考网上多篇源码安装的连接,自己把安装过程丢在这 ...

  7. Codeforce Round #210 Div2

    A:对角线为k其他为0 B:利用两个相邻的数一定gcd为1和1与任何数gcd为1错k个位就行了 C:不会做操蛋,好像是因为上一层的始终小于下一层的 好吧C又研究了一下,是个贪心题,不符合的情况先科不考 ...

  8. UVa10025-The ? 1 ? 2 ? ... ? n = k problem

    分析:因为数字之间只有加减变换,所以-k和k是一样的,都可以当成整数来考虑,只要找到最小的n满足sum=n*(n+1)/2>=k:且sum和k同奇同偶即可,做法是用二分查找,然后在就近查找 因为 ...

  9. 使用javabeen的好处

    什么是javabeen? javaBean在MVC设计模型中是model,又称模型层, 在一般的程序中,我们称它为数据层, 就是用来设置数据的属性和一些行为,然后提供获取属性和设置属性的get/set ...

  10. BackgroundWorker的使用

    一个程序中需要进行大量的运算,并且需要在运算过程中支持用户一定的交互,为了获得更好的用户体验,使用BackgroundWorker来完成这一功能.   基本操作: bgw.RunWorkerAsync ...