Problem-G

概述:一些城市被水淹没,现在要求在这些城市中修建可以连通的道路,其中一些城市本身存在,求在这种情况下的最短路。

思路:kruskal算法问题,边权排序然后用并查集添加边即可。

感想:下午做的,然后写了一篇博客,结果晚上在管理博客时候不小心删掉了。。然后再来补上。下午做的时候开始以为是prim的题目,然后画了好久的图才发现prim算法没有办法解决,不是强连通问题,而且是最短路问题,所以自然考虑的到了kruskal算法,自己做出来的代码在oj上一直timelimit,改过几次代码,结果都不对,然后保留了核心代码,取了一个ac的一些变量设置。。结果就过了。。。尴尬。。

#include<iostream>
#include<string.h>
#include<fstream>
#include<stdio.h>
#include<algorithm>
#include<cmath>

using namespace std;

struct edge 
{
	int x, y, w;
	bool operator <(const edge& ee)const { return w<ee.w; }
}e[25005];
int p[505], cas, n, m, k;
int road[505];
int find(int x) 
{ 
	return x == p[x] ? x : p[x] = find(p[x]); 
}
int main() 
{
	//ifstream cin ("in.txt");
	scanf("%d", &cas);
	while (cas--) 
	{
		scanf("%d%d%d", &n, &m, &k);
		for (int i = 1;i <= n;i++)
			p[i] = i;
		memset(road, 0, sizeof (road));
		for (int i = 0;i<m;i++)
			scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].w);
		for (int i = 0;i<k;i++) 
		{
			int t, start, end;
			scanf("%d%d", &t, &start);t--;
			while (t--) 
			{
				scanf("%d", &end);
				p[find(start)] = find(end);
			}
		}
		int to = 0, cnt = 0;
		for (int i = 1;i <= n;i++)
			if (!road[find(i)])to++, road[find(i)] = 1;
		to--;
		sort(e, e + m);

		int sum = 0;
		for (int i = 0;i<m;i++) 
		{
			int x = find(e[i].x), y = find(e[i].y);
			if (x == y)continue;
			sum += e[i].w;
			p[x] = y;
			cnt++;
			if (cnt == to)break;
		}

		if (cnt<to)printf("-1\n");
		else printf("%d\n", sum);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
error: Command "x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/lib/python3/dist-packages/numpy/core/include -I/usr/lib/python3/dist-packages/numpy/core/include -Ibuild/src.linux-x86_64-3.9/numpy/distutils/include -I/usr/include/python3.9 -c skimage/_shared/transform.c -o build/temp.linux-x86_64-3.9/skimage/_shared/transform.o -MMD -MF build/temp.linux-x86_64-3.9/skimage/_shared/transform.o.d -msse -msse2 -msse3" failed with exit status 1 ########### EXT COMPILER OPTIMIZATION ########### Platform : Architecture: x64 Compiler : gcc CPU baseline : Requested : 'min' Enabled : SSE SSE2 SSE3 Flags : -msse -msse2 -msse3 Extra checks: none CPU dispatch : Requested : 'max -xop -fma4' Enabled : SSSE3 SSE41 POPCNT SSE42 AVX F16C FMA3 AVX2 AVX512F AVX512CD AVX512_KNL AVX512_KNM AVX512_SKX AVX512_CLX AVX512_CNL AVX512_ICL Generated : none CCompilerOpt.cache_flush[809] : write cache to path -> /tmp/pip-install-vds_g0pc/scikit-image_ce84e6b3faf149029da8bbdd92daa66d/build/temp.linux-x86_64-3.9/ccompiler_opt_cache_ext.py [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: legacy-install-failure × Encountered error while trying to install package. ╰─> scikit-image note: This is an issue with the package mentioned above, not pip. hint: See above for output from the failure.、
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值