zoukankan      html  css  js  c++  java
  • nyoj 1239——引水工程——————【最小生成树 prim】

    引水工程

    时间限制:2000 ms  |  内存限制:65535 KB
    难度:3
     
    描述

    南水北调工程是优化水资源配置、促进区域协调发展的基础性工程,是新中国成立以来投资额最大、涉及面最广的战略性工程,事关中华民族长远发展。“南水北调工程”,旨在缓解中国华北西北地区水资源短缺的国家战略性工程。就是把中国长江流域丰盈的水资源抽调一部分送到华北和西北地区。我国南涝北旱,南水北调工程通过跨流域的水资源合理配置,促进南北方经济、社会与人口、资源、环境的协调发展。

    整个工程分东线、中线、西线三条调水线。东线工程位于东部,因地势低需抽水北送至华北地区。中线工程从汉水与其最大支流丹江交汇处的丹江口水库引水,自流供水给黄淮海平原大部分地区,20多座大中城市;西线工程在青藏高原上,由长江上游向黄河上游补水。

    现在有N个区域需要建设水资源工程,它们可以自建水库解决缺水问题,也可以从已有水源的地区建立管道引水过来。当然,这些建设都需要大量投资。

    你能不能给出一个优化水资源配置方案,在保证每个区域都能用上水的前提下,使得整个引水工程费用最低。

     
    输入
    第一行: K 表示有多少组测试数据。
    接下来对每组测试数据:
    第1行: N 表示有N个区域( 1<=N<=300 )
    第2 行: W1 W2 …. WN Wi表示第i个区域自建水库需要的费用
    再有N行: Pi1 Pi2 …. Pin Pij表示建立第i个区域与第j个区域引水管道的费用
    输出
    对于每组测试数据,输出占一行,即建立整个引水工程的最小费用。
    样例输入
    1
    5
    5 4 4 3 6
    0 2 2 2 2
    2 0 3 3 3
    2 3 0 4 5
    2 3 4 0 1
    2 3 5 1 0
    
    样例输出
    10
    来源
    第八届河南省程序设计大赛
    解题思路:只需要在prim求和的时候加min(w[s], minc)即可。表示考虑到了该点是自建水库好还是引水过来好。就是转化。
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<math.h>
    #include<string>
    #include<iostream>
    #include<queue>
    #include<vector>
    #include<set>
    using namespace std;
    typedef long long LL;
    #define mid (L+R)/2
    #define lson rt*2,L,mid
    #define rson rt*2+1,mid+1,R
    const int maxn = 1e3 + 300;
    const LL INF = 0x3f3f3f3f;
    int vis[maxn], lowc[maxn], cost[maxn][maxn];
    int w[maxn];
    int n;
    int prim(int id){
        memset(vis,0,sizeof(vis));
        int retsum = 0;
        vis[id] = 1;
        for(int i = 1; i <= n; i++){
            lowc[i] = cost[id][i];
        }
        retsum = w[id];
        for(int i = 1; i < n; i++){
            int s = -1;
            int minc = INF;
            for(int j = 1; j <= n; j++){
                if(!vis[j]&&lowc[j] < minc){
                    minc = lowc[j];
                    s = j;
                }
            }
           // printf("%d+++
    ",s);
            retsum += min(minc,w[s]);
            vis[s] = 1;
            for(int j = 1; j <= n; j++){
                if(!vis[j] && lowc[j] > cost[s][j]){
                    lowc[j] = cost[s][j];
                }
            }
        }
        return retsum;
    }
    
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n);
            int m = 0, val;
            int sel = INF, id = 0;
            for(int i = 1; i <= n; i++){
                scanf("%d",&w[i]);
                if(w[i] < sel){
                    sel = w[i]; id = i;
                }
            }
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= n; j++){
                    scanf("%d",&cost[i][j]);
                    if(cost[i][j] == 0) cost[i][j] = INF;
                }
            }
            int res = prim(id);
            printf("%d
    ",res);
        }
        return 0;
    }
    

      

  • 相关阅读:
    hdu 1247 Hat’s Words (字典树)
    测试
    hdu 1285 确定比赛名次 (拓扑)
    hdu 3172 Virtual Friends (并查集)
    hdu 3635 Dragon Balls (并查集)
    [Shell学习笔记] read命令从键盘或文件中获取标准输入(转载)
    Shell脚本下条件测试(eq.ne.....)(转载)
    每天一个 linux命令(35):ln 命令(转载)
    ubuntu中 python升级 (转载)
    source命令用法(转载)
  • 原文地址:https://www.cnblogs.com/chengsheng/p/5462879.html
Copyright © 2011-2022 走看看