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 mroads 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 the i-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
最短路、、、、写短路了
思路:堆优化的DIJK。每次拿权最小(权相等优先拿到达这个点用的是公路边的)
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <map> #include <set> #include <cmath> #include <queue> using namespace std; #define LL long long int #define lson id<<1,l,mid #define rson id<<1|1,mid+1,r #define maxn 200080 bool vis[maxn]; LL dis[maxn]; struct Edge { int v;LL cost; bool istrain; Edge(){} Edge(int vv,LL cc,bool a) { v = vv; cost = cc; istrain = a; } bool operator < (const Edge & a)const { if(cost == a.cost) return istrain; else return cost > a.cost; } }; vector <Edge> ans[maxn]; priority_queue <Edge> q; int Dijkstra(int n) { while(!q.empty()) q.pop(); memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); dis[1] = 0; int nown = 0,sum = 0; q.push(Edge(1,0,0)); while(nown < n && !q.empty()) { L: Edge edge = q.top(); q.pop(); while(vis[edge.v] && !q.empty()) goto L; if(!vis[edge.v]) { int nowv = edge.v; vis[nowv] = 1; //cout << nowv << " " << edge.istrain << endl; if(edge.istrain) sum++; nown++; for(int i = 0;i < ans[nowv].size();i++) { if(!vis[ans[nowv][i].v] && (dis[ans[nowv][i].v] >= ans[nowv][i].cost+dis[nowv])) { dis[ans[nowv][i].v] = ans[nowv][i].cost+dis[nowv]; edge.v = ans[nowv][i].v; edge.cost = dis[ans[nowv][i].v]; if(ans[nowv][i].istrain) edge.istrain = 1; else edge.istrain = 0; q.push(edge); } } } } return sum; } int main() { //freopen("in.txt","r",stdin); int n,m,k; while(scanf("%d%d%d",&n,&m,&k)==3) { for(int i = 1;i <= n;i++) ans[i].clear(); for(int i = 1;i <= m;i++) { int u,v,x; scanf("%d%d%d",&u,&v,&x); ans[u].push_back(Edge(v,x,0)); ans[v].push_back(Edge(u,x,0)); } for(int i = 1;i <= k;i++) { int s,y; scanf("%d%d",&s,&y); ans[1].push_back(Edge(s,y,1)); ans[s].push_back(Edge(1,y,1)); } int sum = Dijkstra(n); printf("%d\n",k-sum); } return 0; }