Codeforces 449 B. Jzzhu and Cities(图论dijk)

题目要求在保持每个城市到首都的最短路径不变的前提下,关闭尽可能多的铁路路线。给定城市数量、道路和铁路信息,使用Dijkstra算法求解。初始尝试的解决方案在大规模数据下效果不佳,最终采用增加记录入度的边并遍历铁路的方法,若铁路长度大于最短路径则必须删除,等于且入度不为1时也可删除,以此计算可关闭的铁路路线数量。
摘要由CSDN通过智能技术生成

B. Jzzhu and Cities

time limit per test

2 seconds

memory limit per test

256 megabytes

input
standard input
output
standard 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 u i to v i (and vise versa) using the i -th road, the length of this road is x i . 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 s i (and vise versa), the length of this route is y i .

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 ≤ 10 5 ; 1 ≤ m ≤ 3·10 5 ; 1 ≤ k ≤ 10 5 ) .

Each of the next m lines contains three integers u i , v i , x i (1 ≤ u i , v i ≤ n ; u i ≠ v i ; 1 ≤ x i ≤ 10 9 ) .

Each of the next k lines contains two integers s i and y i (2 ≤ s i ≤ n ; 1 ≤ y i ≤ 10 9 ) .

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.

题意:
有n个城市 从1到n,其中1为首都。每个城市之间有很多条道路,1到其他城市之间有一些铁轨。现在要在最短路不变的情况下,删掉尽量多的铁轨。输出删掉的铁轨数。

真·从早wa到晚,最后换了一种写法= =。。。就很难过
一开始的想法是把所有边都加进去,然后dijk,但在更新dis值得时候多增加一个数组rout[u]记录当前这个点的最短路径上有多少条铁轨。
如果dis[son]>dis[father]+eg[i].w 那么就根据i是不是铁轨更新这个rout
如果dis[son]==dis[father]+eg[i].w 那么就从看是原先rout[son]小还是通过边i 从father转移过来更小,依此来更新rout[son],

同时每次更新都要记录这个点的最短路是由哪个边转移过来的。

最后统计所有点所记录的边,如果是铁轨边那么就tot++;
tot是保留下来的铁轨数
ans就是k-tot;

自我感觉好像没得毛病哈。。。。。

测一些小数据基本都能过,但一旦大了 ans就会偏小。。。。
现在都还没想通。。。
代码贴出来有大佬愿意看的话麻烦告诉我猫饼在哪儿QAQ

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
struct Pair{
    int D,V;
    Pair(long long d,int v){
        D=d,V=v;
    }
    friend bool operator< (c
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值