Codeforces C. Jzzhu and Cities(dijkstra最短路)
题目描述:
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&°[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最短路)的更多相关文章
- Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)
D. Jzzhu and Cities time limit per test: 2 seconds memory limit per test: 256 megabytes input: stand ...
- CF449B Jzzhu and Cities (最短路)
CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...
- [Codeforces 449B] Jzzhu and Cities
[题目链接] https://codeforces.com/contest/449/problem/B [算法] 最短路 时间复杂度 : O(N ^ 2) [代码] #include<bits/ ...
- codeforces 449B Jzzhu and Cities (Dij+堆优化)
输入一个无向图<V,E> V<=1e5, E<=3e5 现在另外给k条边(u=1,v=s[k],w=y[k]) 问在不影响从结点1出发到所有结点的最短路的前提下,最多可以 ...
- Codeforces 450D Jzzhu and Cities [heap优化dij]
#include<bits/stdc++.h> #define MAXN 100050 #define MAXM 900000 using namespace std; struct st ...
- 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 ...
- CF449B Jzzhu and Cities 迪杰斯特拉最短路算法
CF449B Jzzhu and Cities 其实这一道题并不是很难,只是一个最短路而已,请继续看我的题解吧~(^▽^) AC代码: #include<bits/stdc++.h> #d ...
- Codeforces 449 B. Jzzhu and Cities
堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除. B. Jzzhu and Cities time limit per test 2 seconds memory limit per ...
- Dijkstra最短路算法
Dijkstra最短路算法 --转自啊哈磊[坐在马桶上看算法]算法7:Dijkstra最短路算法 上节我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最 ...
随机推荐
- php异常处理小总结
2019年8月23日10:56:31 php很多开发不习惯使用异常处理,因为web开发,重在于快速开发,易用性,高性能,不强调程序健壮性 php的异常使用其实不是太完善,易用性也差点,当然这个对比其他 ...
- 【论文阅读】FaceBoxes- CPU Real-time Face Detector with High Accuracy
前言 参考 1. FaceBoxes_paper; 2. Faceboxes_github_tf; 3. 翻译: 4. 理解1: 5. 理解2: 完
- suanec-rotatelogs
简介 一个简单的日志滚动器 业界已有大量优秀的日志滚动工具来限制日志大小 本工具只是仿制了Apache httpd中的rotatelogs 不同的是,工作模式参考了yarn namenode日志的管理 ...
- SpringBoot小技巧:Jar包换War包
SpringBoot小技巧:Jar包换War包 情景 我们都知道springBoot中已经内置了tomcat,是不需要我们额外的配置tomcat服务器的,但是有时这也可能是我们的一个瓶颈,因为如果我们 ...
- iperf—流量测试
iperf是另外一款用于流量测试的软件,主要运行于Windows系统和安卓系统的手机/PAD(IOS系统下载需要收费). 一个工作在Server模式,另外一个工作在Client模式,输入Server的 ...
- 右键管理员身份打开 命令行cmd
添加到注册表将下面命令保存为reg文件: Windows Registry Editor Version 5.00 [-HKEY_CLASSES_ROOT\Directory\shell\runas] ...
- DDD/CQRS模式,微服务,容器
DDD/CQRS模式,微服务,容器 https://docs.microsoft.com/zh-cn/previous-versions/msp-n-p/ee658109(v=pandp.10) We ...
- CMDB资产采集的四种方式
转 https://www.cnblogs.com/guotianbao/p/7703921.html 资产采集的概念 资产采集的四种方式:Agent.SSH.saltstack.puppet 资产采 ...
- remote origin already exists解决办法
如图翻译过来就是:致命:远程来源已经存在 此时,我们可以先 git remote -v 查看远程库信息: 可以看到,本地库已经关联了origin的远程库,并且,该远程库指向GitHub. 解决办法如下 ...
- spring mvc 处理pojo传递对象时该对象继承父类的属性在网络接收端接收该属性值总是null,why?
//=========================== 情形一: ===============================//在网络上传递User1类对象时info属性值在网络的另一端能够接 ...