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 ...
随机推荐
- Flutter路由(一)
第一点:push使用 1.pushNamed——Navigator.of(context).pushNamed('routeName') Navigator.of(context).pushNamed ...
- CSS元素隐藏
{ display: none; /* 不占据空间,无法点击 */ } /*************************************************************** ...
- docker 安装 gitlab 中文社区版
docker pull twang2218/gitlab-ce-zh 创建一个docker 目录 /usr/local/docker/gitlab 创建一个 docker-compose.yml ve ...
- 实验仓 #779.【CSP2019模拟 Day 1】A题
题目传送门 考场上面做了一个暴力的做法,然后,然后他$WA$了. emmm...($T$就算了吧,$WA$了算什么事啊) 好吧,这道题,其实好像...是一道思维题来着. 如果出现了这样两个打X的格子上 ...
- 【机器学习】HK算法(LMSE算法) LMS算法改进保证线性可分时均方误差标准能够找到线性可分的超平面
1.其实HK算法思想很朴实,就是在最小均方误差准则下求得权矢量. 他相对于感知器算法的优点在于,他适用于线性可分和非线性可分得情况,对于线性可分的情况,给出最优权矢量,对于非线性可分得情况,能够判别出 ...
- xampp:windows找不到文件“-n”
转自:http://blog.csdn.net/soar92/article/details/72897789 安装xampp是总是出总是提示以下错误: ①安装xampp时提示windows找不到文件 ...
- 索引之----mysql单列索引失效的情况
使用的索引名称: 1.隐式转换导致索引失效. 由于表字段定义为vachar类型,但在查询时把该字段作为number类型 以及where条件传给mysql. 2.对索引列进行任何操作(计算(+.-.*. ...
- 更新到.netcore3.0后找不到dotnet-ef的解决办法
在项目根目录下建立global.json文件 { "sdk": { "version": "2.2.402" } } 或使用命令 dotne ...
- 【6.28校内test】T1 Jelly的难题1
Jelly的难题[题目链接] 废话一句:今天中考出成绩,感觉大家考的都超级棒,不管怎样,愿大家成为最好的自己. 好了废话完了,下面是题解部分: SOLUTION: 首先你可能发生的,是看不懂题: 定睛 ...
- linux 内核数据结构之红黑树.
转载: http://www.cnblogs.com/haippy/archive/2012/09/02/2668099.html https://zh.wikipedia.org/zh/%E7%BA ...