文章

99

粉丝

120

获赞

8

访问

96.9k

头像
Prim
综合
发布于2024年3月17日 10:44
阅读数 1.3k

#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 105;
int mpt[maxn][maxn];
int dist[maxn];
int main() {
	int N, M;
	while (scanf("%d%d", &M, &N) != EOF)
	{
		if (M == 0)break;
		for (int i = 1;i <= N;i++)
		{
			dist[i] = INF;
			for (int j = 1;j <= N;j++)
			{
				if (i == j)mpt[i][j] = 0;
				else mpt[i][j] = INF;
			}
		}
		for (int i = 0;i < M;i++)
		{
			int u, v, w;
			scanf("%d%d%d", &u, &v, &w);
			mpt[u][v] = min(mpt[u][v], w);
			mpt[v][u] = min(mpt[v][u], w);
		}
		int sum = 0;
		int flag = 0;
		for (int i = 1;i <= N;i++) dist[i] = mpt[1][i];
		for (int i = 1;i < N;i++)
		{
			int min_len = INF;
			int min_p = -1;
			for (int j = 1;j <= N;j++)
			{
				if (min_len > dist[j] && dist[j] != 0)
				{
					min_len = dist[j];
					min_p = j;
				}
			}
			if (min_p == -1)
			{
				flag = -1;break;...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发