HDU3768 Shopping 解題報告【SPFA預處理+dfs】
阿新 • • 發佈:2019-01-02
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
解題報告
這道題的題意是給你s個點(1<=s<=10),問你一條經過這s個點的最短路。
我們先用SPFA預處理出這s個點到其他點兩兩之間的距離用SPFA預處理出來,之後我們欺負它資料小,直接暴力算路徑就好了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=100000,S=10,inf=0x3f3f3f3f;
struct edge
{
int v,w,next;
}ed[N*10+5];
int T,n,m,tot,ans;
int head[N+5],num;
int flag[N+5],dis[S+5][N+5],a[S+5];//dis[st][v]表示a[st]到v的最短的距離
void build(int u,int v,int w)
{
ed[++num].v=v;
ed[num].w=w;
ed[num].next=head[u];
head[u]=num;
}
void SPFA(int st)
{
queue<int>q;
memset(flag,0,sizeof(flag));
for(int i=0;i<=n;i++)dis[st][i]=inf;
q.push(a[st]);flag[a[st]]=1,dis[st][a[st]]=0;
while(!q.empty())
{
int u=q.front();q.pop();
flag[u]=0;
for(int i=head[u];i!=-1;i=ed[i].next)
{
int v=ed[i].v;
if(dis[st][v]>dis[st][u]+ed[i].w)
{
dis[st][v]=dis[st][u]+ed[i].w;
if(!flag[v])
{
flag[v]=1;
q.push(v);
}
}
}
}
}
void dfs(int pos,int sum,int step)//這裡的pos是a陣列的下標
{
if(step==tot)
{
ans=min(ans,sum+dis[pos][0]);
return ;
}
for(int i=1;i<=tot;i++)
{
if(flag[a[i]])continue;
flag[a[i]]=1;
dfs(i,sum+dis[pos][a[i]],step+1);
flag[a[i]]=0;
}
}
int main()
{
for(scanf("%d",&T);T;T--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
num=0;
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
build(u,v,w);
build(v,u,w);
}
scanf("%d",&tot);
SPFA(0);
for(int i=1;i<=tot;i++)scanf("%d",&a[i]),SPFA(i);
ans=inf;
memset(flag,0,sizeof(flag));
dfs(0,0,0);
printf("%d\n",ans);
}
return 0;
}