D. Jzzhu and Cities

来源:互联网 发布:java读取log日志文件 编辑:程序博客网 时间:2024/05/17 00:13

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 the i-th train route to go from capital of the country to city si (and vise versa), the length of this route isyi.

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 ≤ nui ≠ 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.

Examples
input
5 5 31 2 12 3 21 3 33 4 41 5 53 54 55 5
output
2
input
2 2 31 2 22 1 32 12 22 3
output
2

这道题啊,绝望,必须得用微软的交,不然超时。

if(dis[u] >= dis[re]+ v && p[u])
p[u]=0;

这句很重要,尤其是>=号!!!

学会了vector很高心,也顺带学会了SPFA算法,还行吧,值了今天。


#include<iostream>#include<queue>#include<vector>#include<string.h>#include<stdlib.h>using namespace std;typedef long long ll;const ll INF = 0x3f3f3f3f3f3f3f3f;int n,m,k,vis[100050],p[100050];ll dis[100050];struct Edge{int e;int w;Edge(int ee,int ww):e(ee),w(ww){};};//pair<int,int> pc;vector<Edge> G[100050];void init(){int i,j,k,start,end,lenth;for(i=0;i<m;i++){cin>>start>>end>>lenth;G[start].push_back(Edge(end,lenth));G[end].push_back(Edge(start,lenth));}}void solve(){int i,j,re;for(i=0;i<=n;i++)dis[i]=INF;dis[1]=0;sizeof(vis,0,sizeof(vis));vis[1]=1;sizeof(p,0,sizeof(p));queue<int> que;que.push(1);int aim,value;for(i=0;i<k;i++){cin>>aim>>value;if(value < dis[aim]){dis[aim]=value;p[aim]=1;if(vis[aim]==0){vis[aim]=1;que.push(aim);}}}ll u,v;while(!que.empty()){re=que.front();vis[re]=0;que.pop();for(i=0;i<G[re].size();i++){u=G[re][i].e;v=G[re][i].w;if(dis[u] >= dis[re]+ v && p[u])p[u]=0;if(dis[u] > dis[re] + v){dis[u]=dis[re] + v;if(vis[u]==0){vis[u]=1;que.push(u);}}}}for(i=1;i<=n;i++)k-=p[i];cout<<k<<endl;return ;}int main(){cin>>n>>m>>k;init();solve();return 0;}


0 0
原创粉丝点击