CodeForces-449B Jzzhu and Cities

本文介绍了一种基于SPFA算法结合优先队列优化的方法,用于解决特定类型的最短路径问题。具体而言,在一个带有城市和道路的国家网络中,通过优化后的SPFA算法确定从首都到每个城市的最短路径,并在此基础上决定哪些火车线路可以停运而不改变这些最短路径的距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Examples
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

spfa+优先队列优化

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f
typedef long long ll;
struct Edge{
	ll to;
	ll nxt;
	ll w;
}edge[1000010];
ll tot=0,in[1000100],v[1000100],w[1000100],head[1000100],vis[1000100]; 
void add(ll u,ll v,ll w){
	edge[++tot].to=v; edge[tot].nxt=head[u]; edge[tot].w=w; head[u]=tot;
	edge[++tot].to=u; edge[tot].nxt=head[v]; edge[tot].w=w; head[v]=tot;
}
ll n,m,s,t,inq[1000100],dis[1000100];
void spfa(int u)
{
	memset(dis,INF,sizeof(dis));
	memset(inq, 0 ,sizeof(inq));
	priority_queue<int>q;
	q.push(u);inq[u]=1;dis[u]=0;
	while(!q.empty()){
		int t=q.top();
		q.pop(); inq[t]=0;
		for(int i=head[t];~i;i=edge[i].nxt){
			Edge e=edge[i];
			if(dis[e.to]>dis[t]+e.w){
				in[e.to]=1;
				dis[e.to]=dis[t]+e.w;
				if(!inq[e.to]){
					inq[e.to]=1;
					q.push(e.to);
				}
			}
			else if(dis[e.to]==dis[t]+e.w) in[e.to]++;
		} 
	}
}
int main(){
	memset(head,-1,sizeof(head));
	ll n,m,k; scanf("%lld%lld%lld",&n,&m,&k);
	for(int i=1;i<=m;i++){
		ll a,b,c; scanf("%lld%lld%lld",&a,&b,&c);
		add(a,b,c);
	}
	for(int i=1;i<=k;i++){
		scanf("%lld%lld",&v[i],&w[i]);
		add(1,v[i],w[i]);
	}
	spfa(1);
	ll cnt=0;
	for(int i=1;i<=k;i++){
		if(dis[v[i]]<w[i]) cnt++;
        else if(dis[v[i]]==w[i]){
            if(in[v[i]]>1){
                in[v[i]]--;
                cnt++;
            }
        }
	}
	printf("%lld\n",cnt);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值