D. 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\cdot10^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.

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

题意

一个城市中有 \(m\) 条公路和 \(k\) 条铁路,每条铁路都和起点相连。现在要求在不改变起点到各点最短路径长度的情况下,拆除一些铁路,问最多可以拆除多少条铁路

思路

将公路和铁路放在一起建图,然后去跑最短路,在跑最短路的过程中记录一下每个点的入度(该点被多少条最短路径包含)

铁路可以删除的条件:

  1. 如果起点到该点的最短路径和起点到该点的铁路长度相等,判断该点的入读是否大于 \(1\),如果大于 \(1\),这条铁路也是可以删除的(能够到达该点的最短路径不止一条)
  2. 起点到该点的最短路径小于起点到该点的铁路的长度

代码

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
struct edge
{
int to,Next;
ll value;
}Edge[maxn];
int head[maxn];
int tot;
inline void add_edge(int u,int v,ll w)
{
Edge[tot].to=v;
Edge[tot].Next=head[u];
Edge[tot].value=w;
head[u]=tot++;
}
struct node
{
int u;
ll d;
bool operator < (const node & dui) const{return d>dui.d;}
};
int ss[maxn];
ll yy[maxn];
ll dis[maxn];
int in[maxn];
inline void dijkstra(int s)
{
priority_queue<node>que;
que.push(node{s,0});
dis[s]=0;
while(!que.empty())
{
node res=que.top();
que.pop();
int u=res.u;ll d=res.d;
if(d!=dis[u])
continue;
for(int i=head[u];~i;i=Edge[i].Next)
{
int v=Edge[i].to;
ll w=Edge[i].value;
if(dis[v]==dis[u]+w)
in[v]++;
if(dis[v]>dis[u]+w)
in[v]=1,dis[v]=dis[u]+w,que.push(node{v,dis[v]});
}
}
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/wzy/in", "r", stdin);
freopen("/home/wzy/out", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,k;
cin>>n>>m>>k;
for(int i=0;i<=n;i++)
dis[i]=INF;
ms(head,-1);
int x,y;
ll z;
while(m--)
cin>>x>>y>>z,add_edge(x,y,z),add_edge(y,x,z);
for(int i=0;i<k;i++)
cin>>ss[i]>>yy[i],add_edge(1,ss[i],yy[i]),add_edge(ss[i],1,yy[i]);
dijkstra(1);
ll ans=0;
for(int i=0;i<k;i++)
{
if(yy[i]==dis[ss[i]]&&in[ss[i]]>1)
ans++,in[ss[i]]--;
if(yy[i]>dis[ss[i]])
ans++;
}
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s."<<endl;
#endif
return 0;
}

Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)的更多相关文章

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

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

  2. Codeforces C. Jzzhu and Cities(dijkstra最短路)

    题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  3. Codeforces.567E.President and Roads(最短路 Dijkstra)

    题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...

  4. Codeforces.1051F.The Shortest Statement(最短路Dijkstra)

    题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...

  5. Codeforces Gym101502 I.Move Between Numbers-最短路(Dijkstra优先队列版和数组版)

    I. Move Between Numbers   time limit per test 2.0 s memory limit per test 256 MB input standard inpu ...

  6. [Codeforces 449B] Jzzhu and Cities

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

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

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

  8. Codeforces 715B. Complete The Graph 最短路,Dijkstra,构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF715B.html 题解 接下来说的“边”都指代“边权未知的边”. 将所有边都设为 L+1,如果dis(S,T ...

  9. 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 ...

随机推荐

  1. SpringBoot整合Shiro 三:整合Mybatis

    搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 整合Mybatis 添加Maven依赖 mysql.dr ...

  2. LInkedList总结及部分底层源码分析

    LInkedList总结及部分底层源码分析 1. LinkedList的实现与继承关系 继承:AbstractSequentialList 抽象类 实现:List 接口 实现:Deque 接口 实现: ...

  3. 数仓:解读 NameNode 的 edits 和 fsimage 文件内容

    一.edits 文件 一)文件组成 一个edits文件记录了一次写文件的过程,该过程被分解成多个部分进行记录:(每条记录在hdfs中有一个编号) 每一个部分为: '<RECORD>...& ...

  4. Kafka(一)【概述、入门、架构原理】

    目录 一.Kafka概述 1.1 定义 二.Kafka快速入门 2.1 安装部署 2.2 配置文件解析 2.3Kafka群起脚本 2.4 topic(增删改查) 2.5 生产和消费者命令行操作 三.K ...

  5. 'this' pointer in C++

    The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is avai ...

  6. Equinox OSGi服务器应用程序的配置步骤 (支持JSP页面)

    本文介绍在Eclipse里如何配置一个简单的基于Eclipse Equinox OSGi实现的Web应用程序,在它的基础上可以构造更加复杂的应用,本文使用的是Eclipse 3.3.1版本,如果你的E ...

  7. entfrm-boot开发平台功能介绍【entfrm开源模块化无代码开发平台】

    简介 entfrm开发平台,是一个以模块化为核心的无代码开发平台,是一个集PC和APP快速开发.系统管理.运维监控.开发工具.OAuth2授权.可视化数据源管理与数据构建.API动态生成与统计.工作流 ...

  8. 编译安装haproxy2.0

    先解决lua环境,(因为centos自带的版本不符合haproxy要求的最低版本(5.3)先安装Lua依赖的包 [root@slave-master lua-5.3.5]# yum install  ...

  9. Javascript 数组对象常用的API

    常用的JS数组对象API ES5及以前的Api ECMAScript5为数组定义了5个迭代方法,每个方法接收两个参数, 一个是每项运行的函数,一个是运行该函数的作用域对象(可选项),传入这些方法的函数 ...

  10. Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录

    背景 :在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnectio ...