zoukankan      html  css  js  c++  java
  • zzuoj10409--引水工程 (***最小生成树***)

    10409: D.引水工程

    Time Limit: 2 Sec  Memory Limit: 128 MB
    Submit: 18  Solved: 10
    [Submit][Status][Web Board]

    Description

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

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

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

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

    Input

    第一行:K表示有多少组测试数据。

    接下来对每组测试数据:

    第1行:N表示有N个区域( 1<=N<=300 )

    第2行:W1W2…. WNWi表示第i个区域自建水库需要的费用

    再有N行:Pi1Pi2….PinPij表示建立第i个区域与第j个区域引水管道的费用

    1≤k≤101≤N≤2001≤WiPij≤100000Pij= PjiPii=0 (i=1,…, N)

    所有数据都是整数。数据之间有一个空格。

    Output

    对于每组测试数据,输出占一行,即建立整个引水工程的最小费用。

    Sample Input

    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

    Sample Output

    10

    最小生成树稍微有点小变形。  注意Prime 算法的小变形。  克鲁斯卡尔算法是建立超级源点。

    Prim:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define N 301
    using namespace std;
    const int INF = 0x3f3f3f3f;
    int n, dis[N], vis[N], map[N][N]; 
    void Prime()
    {
        int sum = 0;
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i < n; i++)
        {
            int temp = 1, min = INF;
            for(int j = 1; j <= n; j++)
            {
                if(!vis[j] && dis[j] < min)
                {
                    temp = j;
                    min = dis[j];
                }
            }
            vis[temp] = 1;
            for(int j = 1; j <= n; j++)
                if(!vis[j] && dis[j] > map[temp][j])
                    dis[j] = map[temp][j];
        }
        for(int i = 1; i <= n; i++)
            sum += dis[i]; 
        printf("%d
    ", sum);
    }
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d", &n);
            for(int i = 1; i <= n; i++)
                scanf("%d", &dis[i]);
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                    scanf("%d", &map[i][j]);
            Prime();
        }
        return 0;
    } 

    Kl:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int M = 90400;
    struct Rode
    {
        int from, to, val;
    } edge[M];
    bool cMp(Rode from, Rode to)
    {
        return from.val < to.val;
    }
    int k;
    void Add(int a, int b, int c)
    {
        edge[k].from = a; 
        edge[k].to = b; 
        edge[k++].val = c;
    }
    int n, father[301];
    void init()
    {
        for(int i = 0; i <= n; i++)  //0为虚点建边。 
            father[i] = i;    
    } 
    int Find(int a)
    {
        if(a == father[a])
            return a;
        else
            return father[a] = Find(father[a]);
    }
    bool mercy(int a, int b)
    {
        int Q = Find(a);
        int P = Find(b);
        if(Q != P)
        {
            father[Q] = P;
            return true;
        }
        return false;
    }
    int main()
    {
        int d, T;
        scanf("%d", &T);
        while(T--)
        {
            k = 0;
            scanf("%d", &n); init();
            for(int i = 1; i <= n; i++)
            { 
                scanf("%d", &d);
                Add(0, i, d);
            }
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++)
                {
                    scanf("%d", &d);
                    Add(i, j, d);        
                }
            sort(edge, edge+k, cMp);
            int sum = 0;
            for(int i = 0; i <= k; i++)
                if(mercy(edge[i].from, edge[i].to))
                    sum += edge[i].val;
            printf("%d
    ", sum);
        }
        return 0;
    }
  • 相关阅读:
    MySql基础教程(三)——查询训练
    MySql基础教程(二)
    MySql基础教程(一)
    解决Eclipse闪退问题的方法总结
    MySQL图形工具 MySQL GUI Tools的安装使用方法
    MySql5.6版修改用户登录密码
    Windows下MySQL解压版的配置
    js 数组容易弄混的那些方法
    如何使CSS--better(系列二)
    如何使CSS--better(系列一)
  • 原文地址:https://www.cnblogs.com/soTired/p/4830607.html
Copyright © 2011-2022 走看看