【cf449B. Jzzhu and Cities】dijkstra算法

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 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 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 ≤ 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

Copy

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

Copy

2

input

Copy

2 2 3
1 2 2
2 1 3
2 1
2 2
2 3

output

Copy

2

错误思路:先排道路,再加入火车,假如火车小于当前最短路就删去,

错误原因:新加入的火车有可能也会更新其它路的最短路,难不成加一次dij一次?

/*核心思路:先把火车当成普通的道路,求整张图的最短路,然后遍历每条火车线路是不是最短,
 不是最短或者有其它和它一样短的道路都是可以删去的*/ 
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
vector<pair<int,ll>>to[N];//pair第一个元素存目的地,第二个元素存距离,to的下标存出发点 
int n,m,k;
ll ans,dis[N],b[N];
int cnt[N],a[N],vis[N];
void dijkstra() { 
	memset(dis,127,sizeof(dis));//dis为i到1的最短距离,初始化默认都是独立的没有路联通所以距离为无穷大 
	memset(vis,0,sizeof vis);//vis记录是否遍历过 
	priority_queue<pair<ll,int>>q;//优先队列前面存dis【i】,后面存点 i (可以想象成存路) 
	q.push({0,1});//从1开始遍历 
	dis[1]=0;//1到自己的距离为0 
	while(!q.empty()) {
		int u=q.top().second;//优先队列取头顶用top 因为存的是pair取第二个元素用second 
		q.pop();//千万别忘了pop!!!!!!!!!!!!!! 划重点 
		if(vis[u])continue;//遍历过了就下一个 
		vis[u]=1;//没遍历的话就标一下 
		for(int i=0; i<to[u].size(); i++) {//i从0开始取 
			int v=to[u][i].first,w=to[u][i].second;//目的地,距离 
//			if(vis[v])continue;//假如是这么写我第45个样例过不了可恶 
			if(dis[v]==dis[u]+w) {//加上这条路和本来 它的最短路一样 
				cnt[v]++;//从1到v的最短路增加了 
			}
			if(dis[v]>dis[u]+w) {
				cnt[v]=1;//从1到v的最短路只有当前一条 
				dis[v]=dis[u]+w;
				q.push({-dis[v],v});//因为优先队列是按照第一个元素从大到小排序的,所以以负数的方式存进去可以实现dis绝对值从小到大排序 
			}
		}
	}
}
int main() {
	cin>>n>>m>>k;
	for(int i=1,u,v; i<=m; i++) {
		ll w;//注意!!w应该为longlong,虽然它的数据不会给那么大但是它要和ll类型的计算 
		cin>>u>>v>>w;
		to[u].push_back({v,w});//无向道路双向联通 
		to[v].push_back({u,w}); 
	}
	for(int i=1,v; i<=k; i++) {
		ll w;
		cin>>v>>w;
		to[1].push_back({v,w});//直接看成是从1到v 的路 
		to[v].push_back({1,w});
		a[i]=v;
		b[i]=w;
	}
	dijkstra();
	int ans=0;
	for(int i=1; i<=k; i++) {
		if(dis[a[i]]<b[i])ans++;//当前不是最短路 
		if(dis[a[i]]==b[i]) {//当前是最短路,但有路可以顶替你的位置 
			if(cnt[a[i]]>1) {
				ans++;
				cnt[a[i]]--;//你被噶了 
			}
		}
	}
	cout<<ans;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值