BZOJ1083 SCOI2005 繁忙的都市 最小生成树

题意:求最小生成树

#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <functional>
using namespace std;

const int MAXN=1000+2;
struct NODE{
    int u,w;
    NODE(){}
    NODE(int _u,int _w):u(_u),w(_w){}
    friend bool operator <(NODE a,NODE b){ return a.w>b.w;}
};
int N,graph[MAXN][MAXN],b,dist[MAXN],e,M,ans=-1;
bool visited[MAXN];
priority_queue<NODE> s;

int Prim(int b){
    memset(dist,0X7F,sizeof(dist));
    memset(visited,0,sizeof(visited));
    dist[b]=0;

    NODE tmp=NODE(b,0),t;
    s.push(tmp);

    while(!s.empty()){
        tmp=s.top();
        s.pop();

        if(visited[tmp.u]) continue;
        visited[tmp.u]=1,ans=max(tmp.w,ans);

        for(int i=1;i<=N;i++)
            if(dist[i]>graph[tmp.u][i]){
                dist[i]=graph[tmp.u][i];
                t.w=dist[i],t.u=i;
                s.push(t);
            }
    }
}

int main(){
    cin >> N >> M;
    for(int i=1,u,v,c;i<=M;i++){
        cin >> u >> v >> c;
        if(graph[u][v]) graph[u][v]=graph[v][u]=min(graph[v][u],c);
        else graph[u][v]=graph[v][u]=c;
    }

    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)
            if(!graph[i][j]) graph[i][j]=graph[j][i]=INT_MAX;

    Prim(1);

    cout << N-1 << " " << ans << endl;

    return 0;
}
View Code

 

posted @ 2017-02-28 01:14  WDZRMPCBIT  阅读(122)  评论(0编辑  收藏  举报