CF 449B Dijkstra最短路

  1 #include<iostream>
  2 #include<cstring>
  3 #include<string>
  4 #include<cmath>
  5 #include<cstdio>
  6 #include<queue>
  7 #define ll long long
  8 #define INF 1e16
  9 using namespace std;
 10 const int N = 8e5 + 10;
 11 struct edge
 12 {
 13     int u, to, next;
 14     ll w;
 15 }e[N];
 16 int head[N], cnt[N];
 17 ll dis[N];
 18 bool vis[N];
 19 int tot;
 20 
 21 typedef pair<ll, int> pii;
 22 
 23 void init()
 24 {
 25     for(int i = 0 ; i < N ; i++){
 26         e[i].next = -1;
 27         head[i] = -1;
 28         vis[i] = false;
 29     }
 30     tot = 0;
 31     for(int i = 0 ; i < N ; i++){
 32         dis[i] = INF;
 33     }
 34 }
 35 
 36 inline void add(int u, int v, ll w)
 37 {
 38     e[tot].u = u;
 39     e[tot].to = v;
 40     e[tot].w = w;
 41     e[tot].next = head[u];
 42     head[u] = tot++;
 43 }
 44 
 45 void JK(int now)
 46 {
 47     dis[now] = 0;//    
 48     priority_queue<pii, vector<pii>, greater<pii> >q;//
 49     q.push(make_pair(dis[now], now));
 50     while(!q.empty()){
 51         pii tmp = q.top();
 52         q.pop();
 53         ll d = tmp.first;
 54         int u = tmp.second;
 55         if(vis[u])    continue;
 56         vis[u] = true;
 57         for(int i = head[u] ; ~i ; i = e[i].next){
 58             int v = e[i].to;
 59             if(e[i].w + d == dis[v]){
 60                 cnt[v]++;
 61             }
 62             if(e[i].w + d < dis[v]){
 63                 cnt[v] = 1;
 64                 dis[v] = e[i].w + d;
 65                 q.push(make_pair(dis[v], v));
 66             }
 67         }
 68     }
 69 }
 70 
 71 int n, m, k;
 72 
 73 int main(){
 74     scanf("%d%d%d",&n,&m,&k);
 75     init();
 76     for(int i = 1 ; i <= m ; i++){
 77         int u, v, w;
 78         scanf("%d%d%lld",&u,&v,&w);
 79         add(u, v, w);
 80         add(v, u, w);
 81     }
 82     for(int i = 1 ; i <= k ; i++){
 83         int v, w;
 84         scanf("%d%lld",&v,&w);
 85         add(1, v, w);
 86         add(v, 1, w);
 87     }
 88     
 89     JK(1);
 90     int ans = 0;
 91     for(int i = (m << 1), c = 1 ; c <= k ; c++, i += 2){
 92         int v = e[i].to, w = e[i].w;
 93         if(dis[v] < w){
 94             ans++;
 95         }else if(dis[v] == w){
 96             if(cnt[v] > 1){
 97                 ans++;
 98                 cnt[v]--;
 99             }
100         }
101     }
102     printf("%d\n",ans);
103     
104     return 0;
105 }

 

posted @ 2020-11-18 21:00  LegendN  阅读(105)  评论(0编辑  收藏  举报