codeforces499B————Jzzhu and Cities(最短路)

22 篇文章 0 订阅

Jzzhu and Cities
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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


题意:城市里有M条道路和K条铁路,问在不影响最短路的情况下,最多能去掉多少条铁路。

这题唯一的难点在于有重边,啥时候铁路能去啥时候不能。


我们记录最短路图里每个点的入度(这里的方法记笔记记笔记)

然后遍历每条铁路,如果铁路长度大于最短路,肯定得去掉。

如果等于,在最短路图入度大于一的情况下可以去掉,然后把入度减一。

居然卡SPFA!!!!!


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
const double EPS=1e-16;
const int MAXN =800010;
const long long INF =0x3f3f3f3f3f3f3f;
typedef long long LL;

int n,m,k;
int tot=0;
int head[MAXN];
struct edge{
	int v,next,w;
};
edge e[MAXN];
int vis[MAXN];
LL d[MAXN];
void add(int u,int v,int w){
	e[tot].w=w;
	e[tot].v=v;
	e[tot].next=head[u];
	head[u]=tot++;
}
int in[MAXN]={0};
void SPFA(int s)
{
    queue <int> que;
      memset(vis,0,sizeof(vis));
    for(int i=0;i<=n;i++)
         d[i]=INF;
    d[s]=0;
    que.push(s);vis[s]=1;
    while(!que.empty()){
        int u=que.front();que.pop();vis[u]=0;
        for(int k=head[u];k!=-1;k=e[k].next)
        {
            int v=e[k].v,w=e[k].w;
            if(d[u]+w==d[v])
            	in[v]++;
            if(d[u]+w<d[v]){
            	in[v]=1;
                d[v]=d[u]+w;
                if(!vis[v]){
                    vis[v]=1;
                    que.push(v);
                }
            }
        }
    }
}

typedef pair <int,int> 	P;

void dij(int s){
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d,d+n+1,INF);
	d[s]=0;
	que.push(P(0,s));
	while(!que.empty()){
		P p=que.top();
		que.pop();
		int u=p.second;
		if(d[u]<p.first)
			continue;
		for(int k=head[u];k!=-1;k=e[k].next){
			int v=e[k].v;
			int w=e[k].w;
			if(d[v]==d[u]+w){
				in[v]++;
			}
			if(d[v]>d[u]+w){
				d[v]=d[u]+w;
				in[v]=1;
				que.push(P(d[v],v));
			}
		}
	}
}



int s[MAXN],y[MAXN];
int num=0;
int main()
{
    tot=0;
    memset(head,-1,sizeof(head));
	scanf("%d%d%d",&n,&m,&k);
	for(int i=0;i<m;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		add(u,v,w);
		add(v,u,w);
	}
	int cnt=0;
	for(int i=0;i<k;i++){
		scanf("%d%d",s+i,y+i);
		add(1,s[i],y[i]);
		add(s[i],1,y[i]);
	}
	dij(1);
	for(int i=0;i<k;i++){
		if(d[s[i]]<y[i])
            cnt++;
        else if(d[s[i]]==y[i]){
            if(in[s[i]]>1){
                in[s[i]]--;
                cnt++;
            }
        }
	}
	printf("%d\n",cnt);
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值