[GXOI/GZOI2019旅行者]

#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long LL;
using namespace std;
const int maxn = 1e5+6,inf = 1e9;
int T,n,m,k;
int city[maxn],f[maxn*5],t[maxn*5],l[maxn*5];
struct node{
    int id;LL dis;
    bool operator < (const node &C)const
    {
        return dis > C.dis;
    }
};
struct qwq{
    int cnt,head[maxn],to[maxn*5],next[maxn*5],color[maxn];
    LL dis[maxn],len[maxn*5];
    bool vis[maxn];
    priority_queue <node> q;
    void clear()
    {
        cnt = 0;
        for(int i=0;i<=n;i++)
        {
            head[i] = 0;
            vis[i] = 0;
        }
    }
    inline void add(int u,int v,int w)
    {
        cnt ++;
        to[cnt] = v;
        len[cnt] = w;
        next[cnt] = head[u];
        head[u] = cnt;
    }
    void dijkstra()
    {
        for(int i=1;i<=n;i++) dis[i] = inf;
        for(int i=1;i<=k;i++)
        {
            dis[city[i]] = 0;
            color[city[i]] = city[i];
            q.push((node){city[i],0});
        }
        while(!q.empty())
        {
            node M = q.top();q.pop();
            int u = M.id;
            if(vis[u]) continue;
            vis[u] = 1;
            for(int i=head[u];i;i=next[i])
            {
                int v = to[i];
                if(dis[v] > dis[u] + len[i])
                {
                    dis[v] = dis[u] + len[i];
                    color[v] = color[u];
                    q.push((node){v,dis[v]});
                }
            }
        }
    }
}Dij[2];

template <typename C>
inline void read(C &x)
{
    x = 0;int f = 1; char c;
    while(!isdigit(c = getchar()))
        if(c == '-') f = -1;
    while(isdigit(c))
        x = (x << 3) + (x << 1) + (c & 15),c = getchar();
    x *= f;
}

int main()
{
    read(T);
    while(T --)
    {
        LL ans = inf;
        Dij[0].clear();Dij[1].clear();
        read(n),read(m),read(k);
        for(int i=1;i<=m;i++)
        {
            read(f[i]),read(t[i]),read(l[i]);
            Dij[0].add(f[i],t[i],l[i]);
            Dij[1].add(t[i],f[i],l[i]);
        }
        for(int i=1;i<=k;i++) read(city[i]);
        Dij[0].dijkstra();Dij[1].dijkstra();
        for(int i=1;i<=m;i++)
        {
            if(Dij[0].color[f[i]] != Dij[1].color[t[i]] && Dij[0].color[f[i]] && Dij[1].color[t[i]])
                ans = min(ans,Dij[0].dis[f[i]]+l[i]+Dij[1].dis[t[i]]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/-Wind-/p/11833925.html