nyoj 城市平乱(Dijkstra)
裸
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<string> 6 #include<queue> 7 #include<algorithm> 8 #include<map> 9 #include<iomanip> 10 #include<climits> 11 #include<string.h> 12 #include<cmath> 13 #include<stdlib.h> 14 #include<vector> 15 #include<stack> 16 #include<set> 17 using namespace std; 18 #define INF 1000000007 19 #define MAXN 2010 20 #define Mod 1000007 21 #define N 100010 22 #define NN 30 23 #define sigma_size 3 24 const int MAX = 1000100; 25 const int maxn = 6e5 + 10; 26 using namespace std; 27 typedef long long LL; 28 29 bool vis[1010]; 30 int G[1010][1010]; 31 int army[1100]; 32 int dist[1010]; 33 int n, m, p, q; 34 int u, v, w, tmp; 35 36 void Dijkstra(int src) 37 { 38 int min, pos; 39 for (int i = 1; i <= m; ++i) 40 dist[i] = G[src][i]; 41 dist[src] = 0; 42 vis[src] = true; 43 for (int i = 1; i <= m; ++i) { 44 min = MAX; 45 for (int j = 1; j <= m; ++j) { 46 if (dist[j] < min && !vis[j]) { 47 min = dist[j]; 48 pos = j; 49 } 50 } 51 if (min == MAX) return; 52 vis[pos] = true; 53 for (int j = 1; j <= m; ++j){ 54 if (!vis[j] && dist[pos] + G[pos][j] < dist[j]) { 55 dist[j] = dist[pos] + G[pos][j]; 56 } 57 } 58 } 59 } 60 61 void run() 62 { 63 scanf("%d%d%d%d", &n, &m, &p, &q); 64 memset(army,0,sizeof(army)); 65 memset(vis,0,sizeof(vis)); 66 memset(dist,0,sizeof(dist)); 67 for (int i = 0; i <= m; ++i) { 68 for (int j = 0; j <= m; ++j) { 69 G[i][j] = G[j][i] = MAX; 70 } 71 } 72 for (int i = 0; i < n; ++i) { 73 scanf("%d", &tmp); 74 army[tmp] = 1; 75 } 76 for (int i = 1; i <= p; ++i) { 77 scanf("%d%d%d", &u, &v, &w); 78 if (G[u][v] > w) G[u][v] = G[v][u] = w; 79 } 80 Dijkstra(q); 81 /*for (int i = 1; i <= m; ++i) 82 cout << dist[i] << " "; 83 cout << endl;*/ 84 int mmax = MAX; 85 for (int i = 1; i <= m; ++i) { 86 if (mmax > dist[i] && army[i]) 87 mmax = dist[i]; 88 } 89 printf("%d\n",mmax); 90 //system("pause"); 91 } 92 93 int main() 94 { 95 int T; 96 scanf("%d",&T); 97 while (T--) 98 run(); 99 return 0; 100 }