D Jzzhu and Cities --SPFA

 

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 test(s)
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个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路
 
   
分析:先求一次最短路,铁路的权值大于该点最短距离的显然可以删去,否则将该条边加入图中,再求最短路,记录每个点的前一个点,然后又枚举铁路,已经删去的就不用处理了,如果铁路权值大于该点最短距离又可以删去,权值相等时,该点的前一个点如果不为1,则这个点可以由其他路到达,这条铁路又可以删去。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#define lll __int64
using namespace std;
#define N 100006

struct Edge
{
    int v;
    lll w;
    Edge(int _v,lll _w)
    {
        v = _v;
        w = _w;
    }
    Edge(){}
};

struct Train
{
    int v;
    lll w;
}T[N];

vector<Edge> G[N];
lll dis[N];
int n,m;
int head[N],tot,pre[N];
int cut[N],vis[N];
queue<int> que;

void SPFA(int s)
{
    while(!que.empty())
        que.pop();
    memset(vis,0,sizeof(vis));
    vis[s] = 1;
    que.push(s);
    for(int i=0;i<=n;i++)
        dis[i] = 1000000000000000LL;
    dis[s] = 0;
    while(!que.empty())
    {
        int u = que.front();
        que.pop();
        vis[u] = 0;
        for(int i=0;i<G[u].size();i++)
        {
            int v = G[u][i].v;
            lll w = G[u][i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if(!vis[v])
                {
                    vis[v] = 1;
                    que.push(v);
                }
            }
        }
    }
}

void SPFA2()
{
    while(!que.empty())
    {
        int u = que.front();
        que.pop();
        vis[u] = 0;
        for(int i=0;i<G[u].size();i++)
        {
            int v = G[u][i].v;
            lll w = G[u][i].w;
            if(dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                pre[v] = u;
                if(!vis[v])
                {
                    que.push(v);
                    vis[v] = 1;
                }
            }
            else if(dis[v] == dis[u] + w && pre[v] < u)
                pre[v] = u;
        }
    }
}

int main()
{
    int i,j,k;
    int u,v,y;
    lll w;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        tot = 0;
        for(i=0;i<=n;i++)
            G[i].clear();
        memset(head,-1,sizeof(head));
        memset(cut,0,sizeof(cut));
        memset(pre,-1,sizeof(pre));
        for(i=0;i<m;i++)
        {
            scanf("%d%d%I64d",&u,&v,&w);
            G[u].push_back(Edge(v,w));
            G[v].push_back(Edge(u,w));
        }
        for(i=0;i<k;i++)
        {
            scanf("%d%I64d",&y,&w);
            T[i].v = y;
            T[i].w = w;
        }
        SPFA(1);
        int cnt = 0;
        for(i=0;i<k;i++)
        {
            int v = T[i].v;
            lll w = T[i].w;
            if(dis[v] <= w)
            {
                cut[i] = 1;
                cnt++;
            }
            else
            {
                G[1].push_back(Edge(v,w));
                G[v].push_back(Edge(1,w));
                pre[v] = 1;
                dis[v] = w;
                que.push(v);
                vis[v] = 1;
            }
        }
        SPFA2();
        for(i=0;i<k;i++)
        {
            if(cut[i])
                continue;
            int v = T[i].v;
            lll w = T[i].w;
            if((dis[v] == w && pre[v] != 1) || dis[v] < w)
                cnt++;
        }
        printf("%d\n",cnt);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值