最短路 Codeforces Round #103 (Div. 2) D. Missile Silos

 

题目传送门

 1 /*  2  最短路: 不仅扫描边,还要扫描点;点有两种情况,一种刚好在中点,即从u,v都一样,那么最后/2  3  还有一种是从u,v不一样,两种的距离都是l  4  模板错了,逗了好久:(  5 */  6 #include <cstdio>  7 #include <algorithm>  8 #include <cstring>  9 #include <cmath> 10 #include <vector> 11 #include <queue> 12 using namespace std; 13 14 const int MAXN = 1e5 + 10; 15 const int INF = 0x3f3f3f3f; 16 int d[MAXN]; 17 int cnt[MAXN]; 18 bool vis[MAXN]; 19 vector<pair<int, int> > G[MAXN]; 20 int n, m, s, l, ans, ans2; 21 22 void SPFA(void) { 23 memset (vis, false, sizeof (vis)); 24 memset (d, INF, sizeof (d)); d[s] = 0; 25 queue<int> Q; Q.push (s); 26 while (!Q.empty ()) { 27 int u = Q.front (); Q.pop (); 28 vis[u] = false; 29 for (int i=0; i<G[u].size (); ++i) { 30 int v = G[u][i].first; int w = G[u][i].second; 31 if (d[v] > d[u] + w) { 32 d[v] = d[u] + w; 33 if (!vis[v]) { 34 vis[v] = true; Q.push (v); 35  } 36  } 37  } 38  } 39 } 40 41 int main(void) { //Codeforces Round #103 (Div. 2) D. Missile Silos 42 //freopen ("spfa.in", "r", stdin); 43 44 while (scanf ("%d%d%d", &n, &m, &s) == 3) { 45 for (int i=1; i<=m; ++i) { 46 int u, v, w; scanf ("%d%d%d", &u, &v, &w); 47  G[u].push_back (make_pair (v, w)); G[v].push_back (make_pair (u, w)); 48  } 49 scanf ("%d", &l); SPFA (); 50 51 ans = ans2 = 0; 52 for (int i=1; i<=n; ++i) { 53 for (int j=0; j<G[i].size (); ++j) { 54 int u = i, v = G[i][j].first, w = G[i][j].second; 55 if (d[u] < l && l - d[u] < w) { 56 if (w - (l-d[u]) + d[v] > l) ans++; 57 else if (d[u] + d[v] + w == 2 * l) ans2++; 58  } 59  } 60 if (d[i] == l) ans++; 61  } 62 printf ("%d\n", ans + ans2 / 2); 63  } 64 65 return 0; 66 }

 

你可能感兴趣的:(codeforces)