CodeForces - 449B Jzzhu and Cities 最短路

CodeForces - 449B  Jzzhu and Cities

Jzzhu is the president of country A. There are n cities numbered from1 ton in his country. City1 is the capital of A. Also there arem roads connecting the cities. One can go from cityui tovi (and vise versa) using thei-th road, the length of this road is xi. Finally, there arek train routes in the country. One can use thei-th train route to go from capital of the country to citysi (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 integersui, vi, xi(1 ≤ ui, vi ≤ nui ≠ vi; 1 ≤ xi ≤ 109).

Each of the next k lines contains two integerssi andyi(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.

Example

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 2
2 1 3
2 1
2 2
2 3
Output
2

题意是一个国家有n个城市,m条路,k条铁路,城市标号1是首都,并且k条铁路都与首都相连,问在首都与各个城市最短距离不变的情况下,最多能去多少铁路。

思路:另设一个标记数组train,一开始全为0,然后把有铁路的终点标记为1,并且更新dis数组,注意可能有重复的铁路,然后用spfa求最短路,在松弛时多加一个条件,如果这个点有铁路连接,并且到这个点的最短距离能被松弛,则要去掉这条铁路,标记为0,最后k减去标记为1的就是要去掉的铁路数

开始用的是spfa队列优化,一直在T在45这个样例,搜的题解也有用这个的,为什么我的过不了,然后百度了一个spfa双队列优化,SLF:Small Label First 策略,设要加入的节点是j,队首元素为i,若dist(j)<dist(i),则将j插入队首,否则插入队尾。

#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
const long long  INF = 0x3f3f3f3f3f3f3f3f;
const int maxn=100100;
int N,M,K;
struct edge{
	int v,w,next;
}e[600200];
int head[maxn],cnt;
long long dis[maxn];
bool vis[maxn],train[maxn];
void add_edge(int u,int v,int w)
{
	e[cnt].v=v;
	e[cnt].w=w;
	e[cnt].next=head[u];
	head[u]=cnt++;
}
int spfa()
{	

	dis[1]=0;
	vis[1]=true;
	deque<int> q;
	q.push_back(1);
    
	for(int i=1;i<=K;i++)
	{
		int v,x;
		scanf("%d%d",&v,&x);
		if(dis[v]>x) 
		{
		   dis[v]=x;
		   train[v]=true;
           if(!vis[v])
		   {
			   if(!q.empty()&&dis[v]<dis[q.front()])
			      q.push_front(v);
			   else
			      q.push_back(v); 
				vis[v]=true;    
		   }
     	}
	}
	while(!q.empty())
	{
		int temp=q.front();
		q.pop_front();
		vis[temp]=false;
		
		for(int i=head[temp];i!=-1;i=e[i].next)
		{
			int to=e[i].v;
			if(dis[to]>=dis[temp]+e[i].w&&train[to]) train[to]=false;
			if(dis[to]>dis[temp]+e[i].w)
			{
				dis[to]=dis[temp]+e[i].w;
				if(!vis[to])
				{
					if(!q.empty()&&dis[to]<dis[q.front()])
					   q.push_front(to);
					else
					   q.push_back(to);
					vis[to]=true;      
				}
			}
		}
	}
	int con=0;
	for(int i=1;i<=N;i++)
	    if(train[i]) con++;
	return K-con;    
}
int main(void)
{
	int u,v,w;
	scanf("%d%d%d",&N,&M,&K);
	
	cnt=0;
	for(int i=1;i<=N;i++)
    {
    	vis[i]=false;
    	train[i]=false;
    	dis[i]=INF;
    	head[i]=-1;
    }
	for(int i=1;i<=M;i++)
	{
		scanf("%d%d%d",&u,&v,&w);
		add_edge(u,v,w);
		add_edge(v,u,w);
	} 
	printf("%d\n",spfa());
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值