poj 2395 Out of Hay

简介: 点击打开链接poj 2395 思路:求解最小生成树的最大边 注意:由于这里的长度最大到10^9,所以INF不能为0xFFFFFFF应该为0x3F3F3F3F。

点击打开链接poj 2395


思路:求解最小生成树的最大边
注意:由于这里的长度最大到10^9,所以INF不能为0xFFFFFFF应该为0x3F3F3F3F。

代码:


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 2010
#define INF 0x3F3F3F3F

int n , m;
int vis[MAXN];
int lowcost[MAXN];
int G[MAXN][MAXN];

void init(){
    for(int i = 1 ; i <= n ; i++){
       for(int j = 1 ; j <= n ; j++)
           G[i][j] = INF;
    }
}

void prime(){
     memset(vis , 0 , sizeof(vis));
     int pos , ans;
     ans = 0;
     vis[1] = 1;
     for(int i = 1 ; i <= n ; i++)
        lowcost[i] = G[1][i];
     for(int i = 1 ; i <= n ; i++){
        pos = -1;
        for(int j = 1 ; j <= n ; j++){
           if(!vis[j] && (pos == -1 || lowcost[j] < lowcost[pos]))
              pos = j;
        }
        if(pos == -1)
            break;
        vis[pos] = 1;
        if(ans < lowcost[pos])
          ans = lowcost[pos];
        for(int j = 1 ; j <= n ; j++){
           if(!vis[j] && lowcost[j] > G[j][pos])
             lowcost[j] = G[j][pos];
        }
     }
     printf("%d\n" , ans);
}

int main(){
    int a , b , v;
    scanf("%d%d" , &n , &m);
    init();
    for(int i = 0 ; i < m ; i++){
        scanf("%d%d%d" , &a , &b , &v);
        if(G[a][b] > v)
          G[a][b] = G[b][a] = v;
    }
    printf("%d\n" , INF);
    prime();
    return 0;
}



目录
相关文章
|
19天前
|
算法 数据建模
Poj 3169(差分约束系统)
Poj 3169(差分约束系统)
21 0
poj 1455
Description n participants of > sit around the table. Each minute one pair of neighbors can change their places.
604 0
poj 2299 求逆序数
http://poj.org/problem?id=2299 #include using namespace std; int aa[500010],bb[500010]; long long s=0; void merge(int l,int m,int r) { ...
771 0
|
JavaScript
poj-1006-Biorhythms
Description 人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。
589 0
POJ 2487 Stamps
Description Background Everybody hates Raymond. He’s the largest stamp collector on planet earth and because of that he always makes fun of all the others at the stamp collector parties.
1037 0
POJ 2027 No Brainer
Problem Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indicating the number of data sets.
845 0
|
人工智能
POJ 1936 All in All
Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way.
763 0