Codeforces 449 B. Jzzhu and Cities
2015-03-20 14:44
309 查看
堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除。
B. Jzzhu and Cities
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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
Output a single integer representing the maximum number of the train routes which can be closed.
Sample test(s)
input
output
input
output
B. Jzzhu and Cities
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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
Output a single integer representing the maximum number of the train routes which can be closed.
Sample test(s)
input
5 5 3 1 2 1 2 3 2 1 3 3 3 4 4 1 5 5 3 5 4 5 5 5
output
2
input
2 2 3
1 2 22 1 3
2 1
2 22 3
output
2
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <queue> #include <vector> using namespace std; typedef long long int LL; typedef pair<LL,int> pLI; typedef pair<int,LL> pIL; const int maxn=210000; const LL INF=1LL<<60; int n,m,k; vector<pIL> edge[maxn]; LL dist[maxn]; bool train[maxn]; int main() { scanf("%d%d%d",&n,&m,&k); for(int i=0;i<m;i++) { int u,v;LL x; //scanf("%d%d%lld",&u,&v,&x); scanf("%d%d%I64d",&u,&v,&x); u--; v--; edge[u].push_back(make_pair(v,x)); edge[v].push_back(make_pair(u,x)); } for(int i=0;i<=n;i++) { dist[i]=INF; train[i]=false; } dist[0]=0; for(int i=0;i<k;i++) { int s; LL y; //scanf("%d%lld",&s,&y); scanf("%d%I64d",&s,&y); s--; train[s]=true; dist[s]=min(dist[s],y); } priority_queue<pLI> heap; for(int i=0;i<n;i++) { if(dist[i]!=INF) { heap.push(make_pair(-dist[i],i)); } } while(heap.size()) { pLI temp=heap.top(); heap.pop(); LL D=-temp.first; int u=temp.second; if(dist[u]!=D) continue; for(int i=0,sz=edge[u].size();i<sz;i++) { int v=edge[u][i].first; LL len=edge[u][i].second; if(dist[v]>=dist[u]+len) { if(train[v]==true) { train[v]=false; } } if(dist[v]>dist[u]+len) { dist[v]=dist[u]+len; heap.push(make_pair(-dist[v],v)); } } } int ans=k; for(int i=0;i<n;i++) { ans-=train[i]; } printf("%d\n",ans); return 0; }
相关文章推荐
- CodeForces - 449B Jzzhu and Cities
- CodeForces 449 B. Jzzhu and Cities
- Codeforces 449 B. Jzzhu and Cities(图论dijk)
- Codeforces 449 B. Jzzhu and Cities
- CodeForces 449-A. Jzzhu and Chocolate
- Codeforces 449 A Jzzhu and Chocolate
- Jzzhu and Cities ----CodeForces - 449B
- CodeForces 449B - Jzzhu and Cities(最短路)
- Jzzhu and Cities(CodeForces - 449B)
- Codeforces 449B_Jzzhu and Cities
- codeforces 449 D Jzzhu and Numbers(容斥+dp)
- CodeForces 449 C.Jzzhu and Apples(构造+数论)
- CodeForces 449 A.Jzzhu and Chocolate(枚举)
- CodeForces 449 D.Jzzhu and Numbers(状压DP+容斥原理)
- Jzzhu and Cities CodeForces - 449B (最短路 + 最短路的条数)
- Codeforces 449B - Jzzhu and Cities / 450D - Jzzhu and Cities
- CodeForces 450D - Jzzhu and Cities (dij + heap )
- Codeforces 450D - Jzzhu and Cities(最短路)
- Jzzhu and Cities CodeForces - 450D
- Codeforces 449D:Jzzhu and Numbers