链接:

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. 如何将txt文件转换为带章节目录的mobi文件

    txt文件基本没什么排版可言.所以想要把txt转换为mobi文件方便阅读. 具体步骤如下: 打开txt 用notepad++打开所需要转换的txt文件.(或者使用其他的能够支持正则表达式的编辑器). ...

  2. Message的定义類型

    SAP通过Message来回执程序的执行状态.使用Tcode:SE91. SAP將Message分为不同的类,如下图显示为ZF环境下ZMM01类相关Message列表. Message short t ...

  3. Bresenham’s algorithm( 布兰森汉姆算法)画直线

    Bresenham直线算法是用来描绘由两点所决定的直线的算法,它会算出一条线段在 n 维光栅上最接近的点.这个算法只会用到较为快速的整数加法.减法和位元移位,常用于绘制电脑画面中的直线.是计算机图形学 ...

  4. java:LeakFilling (SQL,JDBC)

    1.JDBC中的sql里面不能加 :号,否则报错 2.Oracle数据必须提交后才可以使用JDBC进行操作,否则没有结果 3. JDBC插入序列: 首先在sequences建一个序列 insert i ...

  5. ios系统App Store安装包下载链接获取

    今天将自己开发的Android版本和ios版本的安装包通过生成二维码的方式展示在H5页面上,Android版的比较简单,但是ios的安装包用户必须从App Store(苹果应用市场)中下载安装,所以获 ...

  6. jenkins pipline和jenkinsfile

    Jenkins Pipeline(或简称为 "Pipeline")是一套插件,将持续交付的实现和实施集成到 Jenkins 中. Jenkins Pipeline 提供了一套可扩展 ...

  7. [Python3] 027 常用模块 time

    目录 time 1. 时间戳 2. UTC 时间 3. 夏令时 4. 时间元组 5. 举例 5.1 例子1 例子2 例子3 例子4 例子5 例子6 例子7 time 1. 时间戳 一个时间表示,根据不 ...

  8. 面试题 | 数据库笔试题集合·之·SQL语句(2)

    第2章 SQL 语句 2.1 选择2.1.1 DELETE FROM S WHERE 年龄>60 语句的功能是( A ) A.从 S 表中彻底删除年龄大于 60 岁的记录B.S 表中年龄大于 6 ...

  9. Python和mysql的连接

    python与mysql的连接: 说明:前提是已近安装了mysql以及可视化工具(本人装的是Navicat) 1.在cmd下下载Python的第三方数据库包:pip install pymysql: ...

  10. 手把手教你vue配置请求本地json数据

    本篇文章主要介绍了vue配置请求本地json数据的方法,分享给大家,具体如下:在build文件夹下找到webpack.dev.conf.js文件,在const portfinder = require ...