Topic links
1 /*2 name:nyoj-115-City Insurgency3 Copyright:4 Author:5 DATE:2018/4/25 17:28:066 Description:7 Dijkstra Template Questions8 enumeration from the city where the troops are located to the rebel city to take the minimum value9 */Ten#include <iostream> One#include <cstdio> A#include <cstring> -#include <cmath> - using namespacestd; the Const intMAXN =1005, inf=0x3f3f3f3f; - intDIS[MAXN], G[MAXN][MAXN], N, M, P, Q, ARMY[MAXN], SRC; - BOOLV[MAXN]; - + voidDijkstra () { - intN =M; + for(intI=1; i<=n; i++) Dis[i] =INF; ADIS[SRC] =0; atmemset (V,0,sizeof(v)); - for(intI=1; i<=n; ++i) { - intMark =-1, mindis=INF; - for(intj=1; j<=n; J + +) { - if(!v[j] && dis[j]<Mindis) { -Mindis =Dis[j]; inMark =J; - } to } +V[mark] =1; - for(intj=1; j<=n; J + +) { the if(!V[j]) { *Dis[j] = min (Dis[j], Dis[mark] +g[mark][j]); $ }Panax Notoginseng } - } the } + intMain () A { the intT; +Cin>>T; - while(t--) { $Memset (G,0x3f,sizeof(g)); $memset (Army,0,sizeof(Army)); -Cin>>n>>m>>p>>Q; - for(intI=0; i<n; i++) { theCin>>Army[i]; - }Wuyi for(intI=0; i<p; i++) { the intx, y, cost; -Cin>>x>>y>>Cost ; Wu if(G[x][y] < cost)Continue; -G[x][y] =Cost ; AboutG[Y][X] =Cost ; $ } - intMindis =0x3f3f3f3f; - for(intI=0; i<n; i++) { -memset (DIS,0,sizeof(DIS)); Asrc =Army[i]; + Dijkstra (); theMindis =min (Mindis, dis[q]); - } $cout<<mindis<<Endl; the } the return 0; the}
nyoj-115-City insurgency (Dijkstra algorithm)