Newcoder 109 E.求长度(spfa+状压)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/V5ZSQ/article/details/83151514

Description

给定一幅 n n 个点 m m 条边的图和 S S 个一定要经过的点,问从 0 0 号点出发,经过这 S S 个点再回到 0 0 号点的最短路径长度是多少。

Input

第一行一个整数 T T 表示数据组数

对于每组数据,第一行两个整数 n , m n,m 表示点数和边数

接下来 m m 行,每行三个整数 x , y , z x, y, z 表示 x y xy 之间有一条长度为 c c 的双向边

接下来一个整数 S S

接下来 S S 行每行一个整数表示一定要经过的点

数据保证有解

( 1 T 2 , 1 n , m 1 0 5 , 0 < x , y < n , 0 z 1000 , S 10 ) (1\le T\le 2,1\le n,m\le 10^5,0<x,y<n,0\le z\le 1000,S\le 10)

Output

T T 行,每行一个整数表示答案。

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

Solution

首先分别以这 s s 个点以及 0 0 点为起点跑一遍 s p f a spfa 得到这 s + 1 s+1 个点到其他点的最短距离,不妨设这 s s 个点为 a 1 , . . . , a s a_1,...,a_s ,而 a 0 = 0 a_0=0 ,以 d p [ S ] [ i ] dp[S][i] 表示已经经过 S S 状态的点(用 s + 1 s+1 01 01 位表示这 s + 1 s+1 个点的经过状态)且当前在 a i a_i 点的最短路径长度,那么枚举下一个要经过的目标点 a j a_j ,使得 S S 中第 j j 位为 0 0 ,进而有转移
d p [ S + 2 j ] [ j ] = m i n ( d p [ S + 2 j ] [ j ] , d p [ S ] [ i ] + d i s ( a i , a j ) ) dp[S+2^j][j]=min(dp[S+2^j][j],dp[S][i]+dis(a_i,a_j))
最后 m i n ( d p [ 2 s + 1 1 ] [ i ] + d i s ( a i , 0 ) ) min(dp[2^{s+1}-1][i]+dis(a_i,0)) 即为答案,时间复杂度 O ( s 2 2 s ) O(s^2\cdot 2^s)

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
#define maxn 100005
#define INF 2e9
struct edge
{
    int to,next,cost;
}g[2*maxn];
int head[maxn],tol;
int dis[11][maxn];
bool vis[maxn];
int T,n,m,s,a[11],dp[2200][11];
void init()
{
    memset(head,-1,sizeof(head));
    tol=0;
}
void add(int u,int v,int c)
{
    g[tol].cost=c;
    g[tol].to=v;
    g[tol].next=head[u];
    head[u]=tol++;
}
void spfa(int s,int *dis)
{
    memset(vis,0,sizeof(vis));
    queue<int>que;
    for(int i=0;i<n;i++)dis[i]=INF;
    dis[s]=0,vis[s]=1;
    que.push(s);
    while(!que.empty())
    {
        int u=que.front();que.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=g[i].next)
        {
            int v=g[i].to,c=g[i].cost;
            if(dis[v]>dis[u]+c)
            {
                dis[v]=dis[u]+c;
                if(!vis[v])vis[v]=1,que.push(v);
            }
        }
    }
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		init();
		while(m--)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w),add(v,u,w);
		}
		scanf("%d",&s);
		for(int i=1;i<=s;i++)
		{
			scanf("%d",&a[i]);
			spfa(a[i],dis[i]);
		}
		s++;
		a[0]=0;
		spfa(0,dis[0]);
		int N=1<<s;
		for(int i=0;i<N;i++)
			for(int j=0;j<s;j++)	
				dp[i][j]=INF;
		dp[0][0]=0;
		for(int S=0;S<N;S++)
			for(int i=0;i<s;i++)
				if(dp[S][i]!=INF)
					for(int j=0;j<s;j++)
						if(!((S>>j)&1))
							dp[S^(1<<j)][j]=min(dp[S^(1<<j)][j],dp[S][i]+dis[i][a[j]]);
		int ans=INF;
		for(int i=0;i<s;i++)ans=min(ans,dp[N-1][i]+dis[i][0]);
		printf("%d\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/V5ZSQ/article/details/83151514
109