题目描述:

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. element ui表格常用功能如:导出 新增 删除 多选 跨页多选 固定表头 多级表头 合并行列 等常见需求

    <template> <div class="table-cooperte"> <el-table :data="tableData&quo ...

  2. VUE 同一页面路由参数变化,视图不刷新的解决方案

    1.监听路由处理 watch: { $route(to, from) { // 逻辑 // 重新调用数据接口 } }, 2.beforeRouteUpdate导航守卫 路由更新时触发 beforeRo ...

  3. 前端传实体数组;后台list实体接收参数

    前端页面: $.ajax({ url: url, data: JSON.stringify(data), dataType: "json", contentType: " ...

  4. OpenVAS开源风险评估系统部署方案

    OpenVAS,即开放式漏洞评估系统,是一个用于评估目标漏洞的杰出框架.功能十分强大,最重要的是,它是“开源”的——就是免费的意思啦- 它与著名的Nessus“本是同根生”,在Nessus商业化之后仍 ...

  5. Jmeter在Http Rest接口中自动生成签名(Json格式请求参数)

    第一步: 签名的java类生成jar包,导入到jmeter的lib目录下(依赖的第三方包也要导入) 第二步:编写jmeter脚本,这里使用BeanShell 进行签名串的生成,目录结构如下: Bean ...

  6. 安装cnpm出现问题

    安装cnpm: 命令行中输入   npm install -g cnpm --registry=http://registry.npm.taobao.org 报:cnpm不是内部命令 解决方法:设置环 ...

  7. 长乐国庆集训Day5

    T1 方阵 题目 [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力钻研,发现秦始皇布置兵马俑是有一定规律的.兵马俑阵总共有n ...

  8. Shell编程入门基础上

    前言 为什么学 Shell Shell 脚本语言是实现 Linux/UNIX 系统管理及自动化运维所必备的重要工具, Linux/UNIX 系统的底层及基础应用软件的核心大都涉及 Shell 脚本的内 ...

  9. 【题解】Luogu P5328 [ZJOI2019]浙江省选

    原题传送门 看起来挺妙实际很暴力的一题 已知每个选手的分数都是平面上的直线 题目实际就是让我们求每条直线在整点处最大是第几大 我们考虑先对所有的直线进行半平面交(因为\(a_i\)都是正整数,所以比普 ...

  10. 魔法 [线段树优化DP]

    也许更好的阅读体验 \(\mathcal{Description}\) 小 \(D\) 正在研究魔法. 小 \(D\) 得到了远古时期的魔法咒语 \(S\),这个咒语共有 \(n\) 个音节,每个音节 ...