题目描述:

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 ≤ 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

Copy

5 5 31 2 12 3 21 3 33 4 41 5 53 54 55 5

Output

Copy

2

Input

Copy

2 2 31 2 22 1 32 12 22 3

Output

Copy

2

思路:

这道题是要给一个交通网络图,实际上只从1点出发,到其余各个点的最短路径,也就是单源最短路径。然后从1点到某些城市有特殊的边可以直达,路径长度也已知。这些条件给定后,现在1点到各个点的最短路径就实际上已经知道了。现在要删除一些特殊边,只要能保证删除连向某点的特殊边后,从1到这个点的最短距离不发生变化。求最多能删多少条这样的特殊边。

分析一下,现在已知1到其余点的最短距离,考虑对每一个特殊边,设这条边直连v点,长度为w.

\(1\).w>d[v](d为最短路径)的话说明我到v点的最短路径不是通过这条边的,因此这条边可以直接删除。

\(2\).若w==d[v],也就是我到v的最短路径可以通过这条边到达,那到底能不能删这条边呢?如果说1就只有一条最短路径到v,也就是到v最短路径只特殊边这一条,那么显然不能删这条边,因为如果删掉1到v的长度会增大。如果1到v最短路径有多条,也就是除了特殊边直达外我还可以通过其它边最短到v,那么这条特殊边就可以删去,而不会影响1到v的最短路径。

于是需要记录到每个点的最短路径条数。实现的话就要在dijkstra算法进行路径松弛的时候记录分情况下最短路的条数,有些技巧性的更新。

注意的是最后在遍历特殊边的时候,进行删除到达v的特殊边时要记得实时更新v的最短路径条数。

代码:

#include <iostream>
#include <vector>
#include <cstdio>
#include <climits>
#include <queue>
#define max_n 100005
#define INF LLONG_MAX
using namespace std;
typedef pair<long long,long long> PLL;
int n,m,k;
vector<PLL> G[max_n];
int num = 0;
long long d[max_n];//最短距离
long long deg[max_n];//最短路条数
vector<PLL> train;//特殊边
//int used[max_n<1];
/*void dijkstra(int s)
{
fill(d,d+max_n,INF);
fill(used,used+max_n,0);
d[s] = 0;
int v;
while(true)
{
int v = -1;
for(int u = 0;u<n;u++)
{
if(!used[u]&&(v==-1)||d[u]<d[v])
{
v = u;
}
}
if(v==-1) break;
used[v] = 1;
for(int u = 0;u<n;u++)
{
d[u] = min(d[u],d[v]+cost[v][u]);
}
}
}*/
void dijkstra(int s)
{
priority_queue<PLL,vector<PLL>,greater<PLL> > que;
fill(d,d+max_n,INF);
d[s] = 0;
que.push(PLL(0,s));
while(!que.empty())
{
PLL p = que.top();
que.pop();
int v = p.second;
if(d[v]<p.first) continue;
for(int i = 0;i<G[v].size();i++)
{
PLL edge = G[v][i];
if(d[edge.first]==d[v]+edge.second)//相等最短路数目加一
{
deg[edge.first]++;
}
if(d[edge.first]>d[v]+edge.second)//更新则最短路数目刷新为一
{
d[edge.first] = d[v]+edge.second;
que.push(PLL(d[edge.first],edge.first));
deg[edge.first]=1;
}
}
}
}
int main()
{
cin >> n >> m >> k;
for(int i = 0;i<m;i++)
{
int u,v;
long long w;
cin >> u >> v >> w;
G[u].push_back(PLL(v,w));
G[v].push_back(PLL(u,w));
}
for(int i = 0;i<k;i++)
{
long long to,w;
cin >> to >> w;
train.push_back(PLL(to,w));
G[1].push_back(PLL(to,w));
}
dijkstra(1);
for(int i = 0;i<train.size();i++)
{
long long v = train[i].first;
long long w = train[i].second;
if(d[v]<w)
{
num++;
}
if(d[v]==w&&deg[v]>1)
{
num++;
deg[v]--;
}
}
cout << num << endl;
return 0;
}

参考文章:

围巾的ACM,Codeforces 449B Jzzhu and Cities(最短路),https://blog.csdn.net/qq_21057881/article/details/50602265

Codeforces C. Jzzhu and Cities(dijkstra最短路)的更多相关文章

  1. Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)

    D. Jzzhu and Cities time limit per test: 2 seconds memory limit per test: 256 megabytes input: stand ...

  2. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  3. [Codeforces 449B] Jzzhu and Cities

    [题目链接] https://codeforces.com/contest/449/problem/B [算法] 最短路 时间复杂度 : O(N ^ 2) [代码] #include<bits/ ...

  4. codeforces 449B Jzzhu and Cities (Dij+堆优化)

    输入一个无向图<V,E>    V<=1e5, E<=3e5 现在另外给k条边(u=1,v=s[k],w=y[k]) 问在不影响从结点1出发到所有结点的最短路的前提下,最多可以 ...

  5. Codeforces 450D Jzzhu and Cities [heap优化dij]

    #include<bits/stdc++.h> #define MAXN 100050 #define MAXM 900000 using namespace std; struct st ...

  6. Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路

    D. Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. CF449B Jzzhu and Cities 迪杰斯特拉最短路算法

    CF449B Jzzhu and Cities 其实这一道题并不是很难,只是一个最短路而已,请继续看我的题解吧~(^▽^) AC代码: #include<bits/stdc++.h> #d ...

  8. Codeforces 449 B. Jzzhu and Cities

    堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除. B. Jzzhu and Cities time limit per test 2 seconds memory limit per ...

  9. Dijkstra最短路算法

    Dijkstra最短路算法 --转自啊哈磊[坐在马桶上看算法]算法7:Dijkstra最短路算法 上节我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最 ...

随机推荐

  1. dva+umi+antd项目从搭建到使用

    先创建一个新项目,具体步骤请参考https://www.cnblogs.com/darkbluelove/p/11338309.html 一.添加document.ejs文件(参考文档:https:/ ...

  2. k8s-job使用

    一.job特性 运行完成后退出,但是不会被删除,便于用户查看日志信息,了解任务完成的情况 删除job时产生的pod也会被一起删除 job中可以运行多个pod(任务执行多次),且可以并行运行缩短任务完成 ...

  3. 【springboot】【idea】实体类免写get、set等方法,使用lombok依赖和插件的@Data类注解

    需求,一个实体类,规范写法一定要对应的get.set方法,有必要还要重写toString方法.虽然可以快速生成get.set等方法,但是如果要添加或减少成员属性时就得重新生成get.set等方法. 而 ...

  4. centos7修改主机名的方法

    在CentOS7中,有三种定义的主机名: 静态的(Static hostname) “静态”主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名. 瞬态的(Tansie ...

  5. turtle1

    #画风车 from turtle import * pensize(2) for i in range(4): seth(360-i*90) fd(150) rt(90) circle(-150,45 ...

  6. JDK8从永生代到元数据区

    永生代 永生代默认的最大内存大小是在32位JVM上为64MB, 在64位JVM上为82MB.可以通过-XX:PermSize=[size]和-XX:MaxPermSize=[size]来调整. 永生代 ...

  7. [转帖]8个最佳Docker容器监控工具,收藏了

    8个最佳Docker容器监控工具,收藏了 https://www.sohu.com/a/341156793_100159565?spm=smpc.author.fd-d.9.1574127778732 ...

  8. Python之路【第十二篇】:Python面向对象高级

    一.反射 1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究 ...

  9. RT1052 BootLoader总结

    RT1052 BootLoader总结‍ 概述 Bootloader涉及到的RT1052单片机资源有:Cache,ram,外部SDRAM,ARM7汇编指令,外部dataFlash. 升级功能涉及到的其 ...

  10. 配置安全web服务

    为站点 http://system1.group8.example.com 配置TLS加密: 1.一个已签名证书从 http://server.group8.example.com/pub/tls/c ...