Jzzhu and Cities (CodeForces - 449B)

Jzzhu and Cities

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.


把火车路线和城市路线混合在一起,求最短路,同时记录每个点的入度;

然后枚举火车站点,如果该点最短路小于火车路线长,那么去掉该火车路线;
如果等于,看这个点的入度,入度大于1,说明可以去掉,反之不能;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=400100;
const int M=400100;
const LL mod=998244353;
int n,m,k;
struct No{
	LL date;
	int post;
}train[M];
struct Node{
	int to,next;
	LL w;
}edge[M<<1];//存双向边要乘以2 
int cnt;
int head[N+100];
void add(int p,int q,LL w){
	edge[cnt].to=q;
	edge[cnt].next=head[p];
	edge[cnt].w=w;
	head[p]=cnt++;
}
int in[N];//入度
LL dis[N];
bool vis[N];
struct Jie{
	LL fi;
	int se;//se为边的尾结点,fi为权值 
	bool operator < (const Jie &a)const {
		return fi > a.fi;
	}
};
void dijkstra(){
	priority_queue<Jie>q;
	memset(vis,false,sizeof(vis));
	for(int i=1;i<=N;i++) dis[i]=2e17;
	dis[1]=0LL;
	Jie n1;
	n1.fi=0LL;
	n1.se=1;
	q.push(n1);
	while (!q.empty()){
		Jie n2=q.top();
		q.pop();
		if(vis[n2.se]) continue;
		vis[n2.se]=true;
		for(int i=head[n2.se];~i;i=edge[i].next){
			int v=edge[i].to;
			if(dis[n2.se]+edge[i].w==dis[v]) in[v]++;
			if(dis[n2.se]+edge[i].w<dis[v]){
				in[v]=1;//这里千万注意,要变为1
				dis[v]=dis[n2.se]+edge[i].w;
				Jie n3;
				n3.fi=dis[v];
				n3.se=v;
				q.push(n3);
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n>>m>>k;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=m;i++){
		int u,v;
		LL x;
		cin>>u>>v>>x;
		add(u,v,x);
		add(v,u,x);
	}
	for(int i=1;i<=k;i++){
		int s;
		LL y;
		cin>>s>>y;
		add(1,s,y);
		add(s,1,y);
		train[i].date=y;
		train[i].post=s;
	}
	dijkstra();
	int sum=0;
	for(int i=1;i<=k;i++){
		if(dis[train[i].post]<train[i].date) sum++;
		if(dis[train[i].post]==train[i].date){
			if(in[train[i].post]>1){
				sum++;
				in[train[i].post]--;//注意要减一 
			}
		}
	}
	cout<<sum<<endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值