【题解】Jzzhu and Cities CodeForces - 449B【最短路】 ⭐⭐⭐

本文探讨了CodeForces-449B问题的解决方案,即在一个包含城市、公路和铁路的国家中,如何最大化地关闭铁路线路而不改变从首都到每个城市的最短距离。通过使用Dijkstra算法计算最短路径和节点入度,确定哪些铁路可以安全移除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jzzhu and Cities CodeForces - 449B

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 ≤ n; ui ≠ 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

Hint




题意:

一个国家里有N个城市, M条公路, 然后又有K条铁路, 铁路从1号点直达各个城市, 现在问最多可以删除几条铁路, 使得1号点到达每点的最短距离不变.

题解:

首先用Dijkstra求出到达每点的最短路, 同时求出每个最短路终点的入度;
接下来我们遍历每条铁路, 如果大于到达该点最短路径毫无疑问直接删掉.
如果等于某点最短距离且该点入度大于1, 说明有多条线路可以以同样的距离到达, 则可以删去

要考虑到铁路和公路是可以同时坐的, 而且题中有重边

经验小结:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1 << 30;
const LL maxn = 1e6 + 10;

int head[maxn], ecnt;
struct Edge{
    int u, v, w, next;
}es[maxn];
void addEdge(int u, int v, int w){
    es[ecnt].u = u, es[ecnt].v = v, es[ecnt].w = w;
    es[ecnt].next = head[u], head[u] = ecnt++;
}
int N, M, K;
int d[maxn];
bool used[maxn];
typedef pair<int, int> P;
int in[maxn]; //入度
void dijkstra(int s) {
    //求得从s出发到达各点的最短路径
    ms(used, 0);
    fill(d, d + maxn, inf);
    priority_queue<P, vector<P>, greater<P> > q;
    q.push(P(d[s] = 0, s));
    while(!q.empty()) {
        P cur = q.top();
        q.pop();
        int u = cur.second;
        if(used[u])
            continue;
        used[u] = true;
        //遍历和cur.second相邻的所有点并更新距离
        for(int i = head[u]; i != -1; i=es[i].next){
            int v = es[i].v, w = es[i].w;
            if(d[v] > d[u] + w) {
                d[v] = d[u] + w;
                q.push(P(d[v], v));
                in[v] = 1;
            }
            else if(d[v] == d[u]+w)
                ++in[v];
        }
    }
}
int s[maxn], y[maxn];
int main() {
    ms(head, -1); ecnt = 0;
    int a, b, c;
    cin >> N >> M >> K;
    for(int i = 1; i <= M; ++i){
        cin >> a >> b >> c;
        addEdge(a, b, c);
        addEdge(b, a, c);
    }
    for(int i = 1; i <= K; ++i){
        cin >> s[i] >> y[i];
        addEdge(1, s[i], y[i]);
        addEdge(s[i], 1, y[i]);
    }
    dijkstra(1);
    int ans = 0;
    for(int i = 1; i <= K; ++i){
        if(d[s[i]] < y[i])
            ++ans;
        else if(d[s[i]] == y[i] && in[s[i]]>1)
            --in[s[i]], ++ans; //入度大于1说明有多条路线可达
    }
    cout << ans << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值