CodeForces-449B(单源最短路,思维)
链接:
https://vjudge.net/problem/CodeForces-449B
题意:
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 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.
思路:
先求到每个点的最短路,同时记录到每个点的最短路有几条.
最后比较每条火车道,是否可以删除.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10;
const long long INF = 1e15;
struct Edge
{
int to;
LL v;
};
struct HeapNode
{
int to;
LL dis;
bool operator < (const HeapNode& that) const
{
return this->dis > that.dis;
}
};
vector<Edge> G[MAXN];
int In[MAXN], Vis[MAXN];
LL Dis[MAXN];
int To[MAXN], Va[MAXN];
int n, m, k;
void Dij()
{
memset(Vis, 0, sizeof(Vis));
memset(In, 0, sizeof(In));
In[1] = 1;
for (int i = 1;i <= n;i++)
Dis[i] = INF;
Dis[1] = 0;
priority_queue<HeapNode> que;
que.push(HeapNode{1, 0LL});
while (!que.empty())
{
HeapNode node = que.top();
que.pop();
if (Vis[node.to])
continue;
Vis[node.to] = 1;
for (int i = 0;i < G[node.to].size();i++)
{
int ne = G[node.to][i].to;
if (Vis[ne])
continue;
LL va = G[node.to][i].v;
if (Dis[ne] == Dis[node.to]+va)
In[ne]++;
if (Dis[ne] > Dis[node.to]+va)
{
In[ne]=1;
Dis[ne] = Dis[node.to]+va;
}
que.push(HeapNode{ne, Dis[ne]});
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int u, v, w;
cin >> n >> m >> k;
for (int i = 1;i <= m;i++)
{
cin >> u >> v >> w;
G[u].push_back(Edge{v, w});
G[v].push_back(Edge{u, w});
}
for (int i = 1;i <= k;i++)
{
cin >> v >> w;
To[i] = v, Va[i] = w;
G[1].push_back(Edge{v, w});
}
Dij();
// for (int i = 1;i <= n;i++)
// cout << Dis[i] << ' ' ;
// cout << endl;
int num = 0;
for (int i = 1;i <= k;i++)
{
if (Dis[To[i]] < Va[i])
num++;
else if (Dis[To[i]] == Va[i] && In[To[i]] > 1)
num++, In[To[i]]--;
}
cout << num << endl;
return 0;
}
CodeForces-449B(单源最短路,思维)的更多相关文章
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- 用scheme语言实现SPFA算法(单源最短路)
最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...
- 单源最短路_SPFA_C++
当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...
- 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)
题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...
- 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home
https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结
刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...
- PAT All Roads Lead to Rome 单源最短路
思路:单源最短路末班就好了,字符串映射成数字处理. AC代码 //#define LOCAL #include <stdio.h> #include <string.h> #i ...
随机推荐
- Servlet的API
(1)Servlet接口中的方法 1)init(ServletConfig arg0) 何时执行:servlet对象创建的时候执行 ServletConfig : 代表的是该servlet对象的配置信 ...
- Java Stream流排序null以及获取指定条数数据
Java8的Stream流的一些用法, //排序 carerVehEntityList = carerVehEntityList.stream().sorted( Comparator.compari ...
- 内容与org.apache.axis2.AxisFault: Connection refused: connect 没有太大的关系
昨天下午 在SVN 检索下来项目 运行的时候遇到一个问题: 然后检查半天访问路径 再运行 居然又出现这个问题 然后又是一顿操作............. 网上找了半天方法 好多人说是 这样的 ...
- Leetcode基础篇30天30题系列之数组:模拟计算法
作者:丁宋涛 数组:加一 题干: 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整 ...
- ${pagecontext.request.contextpath}绝对路径理解
${pageContext.request.contextPath}是JSP取得绝对路径的方法,等价于<%=request.getContextPath()%> .也就是取出部署的应用程序 ...
- springBoot2.0使用@ImportResource引入spring配置文件.xml
1. 编写spring配置文件.xml 这里是bean.xml <?xml version="1.0" encoding="UTF-8"?> < ...
- python-day14(正式学习)
目录 三元表达式 列表推导式 字典生成式 zip()方法 生成器 yield关键字 迭代套迭代 send(value) close() throw() 自定义range方法 生成器表达式 匿名函数 与 ...
- mysql 聚合函数(2)
平均 svg select avg(sal + IFNULL(comm,0)) as avg_sal from t_emp 总和 sum select sum(sal + IFNULL(comm,0) ...
- 编写python爬虫采集彩票网站数据,将数据写入mongodb数据库
1.准备工作: 1.1安装requests: cmd >> pip install requests 1.2 安装lxml: cmd >> pip install lxml ...
- React-native 底部导航栏(二)
1.组件安装:npm install react-native-router-flux --save 2.定义菜单图片和文字: import React, { Component } from 're ...