CodeForces - 449B Jzzhu and Cities (思维+最短路spfa)

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

题目链接:http://codeforces.com/problemset/problem/449/B

题目大意:有n个点,m条公路(u,v,w),k条铁路(1,u,w),1是首都,如果删除第i条铁路后,1到ui 的最短路没变,那么这条铁路就可以删除,问最多可以删除几条铁路

思路:求出点1到各点的最短路,并用in数组标记最短路径的入度,如果第i条铁路的权 wi > dis[ui] 那么这条铁路可以删除,如果wi == dis[ui]  且入度 in[ui] 大于1,也就是可以通过别的路径构成最短路,可以删除这条铁路。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define ll long long
const int N=400010;
const ll inf=9223372036854775806;
int n,m,k;
int first[N],tot,vis[N],in[N],b[N];
ll dis[N],a[N];
struct node
{
    int v,nex;
    ll w;
}e[N<<1];
void adde(int u,int v,ll w)
{
    e[tot].v=v;
    e[tot].w=w;
    e[tot].nex=first[u];
    first[u]=tot++;
}
void spfa()
{
    for(int i=0;i<=n;i++)
    {
        vis[i]=0;
        in[i]=0;
        dis[i]=inf;
    }
    priority_queue<int>q;
    dis[1]=0;
    vis[1]=1;
    q.push(1);
    while(!q.empty())
    {
        int u=q.top();
        q.pop();
        vis[u]=0;
        for(int i=first[u];~i;i=e[i].nex)
        {
            int v=e[i].v;
            if(dis[v]>dis[u]+e[i].w)
            {
                in[v]=1;
                dis[v]=dis[u]+e[i].w;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
            else if(dis[v]==dis[u]+e[i].w)
                in[v]++;
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    int x,y;
    ll w;
    tot=0;
    memset(first,-1,sizeof(first));
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%lld",&x,&y,&w);
        adde(x,y,w);
        adde(y,x,w);
    }
    for(int i=1;i<=k;i++)
    {
        scanf("%d%lld",&x,&w);
        adde(1,x,w);
        adde(x,1,w);
        a[i]=w;
        b[i]=x;
    }
    spfa();
    int ans=0;
    for(int i=1;i<=k;i++)
    {
        x=b[i];
        if(a[i]>dis[x]) 
            ans++;
        else if(a[i]==dis[x]&&in[x]>1)
        {
            in[x]--; //删除这条铁路之后入度减一
            ans++;
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值