链接:

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. Redis 下载与配置window服务

    1.Redis下载 Git下载地址:https://github.com/MicrosoftArchive/redis/releases 2.配置Window服务 步骤一:在 Redis目录 输入 c ...

  2. mysql注入常用函数

    system_user()  系统函数名 user()   用户名 current_user()   当前用户名 session_user()    连接数据库的用户名 database()   数据 ...

  3. 【VS开发】【图像处理】基于灰度世界、完美反射、动态阈值等图像自动白平衡算法的原理、实现及效果

    基于灰度世界.完美反射.动态阈值等图像自动白平衡算法的原理.实现及效果      白平衡是电视摄像领域一个非常重要的概念,通过它可以解决色彩还原和色调处理的一系列问题.白平衡是随着电子影像再现色彩真实 ...

  4. DataGridViewCheckBoxColumn的Value值和EditFormatedValue值不一致

    今天要做一个代码修改DataGridViewCheckBoxColumn的Value值然后再遍历获取DataGridview选中项,因为遍历的时候为了能获取跟界面一致的选项,所以判断是否选中使用的是E ...

  5. 【转】应用宝基于Robotium自动化测试

    (转载:http://tmq.qq.com/2016/05/robotium_for_app_test/) 1 背景目的应用宝项目组采用FT(Feature Team)模式,整个项目组分为多个FT,而 ...

  6. 小记---------Hadoop的MapReduce基础知识

    MapReduce是一种分布式计算模型,主要用于搜索领域,解决海量数据的计算问题 MR由两个阶段组成:Map和Reduce,用户只需要实现map()和reduce()两个函数,即可实现分布式计算. 两 ...

  7. noip2015day2-运输计划

    题目描述 公元$ 2044 $年,人类进入了宇宙纪元. \(L\) 国有 \(n\) 个星球,还有 \(n-1\) 条双向航道,每条航道建立在两个星球之间,这 \(n-1\) 条航道连通了 \(L\) ...

  8. Centos下安装nc命令工具安装以及使用

    1)netcat(nc)是一个简单而有用的工具,被誉为网络安全界的“瑞士军刀”,不仅可以通过使用TCP或UDP协议的网络连接读写数据,同时还是一个功能强大的网络调试和探测工具,能够建立你需要的几乎所有 ...

  9. 使用git配置ssh的文章推荐

    https://blog.51cto.com/sgk2011/1925922 https://www.cnblogs.com/superGG1990/p/6844952.html https://bl ...

  10. vue-resource对比axios import ... from和import {} from 的区别 element-ui

    1.vue-resource对比axios 文章1 文章2 1.0 axios  params 配置参数在url 显示,form-data 用于 图片上传.文件上传 1.1 axios 全局配置 ax ...