用 r来保存John他妈妈告诉他的路线,首先要保证这条路必须通,然后再从r[2](第二个点)求到n的最短路,
最后输出结果就是了.
#include <iostream>
#include <queue>
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxen = 30010;
const int maxn = 1010;
struct node{
int i, v;
node(int ii = 0, int vv = 0):i(ii), v(vv){}
bool operator<(const node &rhs)const{
return v > rhs.v;
}
};
int dis[maxn],g[maxn][maxn], r[maxn], n, m, k;
bool vis[maxn];
int dijkstra(int s, int e){
memset(vis, 0, sizeof(vis));
memset(dis, 0X20, sizeof(dis));
priority_queue<node> q;
q.push(node(s, 0));
dis[s] = g[1][s];
vis[1] = 1;
while(q.size()){
node t = q.top();
q.pop();
if(vis[t.i])continue;
if(t.i == e)break;
vis[t.i] = 1;
for(int i = 1; i <= n; ++i){
if(g[t.i][i] && dis[i] > dis[t.i] + g[t.i][i] && !vis[i]){
dis[i] = dis[t.i] + g[t.i][i];
q.push(node(i, dis[i]));
}
}
}
return dis[e];
}
int main(){
int T, cas = 1;
scanf("%d", &T);
while(T--){
memset(g, 0, sizeof(g));
int eidx = 0;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; ++i){
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
g[u][v] = g[v][u] = w;
}
int s, e = n, pre = 0, sum = 0, f = 0;
scanf("%d", &k);
for(int i = 1; i <= k; ++i){
scanf("%d", &r[i]);
}
for(int i = 1; i <= k - 1; ++i){
if(!g[r[i]][r[i + 1]]){
f = 1;
break;
}
sum += g[r[i]][r[i + 1]];
}
if(f == 0){
printf("TEST %d Y %d\n", cas++ ,sum - dijkstra(r[2], e));
}else{
printf("TEST %d N\n", cas++);
}
}
return 0;
}