zoukankan      html  css  js  c++  java
  • poj 2377 Bad Cowtractors (最大生成树prim)

    Bad Cowtractors

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 1   Accepted Submission(s) : 1
    Problem Description
    Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn't even want to pay Bessie.

    Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
     
    Input
    * Line 1: Two space-separated integers: N and M <br> <br>* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
     
    Output
    * Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.
     
    Sample Input
    5 8 1 2 3 1 3 7 2 3 10 2 4 4 2 5 8 3 4 6 3 5 2 4 5 17
     
    Sample Output
    42
     1 #include <iostream>  
     2 #include <cstdio>  
     3 using namespace std;
     4 const int INF = 0x3f3f3f3f;
     5 int a[1005][1005];
     6 int dis[1005];
     7 bool vis[1005];
     8 int n, m;
     9 void Prime()
    10 {
    11     for (int i = 1; i <= n; i++)
    12     {
    13         vis[i] = false;
    14         dis[i] = a[1][i];
    15     }
    16     dis[1] = 0;
    17     vis[1] = true;
    18     int ans = 0;
    19     for (int i = 2; i <= n; i++)
    20     {
    21         int minn = 0;
    22         int p = -1;
    23         for (int j = 1; j <= n; j++)
    24         {
    25             if (!vis[j] && dis[j]>minn)// 是大于,找出最大的边 
    26                 minn = dis[p = j];
    27         }
    28         if (p == -1)
    29         {
    30             cout << "-1" << endl;
    31             return;
    32         }
    33         vis[p] = true;
    34         ans += minn;
    35         for (int j = 1; j <= n; j++)
    36         {
    37             if (!vis[j] && dis[j]<a[p][j])//尽可能让边变大
    38                 dis[j] = a[p][j];
    39         }
    40     }
    41     cout << ans << endl;
    42 }
    43 int main()
    44 {
    45     while (cin >> n >> m)
    46     {
    47         //初始化为0
    48         for (int i = 1; i <= n; i++)
    49         {
    50             for (int j = 1; j <= n; j++)
    51                 a[i][j] = 0;
    52         }
    53         int x, y, z;
    54         while (m--)
    55         {
    56             scanf("%d%d%d", &x, &y, &z);
    57             if (z>a[x][y])
    58                 a[x][y] = a[y][x] = z;
    59         }
    60         Prime();
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    蓝桥杯-带分数
    蓝桥杯-分红酒
    蓝桥杯-猜算式
    hdu2045不容易系列之(3)—— LELE的RPG难题
    蓝桥杯-奇怪的比赛
    linux应用之perl环境的安装(centos)
    linux应用之php开发环境lamp搭建(centos)
    linux应用之yum命令详解
    linux应用之用户管理相关命令
    linux应用之mysql数据库指定版本的yum安装(centos)
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8444508.html
Copyright © 2011-2022 走看看