1. 程式人生 > >[Codeforces 449B] Jzzhu and Cities

[Codeforces 449B] Jzzhu and Cities

color class n) const make contest void inline lse

[題目鏈接]

https://codeforces.com/contest/449/problem/B

[算法]

最短路

時間復雜度 : O(N ^ 2)

[代碼]

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int INF = 2e9;

int n , m , k;
int mark[MAXN];
long long dist[MAXN];
bool inq[MAXN];
vector< pair<int
,int> > G[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if
(c == -) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 0; x *= f; } int main() { read(n); read(m); read(k); for (int i = 1; i <= m; i++) { int u , 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 (int i = 1; i <= n; i++) { dist[i] = INF; mark[i] = 0; inq[i] = false; } dist[1] = 0; q.push(1); inq[1] = true; for (int i = 1; i <= k; i++) { int u , 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()) { int u = q.front(); q.pop(); inq[u] = false; for (unsigned i = 0; i < G[u].size(); i++) { int v = 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 (int i = 1; i <= n; i++) k -= mark[i]; printf("%d\n",k); return 0; }

[Codeforces 449B] Jzzhu and Cities