zoukankan      html  css  js  c++  java
  • CCF201609-4 交通规划

    问题描述
      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。输入保证每个城市都可以通过铁路达到首都。
     
    #include<iostream>
    #include<vector>
    #include<string>
    #include<cstdio>
    #include<algorithm>
    #include<set>
    #include <queue>
    #include<cstring>
    using namespace std;
    
    #define maxn 10007
    //使用了prim算法
    struct Node{
    Node(){}
    int v; //节点
    int c; //花费
    Node(int a, int b){
        v = a;
        c = b;
    }
    };
    
    vector <Node> g[maxn];
    int dist[maxn];     //每个节点最合适的花费
    int cost[maxn];     //每个花费
    int flag[maxn];     //是否已经连接
    
    bool operator <(Node a,Node b){
        if(a.c==b.c) return a.v<b.v;
        return a.c<b.c;
    }
    
    int prime(int s, int n){
        memset(flag,0,sizeof(flag));
        dist[s] = 0;
        cost[s] = 0;
        Node now,add;
        now.v = s;
        now.c = 0;
        queue <Node> Q;
        Q.push(now);
        while(Q.size()>0){
            now = Q.front();
            Q.pop();
            if(flag[now.v])continue;
            flag[now.v] = 1;
            for(int i=0; i<g[now.v].size(); i++){//对连接的点进行遍历,找花费最小的情况
    
                add.v = g[now.v][i].v;  //选中点所连接的点
                add.c = now.c + g[now.v][i].c; //所需要的花费
                if(flag[add.v]==0&&dist[add.v]>=add.c){ //若该点还未连接且花费小于之前插入的点
                    dist[add.v] = add.c;
                Q.push(add);
                cost[add.v] = min(cost[add.v],g[now.v][i].c);
                }
            }
        }
        int ans = 0;
        for(int i=1; i<=n ;i++)
            ans += cost[i];
        return ans;
    
    }
    int main(){
    
    int n,m;
    scanf("%d%d",&n,&m);
    memset(cost,0x3f3f3f,sizeof(cost));
    memset(dist,0x3f3f3f,sizeof(dist));
    
    int u,v,c;
    
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d",&u,&v,&c);
        g[u].push_back(Node(v,c)); //插入相应的点,并且数组存入连接的点
        g[v].push_back(Node(u,c));
    }
    cout<<prime(1,n)<<endl;
    return 0;
    
    }
  • 相关阅读:
    【Codeforces 349B】Color the Fence
    【Codeforces 459D】Pashmak and Parmida's problem
    【Codeforces 467C】George and Job
    【Codeforces 161D】Distance in Tree
    【Codeforces 522A】Reposts
    【Codeforces 225C】Barcode
    【Codeforces 446A】DZY Loves Sequences
    【Codeforces 429B】Working out
    【Codeforces 478C】Table Decorations
    【Codeforces 478C】Table Decorations
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7260468.html
Copyright © 2011-2022 走看看