Codeforces 449B Jzzhu and Cities(最短路计数+迪杰斯特拉最小堆优化)

H - Jzzhu and Cities

 CodeForces - 449B 

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

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个城市,编号1-n,编号1的城市为首都,城市之间有m条双向道路连接,同时有k条火车路线可以直接让其他城市与首都城市连接,每条火车路线也存在自己的距离长度,因为火车路线的修建存在一定花费,现在需要保证首都在其余的n-1个城市的距离最短的情况下,去除一些没没必要的火车路径,问最多能去除几条。

解题思路:因为求单源最短路,所以很简单的直接把火车路线和普通的公路路线全部建图,然后从源点1开始求到其他点的最短路,但是我们需要知道最终求出的最短路有哪些火车路线是参与的,把没有参与的火车路线统计数目就是答案,因为火车路线是源点1直接通向其他城市的路线,而题目也是问源点1到其他城市的最短路,所以我们可以把最后求出的到每个城市的最短路dis[maxn]与火车路线的距离进行比较,如果某个点v,最短路dis[v]小于源点到这个点v的火车路线w,就说明此时的这条火车路线根本不可能参与,所以去除,ans++,如果某个点v,最短路dis[v]等于源点到这个点v的火车路线w,就需要做特殊判断,看看有没有到终点v有没有其他路线,如果有其他路线可以到达,那么此时的这条火车路线也是没必要的,就去除,因为我们需要知道有没有多余的路线,就涉及了最短路计数问题,这里的最短路需要注意:
常知的最短路计数方程:
if(dis[v]==dis[u]+mp[u][v])
num[v]=num[v]+num[u];
else if(dis[v]>dis[u]+mp[u][v])
{
dis[v]=dis[u]+mp[u][v];
num[u]=num[v];
}
但是这个题目中我们需要知道的是从源点直接到终点v的最短路数目,
所以此时的最短路计数方程是:
if(dis[v]==dis[u]+mp[u][v])
num[v]++;
else if(dis[v]>dis[u]+mp[u][v])
{
dis[v]=dis[u]+mp[u][v];
//1表示源点到源点的最短路只有1条
num[v]=1;
}
然后这题数据比较大,还需要用邻接表储存然后用迪杰斯特拉最小堆优化,不然会严重超时

原文:https://blog.csdn.net/weixin_43918046/article/details/93538349 


代码:


//Full of love and hope for the life

//https://paste.ubuntu.com/

#include<iostream>
#include<queue>
#include<string.h>
#include<string>
#include <stdio.h>

using namespace std;

typedef long long ll;
const int N=3e5+100;
struct Node{
	int v,next;
	ll w;
}edge[4*N];
int head[N],cnt;

void add_edge(int u,int v,ll w)
{
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}

bool vis[N];
ll dis[N];
int num[N],n,m,k;

struct Sode{
	int v;
	ll dis;
	Sode(int v1,ll dis1)
	{
		v=v1;
		dis=dis1;
	}
	bool operator<(const Sode &t)const
	{
		return dis>t.dis;
	}
};

priority_queue<Sode>q;
void dij()
{
	while(!q.empty())
	q.pop();
	memset(vis,0,sizeof(vis));
	memset(num,0,sizeof(num));
	for(int i=1;i<=n;i++)
	dis[i]=1e18;
	dis[1]=0;
	q.push(Sode(1,0));
//	num[1]=1;
	while(!q.empty())
	{
		Sode t=q.top();
		q.pop();
		if(vis[t.v])continue;
		vis[t.v]=true;
		for(int i=head[t.v];i!=-1;i=edge[i].next)
		{
			int v=edge[i].v;
			ll w=edge[i].w;
			//开始最短路计数
			if(dis[v]==dis[t.v]+w)
			num[v]++;
			else if(dis[v]>dis[t.v]+w)
			{
				num[v]=1;
				dis[v]=dis[t.v]+w;
				q.push(Sode(v,dis[v]));
			}
		}
	}
}

vector<int>inv;
vector<ll>inw;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	memset(head,-1,sizeof(head));
	cnt=0;
	while(m--)
	{
		int x,y;
		ll w;
		scanf("%d%d%I64d",&x,&y,&w);
		add_edge(x,y,w);
		add_edge(y,x,w);
	}
	inv.clear();
	inw.clear();
	int ans=0;
	while(k--)
	{
		int v;
		ll w;
		scanf("%d%I64d",&v,&w);
		inv.push_back(v);
		inw.push_back(w);
		add_edge(1,v,w);
		add_edge(v,1,w);
	}
	dij();
	for(int i=0;i<inv.size();i++)
	{
		int v=inv[i];
		ll w=inw[i];
		if(dis[v]<w)
		ans++;
		else if(dis[v]==w)
		{
			//这里注意,因为题目中到终点v的火车路线可能有多条,所以需要num[v]--判断多个终点为v的火车路线
			if(num[v]>1)
			{
				ans++;
				num[v]--;
			}
		}
	}
	printf("%d\n",ans);
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值