Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use thei-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.
Jzzhu doesn't want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn't change.
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105).
Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109).
Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109).
It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
Output a single integer representing the maximum number of the train routes which can be closed.
5 5 3 1 2 1 2 3 2 1 3 3 3 4 4 1 5 5 3 5 4 5 5 5
2
2 2 3 1 2 2 2 1 3 2 1 2 2 2 3
2
思路:在用dijkstra 求最短路时,其实最短路的条数是可以确定的。这个题就利用了这一点,把火车,汽车都加在同一个图里,找首都到所有点的最短路,以及最短路的条数,如果直达某城市的火车线小于
最短路,明显可以去了这个火车线,如果相等,那么就得根据最短路的条数决定了。
#include <cstdio> #include <queue> #include <map> #include <string> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const ll inf = 1e15; ll m,n,k; struct Node{ ll to,v,next; }; Node side[2000000]; ll head[1000000]; ll len = 1; void add(ll x,ll y,ll v) { side[len].to = y; side[len].v = v; side[len].next = head[x]; head[x] = len++; } ll s[1000000],y[1000000]; int in[1000000];//记录最短路的条数 struct node{ ll x,d; node(int x_,int d_) { x = x_ , d = d_; } bool operator < (const node a) const{ return d > a.d; } }; bool vis[120000]; ll dis[120000]; void Dijkstra(ll be,ll ed) { memset(vis,0,sizeof(vis)); for(int i = 1; i <= n; i++) { dis[i] = i==be ? 0 : inf; } node tmp(be,0); priority_queue <node> pq; pq.push(tmp); while(!pq.empty()) { tmp =pq.top(); pq.pop(); if(vis[tmp.x]) continue; vis[tmp.x] = 1; for(int i = head[tmp.x]; i != -1; i=side[i].next) { ll y = side[i].to; if(vis[y]) continue; ll v = side[i].v; if(dis[tmp.x] + v == dis[y]) in[y]++; if(dis[tmp.x] + v < dis[y]) { in[y] = 1; dis[y] = dis[tmp.x] + v; } pq.push((node){y,dis[y]}); } } } int main() { scanf("%lld%lld%lld",&n,&m,&k); memset(head,-1,sizeof(head)); memset(in,0,sizeof(in)); in[1] = 1; len = 1; for(int i = 1; i <= m; i++) { ll x,y,d; scanf("%lld%lld%lld",&x,&y,&d); add(x,y,d); add(y,x,d); } for(int i = 1; i <= k; i++) { scanf("%lld%lld",&s[i],&y[i]); add(1,s[i],y[i]); } Dijkstra(1,n); int num = 0; for(int i = 1; i <= k; i++) { int j =s[i]; if(dis[j] < y[i]) num++; else if(dis[j] == y[i] && in[j] != 1) num++,in[j] --; } printf("%d\n",num); return 0; }