CCF CSP 2018 - 12 - 4 데이터 센터

1560 단어 CSP
code:
누 드 의 최소 생 성 나무 입 니 다.
 
#include
#include
#include
#include
#include
#include
#include
#define rep(i,j,k) for(int i = j; i < k; ++i)
#define mst(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define se second
#define fi first
using namespace std;
typedef long long LL;
const int MAXN = 500000+5;
bool mark[MAXN];
int cnt;
struct Edge{
	int f,to,w;
	Edge(){}
	Edge(int ff,int tt,int ww):f(ff),to(tt),w(ww){}
	bool operator  obj.w;
	}
};

vector G[MAXN];
priority_queue pq;
int n,m,a,b,c;
int tol; //     
 
void visit(int cur){
	mark[cur] = true;
	++cnt;
	for(int i = 0; i < G[cur].size(); ++i){
		Edge &e = G[cur][i];
		if(!mark[e.to]){
			pq.push(e);
		}
	}
}

void prim(int s){
	int ans = -1;
	cnt = 1;
	mst(mark,false);
	mark[s] = true;
	for(int i = 0; i < G[s].size(); ++i){
		pq.push((Edge)G[s][i]);
	}
	int to;
	while(cnt < n && !pq.empty()){
		Edge e = pq.top();
		pq.pop();
		to = e.to;
		if(mark[to]) continue;
		ans = max(ans,e.w);
		visit(to);
	}
	cout << ans << endl;
}

int main()
	{
		
		//freopen("input.txt","r",stdin);
		ios::sync_with_stdio(false);
		cin.tie(NULL);
		cout.tie(NULL);
		int root;
		cin >> n >> m >> root;
		rep(i,0,m){
			cin >> a >> b >> c;
			G[a].push_back(Edge(a,b,c));
			G[b].push_back(Edge(b,a,c));
		} 
		prim(root);

		return 0;
	}

 

좋은 웹페이지 즐겨찾기