瞌睡小源


私信TA

用户名:H2130823055

访问量:4658

签 名:

我が名はめぐみん、爆裂魔法を操りし者

等  级
排  名 45
经  验 11363
参赛次数 5
文章发表 73
年  龄 0
在职情况 学生
学  校 贺州学院
专  业

  自我简介:

解题思路:最小生成树,如果道路是好的就提前连上且不用成本

注意事项:

参考代码:

#include<bits/stdc++.h>
using namespace std;
int f[1000];
struct node
{
	int from;
	int to;
	int w;
};
int find(int x)
{
	if(x!=f[x])
	{
		return f[x]=find(f[x]);
	}
	return x;
}
long long ans=0;
void meare(node a,bool t)
{
	int x=find(a.from);
	int y=find(a.to);
	if(x!=y)
	{
		f[y]=x;
		if(t)ans+=a.w;
		
	}
}
vector<node>v;
bool cmp(node a,node b)
{
	return a.w<b.w;
}
int main()
{
	int n;
	while(cin>>n&&n)
	{
		ans=0;
		for(int i=0;i<=n;i++)
		{
			f[i]=i;
		}
		for(int i=1;i<=(n*(n-1)/2);i++)
		{
			int d;
			node a;
			cin>>a.from>>a.to>>a.w>>d;
			if(d==1)
			{
				meare(a,0);
			}
			else
			{
				v.push_back(a);
			}
		}
		sort(v.begin(),v.end(),cmp);
		for(int i=0;i<v.size();i++)
		{
			meare(v[i],1);
		}
		v.clear();
		cout<<ans<<endl;	
	}
	return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区