City insurgency
-
Describe
-
The general South commanded N troops, which were stationed in n different cities.
He is using these N troops to maintain the law and order of M cities, the M cities numbered from 1 to M respectively.
Now, the handyman advisers told the south General, the city of K riots, South General from all the troops sent a unit along the nearest road to the riot city of insurgency.
Now that you know the time required to march between any of the two cities, you, as the most powerful programmer in the South, ask you to write a program to tell the first squad of the Southern general how long it will take to reach the rebel city.
Note that there may be more than one route between two cities.
-
-
Input
-
The
-
first line enters an integer t, which represents the number of groups of test data. (T<20)
The first line of each set of test data is four integers n,m,p,q (1<=n<=100,n<=m<=1000,m-1<=p<=100000) where N represents the number of troops, M represents the number of cities, and P represents the number of roads between cities, Q indicates the city number where the riot occurred.
The subsequent line is n integers, indicating the number of the city in which the troop is located.
After the P line, each line has three positive integers, a,b,t (1<=a,b<=m,1<=t<=100), indicating the road between A and b if the march takes a T
The data guarantee that the city of riots is accessible.
-
-
Output
-
-
for each set of test data, the time when the first troop reached the rebel city was output. One row per set of outputs
-
-
Sample input
-
-
-
-
Sample output
-
4
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 Const intinf=0x3f3f3f3f;7 intM,n;8 intg[1005][1005];9 BOOLvis[1005];Ten intd[1005]; One A voidInit () - { -memset (G,inf,sizeof(G)); thememset (Vis,0,sizeof(Vis)); - } - - voidDijkstraints) + { - intu,i,j,v; + for(i=1; i<=m;i++) Ad[i]=G[s][i]; atd[s]=0; -vis[s]=1; - for(i=1; i<m;i++) - { - inttmp=inf; - for(j=1; j<=m;j++) in if(d[j]<tmp&&!Vis[j]) - { totmp=D[j]; +u=J; - } thevis[u]=1; * for(v=1; v<=m;v++) $ {Panax Notoginseng if(!vis[v]&&d[u]+g[u][v]<D[v]) -d[v]=d[u]+G[u][v]; the } + } A } the + intMain () - { $ intt,p,q,i,u,v,w; $ inta[ the]; -scanf"%d",&t); - while(t--) the { - init ();Wuyiscanf"%d%d%d%d",&n,&m,&p,&q); the for(i=0; i<n;i++) -scanf"%d",&a[i]); Wu for(i=0; i<p;i++) - { Aboutscanf"%d%d%d",&u,&v,&W); $g[u][v]=g[v][u]=min (g[u][v],w); - } - Dijkstra (q); - intans=inf; A for(i=0; i<n;i++) + if(d[a[i]]<ans) theans=D[a[i]]; -printf"%d\n", ans); $ } the return 0; the}
Nyoj 115 City Insurgency (Dijkstra)