[Topic link]
Https://codeforces.com/contest/449/problem/B
Algorithm
Shortest circuit
Time complexity: O (N ^ 2)
Code
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e5 +Ten;Const intINF =2e9;intN, M, K;intMARK[MAXN];Long LongDIST[MAXN];BOOLInq[maxn];vector< pair<int,int> >g[maxn];template<typename t> InlinevoidChkmax (T &x,t y) {x =Max (x, y);} Template<typename t> InlinevoidChkmin (T &x,t y) {x =min (x, y);} Template<typename t> InlinevoidRead (T &x) {T F=1; x =0; Charc =GetChar (); for(;!isdigit (c); c = GetChar ())if(c = ='-') F =-F; for(; IsDigit (c); c = GetChar ()) x = (x <<3) + (x <<1) + C-'0'; X*=F;}intMain () {read (n); read (m); Read (k); for(inti =1; I <= m; i++) { intu, V, W; Read (u); Read (v); Read (w); G[u].push_back (Make_pair (v,w)); G[v].push_back (Make_pair (u,w)); } Queue<int>Q; for(inti =1; I <= N; i++) {Dist[i]=INF; Mark[i]=0; Inq[i]=false; } dist[1] =0; Q.push (1); inq[1] =true; for(inti =1; I <= K; i++) { intu, W; Read (u); Read (w); Mark[u]=1; if(W <Dist[u]) {Dist[u]=W; if(!Inq[u]) {Inq[u]=true; Q.push (U); } } } while(!Q.empty ()) { intU =Q.front (); Q.pop (); Inq[u]=false; for(Unsigned i =0; I < g[u].size (); i++) { intv = g[u][i].first, w =G[u][i].second; if(Dist[u] + w <= dist[v] && mark[v]) mark[v] =0; if(Dist[u] + W <Dist[v]) {Dist[v]= Dist[u] +W; if(!Inq[v]) {Inq[v]=true; Q.push (v); } } } } for(inti =1; I <= N; i++) K-=Mark[i]; printf ("%d\n", K); return 0; }
[Codeforces 449B] Jzzhu and Cities