rt,样例都没过,晚上脑子都不好使了。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
char *p1,*p2,buf[1<<20+5];
inline int read(){
int x=0;
char c=gc();
while(!isdigit(c)){
c=gc();
}while(isdigit(c)){
x=x*10+(c^48);
c=gc();
}
return x;
}
const int N = 50005, M = 200005;
int n, m;
int a[15];
int h[N], e[M], ne[M], w[M], idx;
int res;
int dist[N][15];
bool st[N], z[15];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
void spfa(int S)
{
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
queue<int> q;
q.push(S);
st[S] = 1;
dist[S][S] = 0;
while (!q.empty())
{
int t = q.front();
q.pop();
st[t] = 0;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[t][S] + w[i] < dist[j][S])
{
dist[j][S] = dist[t][S] + w[i];
if (!st[j])
{
q.push(j);
st[j] = 1;
}
}
}
}
}
void dfs(int s, int p, int c)
{
if (s >= 5)
{
res = min(res, c);
return;
}
for (int i = 1; i <= 5; i ++ )
{
if (z[i]) continue;
z[i] = 1;
dfs(s + 1, i, c + dist[a[i]][p]);
z[i] = 0;
}
}
int main()
{
memset(h, -1, sizeof h);
n = read(), m = read();
for (int i = 1; i <= 5; i ++ ) a[i] = read();
for (int i = 1; i <= m; i ++ )
{
int x, y, t;
x = read(), y = read(), t = read();
add(x, y, t);
add(y, x, t);
}
a[6] = 1;
spfa(6), spfa(1), spfa(2), spfa(3), spfa(4), spfa(5);
for (int i = 1; i <= 5; i ++ )
{
z[i] = 1;
dfs(1, i, dist[a[i]][6]);
z[i] = 0;
}
printf("%d\n", res);
return 0;
}