zoukankan      html  css  js  c++  java
  • [CCF CSP]201609-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号点的路径长度和原图中到1号点的最短路相等

    思路:改进dijstra算法,松弛的时候,到源点距离相等时,节点也可以加入队列,节点增添一个属性preW,表示该最短路路径上,前驱边的权值。优先把短的边加入到生成树,加入时答案加上该条边的贡献。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 10005;
    const int INF=0x3f3f3f3f;
    int d[N],vis[N],ans;
    
    struct node{
        int num;
        int dis;
        int preW;
        node(){}
        node(int a,int b,int c){num=a;dis=b;preW=c;}
    };
    struct Edge{
        int v;
        int w;
        Edge(){}
        Edge(int a,int b) {v=a;w=b;}
    };
    vector<Edge> g[N];da
    priority_queue<node> que;
    bool operator<(const node&a,const node& b){
        if(a.dis==b.dis) return a.preW>b.preW;
        return a.dis>b.dis;
    }
    
    void dijkstra(int u)
    {
        memset(d,INF,sizeof(d));
        memset(vis,0,sizeof(vis));
        while(!que.empty()) que.pop();
        que.push(node(1,0,0));
        while(!que.empty())
        {
            node now=que.top();
            que.pop();
            int u=now.num,dis=now.dis;
            if(vis[u]) continue;
            vis[u]=1;
            ans+=now.preW;
            for(int i=0;i<g[u].size();i++)
            {
                int v=g[u][i].v,w=g[u][i].w;
                if(vis[v]) continue;
                if(dis+w<=d[v])
                {
                    d[v]=dis+w;
                    que.push(node(v,d[v],w));
                }
            }
        }
    }
    int main()
    {
        int n,m,u,v,w;
        ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
        cin>>n>>m;
        for(int i=0;i<m;i++)
        {
            cin>>u>>v>>w;
            g[u].push_back(Edge(v,w));
            g[v].push_back(Edge(u,w));
        }
        ans=0;
        dijkstra(1);
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    JVM 综述
    看 Netty 在 Dubbo 中如何应用
    Netty 心跳服务之 IdleStateHandler 源码分析
    Netty 高性能之道
    Netty 解码器抽象父类 ByteToMessageDecoder 源码解析
    Netty 源码剖析之 unSafe.write 方法
    Netty 出站缓冲区 ChannelOutboundBuffer 源码解析(isWritable 属性的重要性)
    Netty 源码剖析之 unSafe.read 方法
    Netty 内存回收之 noCleaner 策略
    Netty 源码阅读的思考------耗时业务到底该如何处理
  • 原文地址:https://www.cnblogs.com/Andrew-aq/p/12518808.html
Copyright © 2011-2022 走看看