Codeforces 450D - Jzzhu and Cities(最短路)

D. Jzzhu and Cities
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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

题意:
给出n个城市, 标号1-n.
给出m个边, 是两个城市之间的道路.
给出k个铁路, 是1-其他城市的铁路.

问最多删除多少条铁路, 保证1到其他城市的最短路径的长度不变.

解题思路:
先标记上铁路的长度, 然后跑一边spfa, 如果这条铁路能被松弛, 那么这条铁路便可以被删除.

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f3f3f3f3f;
int n, m, k, res;
int d[maxn];
bool vis[maxn], train[maxn];
vector<pair<int, int> > mp[maxn];
void init()
{
    cin >> n >> m >> k;

    while(m--){
        int u, v, x;
        cin >> u >> v >> x;
        mp[v].push_back(make_pair(u, x));
        mp[u].push_back(make_pair(v, x));
    }

    for(int i = 1; i <= n; i++){
        d[i] = INF;
    }
    res = k;
}

void solve()
{
    queue<int> Q;
    d[1] = 0;
    vis[1] = 1;
    Q.push(1);

    while(k--){
        int u, x;
        cin >> u >> x;
        if(d[u] > x){
            d[u] = x;
            train[u] = 1;
            if(!vis[u]){
                vis[u] = 1;
                Q.push(u);
            }
        }
    }

    while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        for(int i = 0; i < mp[u].size(); i++){
            int v = mp[u][i].first;
            int x = mp[u][i].second;
            if(d[v] >= d[u] + x && train[v]){
                train[v] = 0;
            }
            if(d[v] > d[u] + x)
            {
                d[v] = d[u] + x;
                if(!vis[v]){
                    vis[v] = 1;
                    Q.push(v);
                }
            }
        }
    }
    for(int i = 1; i <= n; i++){
        res -= train[i];
    }
    cout << res;
}

main()
{
    ios::sync_with_stdio(0);
    init();
    solve();
    return 0;
}
### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值