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)不变的情况下,问最多能删除多少条铁路 分析:先求一次最短路,铁路 ...
随机推荐
- Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android
在写自己定义的view时,有时会报下面错误: Caused by: java.lang.NoSuchMethodException: <init> [class android.co ...
- bow lsa plsa
Bag-of-Words (BoW) 模型是NLP和IR领域中的一个基本假设.在这个模型中,一个文档(document)被表示为一组单词(word/term)的无序组合,而忽略了语法或者词序的部分.B ...
- Summary Day30
1.内存管理 1.1 进程中的内存区域划分 代码区 仅仅读常理区 全局区 BSS 堆 栈 1.2 字符串存储形式之间的比較 字符指针,字符数组.字符动态内存 1.3 虚拟内 ...
- 三、链路追踪系统 zipkin
一.构建项目 用到的依赖直接看pom.xml的注释吧 <?xml version="1.0" encoding="UTF-8"?> <proj ...
- [转]在Win7 64位注册ActiveX控件
原文作者: K_H_H 原文地址: http://blog.sina.com.cn/s/blog_56b96c5a0100ztc7.html 首先必须以管理员身份运行cmd.exe,即在cmd.ex ...
- php实现 称砝码(背包)
php实现 称砝码(背包) 一.总结 一句话总结: 1.dp的实质是什么? 刷表啊,用空间换时间 把表画出来会做得更快 13 //动态规划就是一个表 14 //至于这个表的更新就是上面层的表更新下面层 ...
- 段的创建表user_segments 分类: H2_ORACLE 2013-08-10 11:13 714人阅读 评论(0) 收藏
1.段的定义及类型 Oracle中的段(segment)是占用磁盘空间的一个对象,最常见的段类型包括: l 聚簇cluster l 表table l 表分区 tablepartition l ...
- Java NIO的基本概念与使用
public class TestBuffer { /** * 一. 缓冲区 (Buffer):Java Nio中负责数据的 存取+缓冲就是数组.用于存储不同类型的数据 * * 根据类型不同(bool ...
- 解决win7系统不支持16位实模式汇编程序DOS执行的问题
这学期学习了汇编,在自己电脑上发现,win7的dos不支持16位实模式. 对编程来说,不能执行程序是致命的. 在经过网上搜集资料后,得到一种解决的方法--使用dosbox软件执行 dosbox简单说, ...
- freemarker自己定义标签报错(七)
1.错误描写叙述 六月 09, 2014 11:11:09 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template pr ...