zoukankan      html  css  js  c++  java
  • POJ1258 Agri-Net

    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 81076   Accepted: 33464

    Description

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
    The distance between any two farms will not exceed 100,000.

    Input

    The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    Output

    For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    Sample Input

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    Sample Output

    28

    Source

    模板题

    这题TLE了一天,脑溢血了。 用scanf + printf会TLE,换成cin取消同步以后就AC了。

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <vector>
     5 #include <iomanip>
     6 #include <cmath>
     7 #include <cstdio>
     8 using namespace std;
     9 const int MAXN = 103;
    10 const int INF = 0x3f3f3f3f;
    11 typedef long long ll;
    12 
    13 
    14 int n, num;
    15 bool vis[MAXN];
    16 int dist[MAXN], g[MAXN][MAXN];
    17 
    18 int prim(){
    19     int ans = 0;
    20     memset(vis, 0, sizeof vis);
    21     for(int i = 1; i <= n; ++ i){
    22         dist[i] = INF;
    23     }
    24     
    25     for(int i = 0; i < n; ++ i){
    26         int t = -1;
    27         for(int j = 1; j <= n; ++ j)
    28             if(!vis[j] and (t == -1 or dist[t] > dist[j]))
    29                 t = j;
    30         if(i)
    31             ans += dist[t];
    32         for(int j = 1; j <= n; ++ j)
    33             if(j != t)
    34                 dist[j] = min(dist[j], g[t][j]);
    35         
    36         vis[t] = true;
    37         
    38     }
    39     return ans;
    40 }
    41 int main(){
    42     int x, y, w;
    43     ios::sync_with_stdio(false);
    44     cin.tie(0);
    45     cout.tie(0);
    46     while(cin >> n and n){
    47         num = 0;
    48         
    49         for(int i = 1; i <= n; ++ i){
    50             for(int j = 1; j <= n; ++ j){
    51                 cin >> w;
    52                 g[i][j] = w;
    53             }
    54         }
    55         
    56         cout << prim() << endl;
    57     }
    58     return 0;
    59 }
    Prim写法
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <vector>
     6 #include <iomanip>
     7 #include <cmath>
     8 using namespace std;
     9 const int MAXN = 113;
    10 const int INF = 0x3f3f3f3f;
    11 typedef long long ll;
    12 typedef struct _edge{
    13     int u;
    14     int v;
    15     int w;
    16     bool operator< (const _edge& e) const {
    17         return w < e.w;
    18     }
    19 }edge;
    20 
    21 edge edges[10003];
    22 int n, num;
    23 int pa[MAXN];
    24 
    25 int find(int x){
    26     return x == pa[x]? x : (pa[x] = find(pa[x]));
    27 }
    28 bool Union(int x, int y){
    29     x = find(x);
    30     y = find(y);
    31     if(x == y)
    32         return false;
    33     pa[y] = x;
    34     return true;
    35 }
    36 int kruskal(){
    37     int ans = 0;
    38         int cnt = 1;
    39         for(int i = 1; i <= n; ++ i)
    40             pa[i] = i;
    41         sort(edges, edges + num);
    42         
    43         for(int i = 0; i < num; ++ i){
    44             if(Union(edges[i].u, edges[i].v)){
    45                 ans += edges[i].w;
    46                 ++ cnt;
    47                 if(cnt == n)
    48                     break;
    49             }
    50         }
    51     return ans;
    52 }
    53 int main(){
    54     int x, y, w;
    55     ios::sync_with_stdio(false);
    56     cin.tie(0);
    57     cout.tie(0);
    58     while(cin >> n and n){
    59         num = 0;
    60         for(int i = 1; i <= n; ++ i){
    61             for(int j = 1; j <= n; ++ j){
    62                 cin >> w;
    63                 if(j > i){
    64                     edges[num].u = i;
    65                     edges[num].v = j;
    66                     edges[num ++].w = w;
    67                 }
    68             }
    69         }
    70         
    71         cout << kruskal() << endl;
    72     }
    73     return 0;
    74 }
    kruskal写法
  • 相关阅读:
    来了!GitHub for mobile 发布!iOS beta 版已来,Android 版即将发布
    五角场之殇。曾与张江、漕河泾、紫竹齐名。如今,上海四大IT科技园是否还在?
    Visual Studio Online 的 FAQ:iPad 支持、自托管环境、Web 版 VS Code、Azure 账号等
    VS Code 1.40 发布!可自行搭建 Web 版 VS Code!
    Visual Studio Online,带来四种开发模式,未来已来。
    基于七牛云对象存储,搭建一个自己专属的极简Web图床应用(手摸手的注释讲解核心部分的实现原理)
    css伪元素::before与::after使用基础示例
    gcc生成静态链接库与动态链接库步骤,并链接生成可执行文件的简单示例
    shell求水仙花数
    shell实现简单的数组排序
  • 原文地址:https://www.cnblogs.com/daremo/p/14432388.html
Copyright © 2011-2022 走看看