博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Dijkstra】CCF201609-4 交通规划
阅读量:7032 次
发布时间:2019-06-28

本文共 2602 字,大约阅读时间需要 8 分钟。

问题描述
  G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
  建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
输入格式
  输入的第一行包含两个整数
n
m,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到
n编号,首都为1号。
  接下来
m行,每行三个整数
a
b
c,表示城市
a和城市
b之间有一条长度为
c的双向铁路。这条铁路不会经过
a
b以外的城市。
输出格式
  输出一行,表示在满足条件的情况下最少要改造的铁路长度。
样例输入
4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2
样例输出
11
评测用例规模与约定
  对于20%的评测用例,1 ≤ 
n ≤ 10,1 ≤ 
m ≤ 50;
  对于50%的评测用例,1 ≤ 
n ≤ 100,1 ≤ 
m ≤ 5000;
  对于80%的评测用例,1 ≤ 
n ≤ 1000,1 ≤ 
m ≤ 50000;
  对于100%的评测用例,1 ≤ 
n ≤ 10000,1 ≤ 
m ≤ 100000,1 ≤ 
a
b ≤ n,1 ≤ 
c ≤ 1000。输入保证每个城市都可以通过铁路达到首都。
 
分析
1. 最短路径问题,但问题所求是在最短路径的前提下,连通每个点所增加的最小花费是多少。这就需要一个cost[]数组记录首都到其他城市所增加的花费,即点i到前一个点的距离,当出现好几条路径可以选择时,cost[i]=min(cost[i],i.dist).
2. priority_queue比较方式默认用operator<,也就是优先队列是大顶堆,队头元素最大。重载<使其从小到大排列
3. Dijkstra的算法是使用的紫书上的基于STL的方法,之前使用数据结构课本上直接使用数组记录的方法,提交显示运行错误,大概是复杂度的问题?
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const int NINF = 0xc0c0c0c0;const int maxn = 10001;int MonthDay[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};struct Edge{ ll from, to, dist; Edge(ll f, ll t, ll d):from(f),to(t), dist(d){}};vector
edge;vector
G[maxn];bool done[maxn];ll d[maxn];ll p[maxn];ll m, n;ll cost[maxn];struct Node{ ll d, u; Node(ll dd, ll uu):d(dd), u(uu){} bool operator <(const Node& a)const { return d>a.d; }};void Dijkstra(ll s){ priority_queue
q; for(ll i = 0; i < n; i++) { cost[i] = INF; d[i] = INF; } d[s] = 0; cost[s] = 0; memset(done, 0, sizeof(done)); q.push(Node(0,s)); while(!q.empty()) { Node x = q.top(); q.pop(); ll u = x.u; if(done[u]) continue; done[u] = true; for(ll i = 0; i < G[u].size(); i++) { Edge& e = edge[G[u][i]]; if(d[e.to] > d[u]+e.dist) { d[e.to] = d[u]+e.dist; cost[e.to] = e.dist; p[e.to] = G[u][i]; q.push(Node(d[e.to], e.to)); } if(d[e.to] == d[u]+e.dist) { cost[e.to] = min(cost[e.to], e.dist); } } }}int main() { cin >> n >> m; ll a, b, c; for(ll i = 0; i < n; i++){ G[i].clear(); } edge.clear(); for(ll i = 0; i < m; i++){ cin >> a >> b >> c; a--; b--; edge.push_back(Edge(a,b,c)); ll t = edge.size()-1; G[a].push_back(t); edge.push_back(Edge(b,a,c)); G[b].push_back(t+1); } Dijkstra(0); ll ans = 0; for(ll i = 0; i < n; i++) { ans += cost[i]; } cout << ans << endl; return 0;}

 

转载于:https://www.cnblogs.com/kikii233/p/8583777.html

你可能感兴趣的文章
MongoDB 4.0 RC 版本强势登陆
查看>>
AliOS Things网络适配框架 - SAL
查看>>
iOS 客户端与服务端做时间同步
查看>>
多个请求统一更新界面
查看>>
illuminate/routing 源码分析之注册路由
查看>>
网易公共技术Java研发工程师面经(offer)
查看>>
说说如何在登录页实现生成验证码功能
查看>>
笔记-softmax、softmax loss
查看>>
FastDFS蛋疼的集群和负载均衡(六)之Nginx高可用集群
查看>>
C语言入门经典读书笔记----第十一章 结构化数据
查看>>
Apache Thrift系列详解(二) - 网络服务模型
查看>>
chrome devtools使用详解——Performance
查看>>
了解一下ES6: 解构赋值&字符串
查看>>
7 - 在 Django Admin 后台发布文章
查看>>
SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页
查看>>
Umeng第三方登录
查看>>
EggBorn.js:一款顶级Javascript全栈开发框架
查看>>
前端开始的那件事——表单
查看>>
【前端】HTML属性
查看>>
js 算法3
查看>>