zoukankan      html  css  js  c++  java
  • CCF 201412-4 最优灌溉

    问题描述
    试题编号: 201412-4
    试题名称: 最优灌溉
    时间限制: 1.0s
    内存限制: 256.0MB
    问题描述:
    问题描述
      雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉。   为了灌溉,雷雷需要建立一些水渠,以连接水井和麦田,雷雷也可以利用部分麦田作为“中转站”,利用水渠连接不同的麦田,这样只要一片麦田能被灌溉,则与其连接的麦田也能被灌溉。   现在雷雷知道哪些麦田之间可以建设水渠和建设每个水渠所需要的费用(注意不是所有麦田之间都可以建立水渠)。请问灌溉所有麦田最少需要多少费用来修建水渠。
    输入格式
      输入的第一行包含两个正整数n, m,分别表示麦田的片数和雷雷可以建立的水渠的数量。麦田使用1, 2, 3, ……依次标号。   接下来m行,每行包含三个整数ai, bi, ci,表示第ai片麦田与第bi片麦田之间可以建立一条水渠,所需要的费用为ci
    输出格式
      输出一行,包含一个整数,表示灌溉所有麦田所需要的最小费用。
    样例输入
    4 4 1 2 1 2 3 4 2 4 2 3 4 3
    样例输出
    6
    样例说明
      建立以下三条水渠:麦田1与麦田2、麦田2与麦田4、麦田4与麦田3。
    评测用例规模与约定
      前20%的评测用例满足:n≤5。   前40%的评测用例满足:n≤20。   前60%的评测用例满足:n≤100。   所有评测用例都满足:1≤n≤1000,1≤m≤100,000,1≤ci≤10,000。

    写法一:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <queue>
     6 using namespace std;
     7 struct Edge{
     8     int from,to,dist;
     9     bool operator < (const Edge& ed) const{//ÉýÐòÅÅÁÐ
    10      return dist>ed.dist;
    11     }
    12 };
    13 const int maxn=1005;
    14 priority_queue<Edge> que;
    15 int pre[maxn];
    16 int n,m,fee;
    17 int find(int v)
    18 {
    19     int u=pre[v];
    20     while(pre[u]!=u)
    21     u=pre[u];
    22     while(pre[v]!=u)
    23     {
    24         int temp=pre[v];
    25         pre[v]=u;
    26         v=temp;
    27     }
    28     return u;
    29 }
    30 int main()
    31 {
    32     scanf("%d%d",&n,&m);
    33     for(int i=0;i<m;i++)
    34     {
    35         Edge ed;
    36         scanf("%d%d%d",&ed.from,&ed.to,&ed.dist);
    37         que.push(ed);
    38     }
    39     for(int i=1;i<=n;i++)
    40     pre[i]=i;
    41     int pc=0;
    42     while(!que.empty())
    43     {
    44         Edge e=que.top();
    45         que.pop();
    46         int v1=find(e.from);
    47         int v2=find(e.to);
    48         if(v1!=v2)
    49         {
    50           pre[v1]=v2;
    51           fee+=e.dist;
    52           pc++;
    53         }
    54         if(pc>=n-1)
    55         break;
    56     }
    57     printf("%d
    ",fee);
    58     return 0;
    59 }

    写法二:

     1 //201412-4
     2 //最优灌溉  Kruskal
     3 #include<cstdio>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<algorithm>
     7 #include<iostream>
     8 #include<stack>
     9 using namespace std;
    10 struct line{
    11     int a,b,v;
    12 };
    13 line l[100005];
    14 int f[1005];
    15 bool cmp(line a,line b){//升序排列
    16     return a.v<b.v;
    17 }
    18 int find(int a){
    19     if(f[a]!=a){
    20         f[a]=find(f[a]);
    21     }
    22     return f[a];
    23 }
    24 int main(){
    25     //freopen("D:\INPUT.txt","r", stdin);
    26     int n,m;
    27     while(scanf("%d %d",&n,&m)!=EOF){
    28         int i=0;
    29         for(;i<m;i++){
    30             scanf("%d %d %d",&l[i].a,&l[i].b,&l[i].v);
    31         }
    32         for(i=0;i<=n;i++){
    33             f[i]=i;
    34         }
    35         sort(l,l+m,cmp);
    36         /*for(i=0;i<=m;i++){
    37             cout<<i<<" "<<l[i].v<<endl;
    38         }*/
    39         int sum=0;
    40         i=0;
    41         n--;
    42         while(n&&i<m){//点的数量可能比线段的数量更多
    43             int a=l[i].a;
    44             int b=l[i].b;
    45             a=find(a);
    46             b=find(b);
    47             if(a!=b){
    48                 //cout<<i<<endl;
    49                 f[a]=b;
    50                 sum+=l[i].v;
    51                 n--;
    52             }
    53             i++;
    54         }
    55         cout<<sum<<endl;
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    【Leetcode_easy】961. N-Repeated Element in Size 2N Array
    【Leetcode_easy】953. Verifying an Alien Dictionary
    【Leetcode_easy】949. Largest Time for Given Digits
    【Leetcode_easy】944. Delete Columns to Make Sorted
    【Leetcode_easy】942. DI String Match
    【Leetcode_easy】941. Valid Mountain Array
    【Leetcode_easy】938. Range Sum of BST
    【Leetcode_easy】937. Reorder Log Files
    【Leetcode_easy】933. Number of Recent Calls
    【Leetcode_easy】929. Unique Email Addresses
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4368390.html
Copyright © 2011-2022 走看看