欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Networking 最小生成树

程序员文章站 2022-06-16 08:58:30
...

Networking
原题链接https://vjudge.net/contest/352170#problem/B
Networking 最小生成树
Networking 最小生成树
Networking 最小生成树
题目中道路都已经给出,很明显的数据,使用Kr要简单一些,使用prim时要注意读取数据时更新map的条件,两个城市之间可能不止一条路,我们只需要最小的那一条。
Prim:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
long long n, m;
long long map[305][305];
long long ans;
long long dis[300005];
bool vis[30005];
void prim()
{
    ans = 0;
    long long i, j;
    for (i = 1; i <= n; i++)
    {
        dis[i] = map[1][i];
        vis[i] = false;
    }
 //   for (i = 1; i <= n;i++)
 //   {
 //       cout<<"**"<< dis[i] << "  ";
//    }
  //  cout << endl;
    vis[1] = true;
    for (i = 1; i < n; i++)
    {
        long long minn = INF;
        long long p = 1;
        for (j = 1; j <= n; j++)
        {
            if (!vis[j] && dis[j] < minn)
            {
                minn = dis[j];
                p = j;
            }
        }
        if (minn == 100)
        {
            ans = -1;
            return;
        }
      //  cout << "minn=" << minn << endl;
        ans += minn;
        vis[p] = true;
        for (j = 1; j <= n; j++)
        {
            if (!vis[j] && dis[j] > map[p][j])
            {
                dis[j] = map[p][j];
            }
        }
    }
    return;
}
int main()
{
    while (~scanf("%lld", &n))
    {
        if (n == 0)
        {
            break;
        }
        long long m;
        long long i, j;
        for (i = 0; i <= n; i++)
        {
            for (j = 0; j <= n; j++)
            {
                map[i][j] = INF;
            }
        }
        scanf("%lld", &m);
        while (m--)
        {
            long long a, b, w;
            scanf("%lld %lld %lld", &a, &b, &w);
            if (w < map[a][b])//特判一下,我们只要最小的
            {
                map[a][b] = w;
            }
            if (w < map[b][a])
            {
                map[b][a] = w;
            }
        }
  //      for (i = 1; i <= n;i++)
  //      {
  //          for (j = 1; j <= n;j++)
  //          {
   ///             printf("%4lld ", map[i][j]);
   //         }
     //       cout << endl;
     //   }
            prim();
        cout << ans << endl;
    }
    return 0;
}

Kruskal:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <queue>
using namespace std;
long long pre[300005];
struct node
{
	long long u;
	long long v;
	long long w;
} stu[300005];
long long ans;
long long cnt;
bool cmp(node x, node y)
{
	return x.w < y.w;
}
long long find(long long x)
{
	if (x == pre[x])
	{
		return x;
	}
	return pre[x] = find(pre[x]);
}
void join(long long x, long long y, long long w)
{
	long long ss1 = find(x);
	long long ss2 = find(y);
	if (ss1 != ss2)
	{
		pre[ss2] = ss1;
		ans += w;
		cnt--;
	}
}
int main()
{
	long long n, i;
	while (~scanf("%lld", &n))
	{
		if (n == 0)
		{
			break;
		}
		ans = 0;
		cnt = n;
		long long m;
		scanf("%lld", &m);
		for (i = 1; i <= m;i++)
		{
			scanf("%lld %lld %lld", &stu[i].u, &stu[i].v, &stu[i].w);
		}
		if(n==1)
		{
			cout << "0" << endl;
			continue;
		}
		sort(stu + 1, stu + 1 + m, cmp);
		//for (i = 1; i < z;i++)
		//{
	//		printf("%lld %lld %lld\n", stu[i].u, stu[i].v, stu[i].w);
	//	}
		for (i = 1; i <= n; i++)
		{
			pre[i] = i;
		}
		for (i = 1; i <= m ; i++)
		{
			join(stu[i].u, stu[i].v, stu[i].w);
			if (cnt == 1)
			{
				break;
			}
		}
		if(cnt>1)
		{
			ans = -1;
		}
		cout << ans << endl;
	}
	return 0;
}

相关标签: 最小生成树