【Dijkstra】CCF201609-4 交通规划

问题描述
  G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家也建设一个高速铁路系统。
  建设高速铁路投入非常大,为了节约建设成本,G国国王决定不新建铁路,而是将已有的铁路改造成高速铁路。现在,请你为G国国王提供一个方案,将现有的一部分铁路改造成高速铁路,使得任何两个城市间都可以通过高速铁路到达,而且从所有城市乘坐高速铁路到首都的最短路程和原来一样长。请你告诉G国国王在这些条件下最少要改造多长的铁路。
输入格式
  输入的第一行包含两个整数nm,分别表示G国城市的数量和城市间铁路的数量。所有的城市由1到n编号,首都为1号。
  接下来m行,每行三个整数abc,表示城市a和城市b之间有一条长度为c的双向铁路。这条铁路不会经过ab以外的城市。
输出格式
  输出一行,表示在满足条件的情况下最少要改造的铁路长度。
样例输入
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 ≤ ab ≤ n,1 ≤ c ≤ 1000。输入保证每个城市都可以通过铁路达到首都。
 
分析
1. 最短路径问题,但问题所求是在最短路径的前提下,连通每个点所增加的最小花费是多少。这就需要一个cost[]数组记录首都到其他城市所增加的花费,即点i到前一个点的距离,当出现好几条路径可以选择时,cost[i]=min(cost[i],i.dist).
2. priority_queue比较方式默认用operator<,也就是优先队列是大顶堆,队头元素最大。重载<使其从小到大排列
3. Dijkstra的算法是使用的紫书上的基于STL的方法,之前使用数据结构课本上直接使用数组记录的方法,提交显示运行错误,大概是复杂度的问题?
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string.h>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <list>
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>edge;
vector<ll>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<Node>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;
}

 

posted @ 2018-03-16 17:45  kikii233  阅读(157)  评论(0编辑  收藏  举报