Codeforces 449 B. Jzzhu and Cities
堆优化dijkstra,假设哪条铁路能够被更新,就把相应铁路删除。
2 seconds
256 megabytes
standard input
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 mroads
connecting the cities. One can go from city ui to vi (and
vise versa) using the i-th road, the length of this road is xi.
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 si (and
vise versa), the length of this route is yi.
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.
The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105).
Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109).
Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 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 a single integer representing the maximum number of the train routes which can be closed.
5 5 3
1 2 1
2 3 2
1 3 3
3 4 4
1 5 5
3 5
4 5
5 5
2
2 2 3
1 2 2
2 1 3
2 1
2 2
2 3
2
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector> using namespace std; typedef long long int LL;
typedef pair<LL,int> pLI;
typedef pair<int,LL> pIL; const int maxn=210000;
const LL INF=1LL<<60; int n,m,k;
vector<pIL> edge[maxn]; LL dist[maxn];
bool train[maxn]; int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<m;i++)
{
int u,v;LL x;
//scanf("%d%d%lld",&u,&v,&x);
scanf("%d%d%I64d",&u,&v,&x);
u--; v--;
edge[u].push_back(make_pair(v,x));
edge[v].push_back(make_pair(u,x));
}
for(int i=0;i<=n;i++)
{
dist[i]=INF;
train[i]=false;
}
dist[0]=0;
for(int i=0;i<k;i++)
{
int s; LL y;
//scanf("%d%lld",&s,&y);
scanf("%d%I64d",&s,&y);
s--;
train[s]=true;
dist[s]=min(dist[s],y);
}
priority_queue<pLI> heap;
for(int i=0;i<n;i++)
{
if(dist[i]!=INF)
{
heap.push(make_pair(-dist[i],i));
}
}
while(heap.size())
{
pLI temp=heap.top(); heap.pop();
LL D=-temp.first;
int u=temp.second;
if(dist[u]!=D) continue;
for(int i=0,sz=edge[u].size();i<sz;i++)
{
int v=edge[u][i].first;
LL len=edge[u][i].second;
if(dist[v]>=dist[u]+len)
{
if(train[v]==true)
{
train[v]=false;
}
}
if(dist[v]>dist[u]+len)
{
dist[v]=dist[u]+len;
heap.push(make_pair(-dist[v],v));
}
}
}
int ans=k;
for(int i=0;i<n;i++)
{
ans-=train[i];
}
printf("%d\n",ans);
return 0;
}
Codeforces 449 B. Jzzhu and Cities的更多相关文章
- Codeforces 450D:Jzzhu and Cities(最短路,dijkstra)
D. Jzzhu and Cities time limit per test: 2 seconds memory limit per test: 256 megabytes input: stand ...
- Codeforces 449.C Jzzhu and Apples
C. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces C. Jzzhu and Cities(dijkstra最短路)
题目描述: Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 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 CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...
- CF449B Jzzhu and Cities 迪杰斯特拉最短路算法
CF449B Jzzhu and Cities 其实这一道题并不是很难,只是一个最短路而已,请继续看我的题解吧~(^▽^) AC代码: #include<bits/stdc++.h> #d ...
- [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 Round #257(Div.2) D Jzzhu and Cities --SPFA
题意:n个城市,中间有m条道路(双向),再给出k条铁路,铁路直接从点1到点v,现在要拆掉一些铁路,在保证不影响每个点的最短距离(距离1)不变的情况下,问最多能删除多少条铁路 分析:先求一次最短路,铁路 ...
随机推荐
- [Android 4.4.2] 泛泰A870 Mokee4.4.2 20140531 RC1.0 by syhost
欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...
- PHP回调函数--call_user_func_array
我这是抄的 感谢 https://www.cnblogs.com/zzl-21086595/p/4547519.html 全局函数的回调 这里的全局函数的意思,是直接使用function定义的函数,它 ...
- sprinng in action 第四版第六章中的ValidationMessages.properties不起作用
文件名必须是ValidationMessages.properties,必须放在类的根目录下
- (转)Windows Server 2008 R2 域控制器部署指南
转自:https://technet.microsoft.com/zh-cn/cloud/gg462955.aspx 一.域控制器安装步骤: 1.装 Windows Server 2008 R2并配置 ...
- 2016最新CocoaPods安装与使用
前言 是不是已经厌烦了将各种库拖拽到Xcode项目中?那么,CocoaPods的出现就帮你解决了这一问题.CocoaPods是Objective-C项目中最有名的类库管理工具,可以解决库与库之间的依赖 ...
- javascript 调用C++函数
分3步: 一>实现IDispatch 接口 #ifndef _IDISPIMP_H_ #define _IDISPIMP_H_ // idispimp.h class CImpIDispatch ...
- Android开发之SpannableString具体解释
在实际的应用开发过程中常常会遇到.在文本的不同部分显示一些不同的字体风格的信息如:文本的字体.大小.颜色.样式.以及超级链接等. 普通情况下,TextView中的文本都是一个样式.对于类似的情况.能够 ...
- $.getJSON 跨域
//支持跨域 $.getJSON(url + '&callback=?', function(res) { if (res.status === 0) { console.log(res.re ...
- 第一次使用docker for windows 遇到的坑
原文:第一次使用docker for windows 遇到的坑 1. 目前win10安装docker, 不需要安装其他工具,可直接到官网下载 2. 此版本的docker可同时运行Windows con ...
- 嵌入式linux串口设置(一)
在linux中,所有的设备文件一般都位于“/dev”下,串口1和串口2对应的设备名依次为“/dev/ttyS0”, “/dev/ttyS1”,而且USB转串口的设备名通常为“/dev/ttyUSB0” ...