CF449B-- 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 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
最短路、、、、写短路了尴尬
思路:堆优化的DIJK。每次拿权最小(权相等优先拿到达这个点用的是公路边的)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <queue>
using namespace std;
#define LL long long int
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define maxn 200080
bool vis[maxn];
LL dis[maxn];
struct Edge
{
    int v;LL cost;
    bool istrain;
    Edge(){}
    Edge(int vv,LL cc,bool a)
    {
        v = vv; cost = cc;  istrain = a;
    }
    bool operator < (const Edge & a)const
    {
        if(cost == a.cost)  return istrain;
        else return cost > a.cost;
    }
};
vector <Edge> ans[maxn];
priority_queue <Edge> q;
int Dijkstra(int n)
{
    while(!q.empty()) q.pop();
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    dis[1] = 0;
    int nown = 0,sum = 0;
    q.push(Edge(1,0,0));
    while(nown < n && !q.empty())
    {
L:
        Edge edge = q.top();
        q.pop();
        while(vis[edge.v] && !q.empty())    goto L;
        if(!vis[edge.v])
        {
            int nowv = edge.v;
            vis[nowv] = 1;
            //cout << nowv << " " << edge.istrain << endl;
            if(edge.istrain)    sum++;
            nown++;
            for(int i = 0;i < ans[nowv].size();i++)
            {
                if(!vis[ans[nowv][i].v] && (dis[ans[nowv][i].v] >= ans[nowv][i].cost+dis[nowv]))
                {
                    dis[ans[nowv][i].v] = ans[nowv][i].cost+dis[nowv];
                    edge.v = ans[nowv][i].v;
                    edge.cost = dis[ans[nowv][i].v];
                    if(ans[nowv][i].istrain)    edge.istrain = 1;
                    else edge.istrain = 0;
                    q.push(edge);
                }
            }
        }
    }
    return sum;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)==3)
    {
        for(int i = 1;i <= n;i++)
            ans[i].clear();
        for(int i = 1;i <= m;i++)
        {
            int u,v,x;
            scanf("%d%d%d",&u,&v,&x);
            ans[u].push_back(Edge(v,x,0));
            ans[v].push_back(Edge(u,x,0));
        }
        for(int i = 1;i <= k;i++)
        {
            int s,y;
            scanf("%d%d",&s,&y);
            ans[1].push_back(Edge(s,y,1));
            ans[s].push_back(Edge(1,y,1));
        }
        int sum = Dijkstra(n);
        printf("%d\n",k-sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值