Shopping
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 511 Accepted Submission(s): 165
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.
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题意:有n个地点,m条边,给出k个地点,从点0出发,要经过全部k个地点,然后回到起点0,求最短的路线长度。思路:求出k个地点到各个点的最短路径,因为k最大只是10,所以枚举k个数的排列,分别求路线的长度。AC代码:#include <iostream> #include <cmath> #include <cstdlib> #include <cstring> #include <cstdio> #include <queue> #include <stack> #include <ctime> #include <vector> #include <algorithm> #define ll long long #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) #define eps 1e-6; using namespace std; const int INF = 1e9; const int maxn = 100005; struct Edge{ int v, w, next; }et[maxn * 10]; int dis[15][maxn], eh[maxn], shop[15], hash[maxn]; bool vis[maxn]; int n, m, k, num; void init(){ memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v, int w){ Edge e = {v, w, eh[u]}; et[num] = e; eh[u] = num++; } void spfa(int s, int id){ queue<int>Q; memset(vis, false, sizeof(vis)); for(int i = 0; i < n; i++) dis[id][i] = INF; dis[id][s] = 0; vis[s] = true; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = false; for(int i = eh[u]; i != -1; i = et[i].next) { int v = et[i].v, w = et[i].w; if(dis[id][v] > dis[id][u] + w) { dis[id][v] = dis[id][u] + w; if(!vis[v]) { vis[v] = true; Q.push(v); } } } } } int main() { int t, a, b, c; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); init(); while(m--) { scanf("%d%d%d", &a, &b, &c); add(a, b, c); add(b, a, c); } scanf("%d", &k); for(int i = 0; i < k; i++) { scanf("%d", &shop[i]); hash[shop[i]] = i; spfa(shop[i], i); } int ans = INF; do{ int sum = 0; for(int i = 0; i < k - 1; i++) sum += dis[hash[shop[i]]][shop[i + 1]]; sum += dis[hash[shop[0]]][0] + dis[hash[shop[k - 1]]][0]; if(sum < ans) ans = sum; }while(next_permutation(shop, shop + k)); printf("%d\n", ans); } return 0; }

本文介绍了一个购物路径优化问题,旨在寻找从家出发经过指定商店再返回家的最短路径。通过构建城市道路网络模型,利用SPFA算法计算最短距离,并采用枚举法遍历所有可能的商店访问顺序,最终确定最优路径。
717

被折叠的 条评论
为什么被折叠?



