交通规划

6 篇文章 0 订阅
2 篇文章 0 订阅

[CCF]交通规划

Question

  G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
  建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。

  输入的第一行包含两个整数nm,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到n编号,首都为1号。   接下来m行,每行三个整数abc,表示城市a和城市b之间有一条长度为c的双向铁路。这条铁路不会经过ab以外的城市。

  输出一行,表示在满足条件的情况下最少要改造的铁路长度。

Input

4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2

Output

11

Solution

// http://118.190.20.162/view.page?gpid=T44
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<cctype>
#include<map>
#include<utility>
using namespace std;
#define print(A) cout << #A << ": "<< A << endl;
#define inf 0x3fffffff
typedef long long int ll;

struct edge {
  int head, tail, dist;
  edge(int h = -1, int t = -1, int d = -1): head(h), tail(t), dist(d) {}
};

struct headnode {
  int node, dist;
  headnode(int n = -1, int d = -1): node(n), dist(d) {}
  bool operator<(const headnode& another) const { return dist > another.dist; }
};

int main(int argc, char const *argv[]) {
  int node_num, edge_num; cin >> node_num >> edge_num;
  vector<int> d;
  d.resize(node_num+1, inf);
  vector<bool> done;
  done.resize(node_num+1, false);
  vector<vector<int> > graph;
  graph.resize(node_num+1, vector<int>());
  vector<edge> edges;
  edges.resize(edge_num+1);
  // 记录本节点离上一结点的距离
  vector<int> last_add;
  last_add.resize(node_num+1, inf);

  for (int i = 1; i <= edge_num; i++) {
    int node1, node2, dist;
    cin >> node1 >> node2 >> dist;
    edges[i] = edge(node1, node2, dist);
    graph[node1].push_back(i);
    graph[node2].push_back(i);
  }

  priority_queue<headnode> pq;
  pq.push(headnode(1, 0));
  last_add[1] = 0;
  d[1] = 0;
  while (!pq.empty()) {
    headnode top = pq.top(); pq.pop();
    if (done[top.node]) continue;
    done[top.node] = true;
    for (int i = 0; i < graph[top.node].size(); i++) {
      int index = graph[top.node][i];
      edge temp = edges[index];
      int another_node = (top.node == temp.head)? temp.tail: temp.head;
      if (d[another_node] > d[top.node]+temp.dist) {
        last_add[another_node] = temp.dist;
        d[another_node] = d[top.node]+temp.dist;
        pq.push(headnode(another_node, d[another_node]));
      } else if (d[another_node] == d[top.node]+temp.dist) {
        last_add[another_node] = min(temp.dist, last_add[another_node]);
      }
    }

  }
  int res = 0;
  for (int i = 1; i <= node_num; i++) res += last_add[i];
  cout << res << endl;
  return 0;
}

思路:使用Dijkstra的板子,稍有改动就是在于需要添加一个数组last_add来标记最短路径的时候离上一个结点的距离。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值