Codeforces 449B Jzzhu and Cities(最短路)

题意:一个城市,有m条道路,还有k条铁路(铁路都与点1相连),问在不改变点1到各个点的最短路的前提下最多可以删除多少条铁路

思路:将道路和铁路一起建在同一个图中,跑一遍最短路并记录每个点的入度,接下来有两种情况可以判断可以删除这条铁路。

1,如果最短路比铁路到点1的距离短,那毫无疑问可以删除

2,如果最短路和铁路到点1的距离相同,那么如果入度大于1的话,说明可以通过走别的点到达,不需要铁路所以也可以删除

Trick:INF要开大一点...


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 100005
#define LL long long
int cas=1,T;
const LL INF = 1e17;
int n,m;
struct Edge
{
	int from,to,dist;
	Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
struct HeapNode
{
	int d,u;
	bool operator < (const HeapNode&rhs)const
	{return d>rhs.d;}
};
vector<Edge>edges;
vector<int>G[maxn];
bool done[maxn];
LL d[maxn];
LL inq[maxn];
struct Dijkstra
{ 
   /* vector<Edge>edges;
	vector<int>G[maxn];
	bool done[maxn];
	LL d[maxn];
	LL inq[maxn];*/
	void init()
	{
		for (int i = 0;i<=n;i++)
			G[i].clear();
		edges.clear();
		memset(inq,0,sizeof(inq));
	}
	void AddEdge(int from,int to,int dist)
	{
		edges.push_back(Edge(from,to,dist));
		int mm = edges.size();
		G[from].push_back(mm-1);
	}
	void dijkstra(int s)
	{
		priority_queue<HeapNode> q;
		for (int i =0;i<=n;i++)
			d[i]=INF;
		d[s]=0;
		memset(done,0,sizeof(done));
		q.push((HeapNode){0,s});
		while (!q.empty())
		{
            HeapNode x = q.top();q.pop();
			int u = x.u;
			if (done[u])continue;
			done[u]=1;
			for (int i = 0;i<G[u].size();i++)
			{
				Edge &e = edges[G[u][i]];
				if (d[e.to] == d[u]+e.dist)
					inq[e.to]++;
				if (d[e.to] > d[u]+e.dist)
				{
					d[e.to] = d[u] + e.dist;
					inq[e.to]=1;
					q.push((HeapNode){d[e.to],e.to});
				}
			}
		}
	}
};
struct train
{
	int v;
	LL dist;
}t[maxn];
int main()
{
	int k;
	scanf("%d%d%d",&n,&m,&k);
	Dijkstra dij;
	dij.init();
	for (int i = 0;i<m;i++)
	{
		int u,v,di;
		scanf("%d%d%d",&u,&v,&di);
        dij.AddEdge(u,v,di);
		dij.AddEdge(v,u,di);
	}
	for (int i = 0;i<k;i++)
	{
        scanf("%d%lld",&t[i].v,&t[i].dist);
		dij.AddEdge(1,t[i].v,t[i].dist);
	}
	dij.dijkstra(1);
	LL ans = 0;
	for (int i = 0;i<k;i++)
	{
		if (t[i].dist == d[t[i].v])
			if (inq[t[i].v]>1)
			{
				ans++;
				inq[t[i].v]--;
			}
		if (t[i].dist > d[t[i].v])
			ans++;
	}
	printf("%lld\n",ans);
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


题目

Description

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

Sample Input

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值