最小生成树+二分【洛谷P2330】

传送门:https://www.luogu.org/problemnew/show/P2330

终于做对了一个图论+二分的题,虽然比较简单吧,是个黄题...

直接prim求一下最小生成树,然后就二分一下分值就好啦!

#include 
using namespace std;
const int maxn = 330;
const int INF = 1e9+7;
int vis[maxn];
int n,m;
int cnt = 0;
struct node
{
	int to;
	int cost;
	bool operator<(const node &a)const
	{
		return cost>a.cost;
	}
};
vector G[maxn];
int prim(int s,int x)
{
	int ans = 0;
	priority_queue q;
	vis[s] = 1;
	for(int i=0;i>n>>m;
	int l = INF,r = 0;
	for(int i=0;i>x>>y>>z;
		G[x].push_back((node){y,z});
		G[y].push_back((node){x,z});
		l = min(l,z);
		r = max(r,z);
	}
	int ans = 0;
	int res = 0;
	while(l<=r)
	{
		int mid = (l+r)/2;
		if(check(mid))
		{
			res = cnt;
			ans = mid;
			r = mid-1;
		}
		else
		{
			l = mid+1;
		}
	}
	cout<

 

你可能感兴趣的:(最小生成树+二分【洛谷P2330】)