链接:

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(单源最短路,思维)的更多相关文章

  1. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  2. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  3. 用scheme语言实现SPFA算法(单源最短路)

    最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...

  4. 单源最短路_SPFA_C++

    当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...

  5. 【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条无向边的图,每条边权都为正.令 ...

  6. 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home

    https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...

  7. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  8. 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结

    刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...

  9. PAT All Roads Lead to Rome 单源最短路

    思路:单源最短路末班就好了,字符串映射成数字处理. AC代码 //#define LOCAL #include <stdio.h> #include <string.h> #i ...

随机推荐

  1. iOS多选实现注意点

    下面对APP的多选选择列表实现进行总结,为了在以后的每个项目的多选实现,测试总是提一样的bug总结的. 具体的实现代码就不复制粘贴了,不过在多选问题上遇到问题的可以我们一起讨论讨论的哈... 可能总结 ...

  2. 封装cookie,自定义过期时间,domain,path

    在使用Cookie进行存储的时候,遇到了许多不可思议的bug,特地标识出来,以作总结. 是这样一个项目,登录是放在官网进行操作的,而登录进入的是后台,后台和官网属于同一域名的不同目录,那么常规进行co ...

  3. 【HANA系列】SAP 【第一篇】EXCEL连接SAP HANA的方法(ODBC)

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP [第一篇]EXCEL连接 ...

  4. #Java学习之路——基础阶段二(第十四篇)

    我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...

  5. 简述Js中,判断对象为空对象的几种方式

    1.空对象.空引用以及undefined三种概念的区别 空对象:是对象,但它的值是指向没有任何属性的对象的引用, {}是指 不含任何属性 的对象,当然对象属性包括 字面值和函数: 空引用:obj=nu ...

  6. python 并发编程 多线程 GIL全局解释器锁基本概念

    首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念. 就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代码. ...

  7. 爬取网易云音乐评论!python 爬虫入门实战(六)selenium 入门!

    说到爬虫,第一时间可能就会想到网易云音乐的评论.网易云音乐评论里藏了许多宝藏,那么让我们一起学习如何用 python 挖宝藏吧! 既然是宝藏,肯定是用要用钥匙加密的.打开 Chrome 分析 Head ...

  8. adb 打印kernel输出的log

     一. linux 内核printk机制     1.1. Android内核是基于Linxu kernel的,因此其log机制也是通用的,在Android内核中使用printk函数进行Log输出.与 ...

  9. x系统清理/tmp/文件夹的原理

    转自:http://www.opsers.org/base/clean-up-on-the-linux-system-tmp-folder-you-may-want-to-know.html§ 我们知 ...

  10. 关于js计算非等宽字体宽度的方法

    准备一个容器 首先在body外插入一个absolute的容器避免重绘: const svgWidthTestContainer = document.createElement('svg'); svg ...