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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章