舒适的路(并查集 + 枚举)

 最舒适的路

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 15  Solved: 8
[ Submit][ Status][ Web Board]

Description

中国有许多大城市,城市之间通过高速铁路进行交流,每条高铁都对行驶在上面的特快列车限制了固定的速度值,同时中国人对特快列车的“舒适度”有很特殊的要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(可以理解为高铁的限速要求,特快列车必须瞬间提速/降速,好痛苦呀 )。

但中国人对时间却没那么多要求。要你找出一条城市间的最舒适的路径。(高铁是双向的)。

Input

多个测试实例,每个实例包括:

第一行有2个正整数N(1<n<=200)和M (m<=1000),表示有N个城市和M条高铁。

接下来的行是三个正整数X, Y, V,表示从出发城市到目的城市,并且速度为V。V<=1000000

然后是一个正整数K(K<11),表示寻路的个数。

接下来K行每行有2个正整数,表示寻路的起终点。

Output

每个寻路要求打印一行,仅输出一个非负整数表示最佳路线的舒适度最高速与最低速的差。如果起点和终点不能到达,那么输出-1。

Sample Input

4 41 2 22 3 41 4 13 4 221 31 2

Sample Output

10


这道题可以用最短路的思想解决也可以用并查集+枚举的方法解决

核心算法就是这个:

排完序之后我各种情况枚举,当我查找当前的1条路径的时候我不断的加举例,并且判断这俩个点连没连通如果连通就break。

因为有好多路线所以俩个for循环记住要遍历一遍就重新初始化一下

一开始有点难理解,然后会了之后这是一道很水的并查集

cin >> x >> y;
			int ans = maxn;
			for(int j = 0; j < m; j++)
			{
				fun();
				int maxx = 0, minn = 0x3f3f3f3f;
				for(int p = j; p < m; p++)
				{
					join(s[p].x, s[p].y);
					maxx = max(maxx, s[p].s);
					minn = min(minn, s[p].s);
					if(Find(x) == Find(y))
					{
						ans = min(ans, maxx - minn);
						break;
					}
				}
			}
			if(ans == maxn)
			{
				cout << -1 << endl;
			}
			else
			{
				cout << ans << endl;
			}


# include <iostream>
# include <numeric>
# include <algorithm>
# include <functional>
# include <list>
# include <map>
# include <set>
# include <stack>
# include <deque>
# include <queue>
# include <vector>
# include <ctime>
# include <cstdlib>
# include <cmath>
# include <string>
# include <cstring>

using namespace std;

struct node
{
	int x, y, s;
};

int pre[205];
const int maxn = 0x3f3f3f3f;

bool cmp(struct node s1, struct node s2)
{
	return s1.s < s2.s;
}

void fun()
{
	for(int i = 0; i < 205; i++)
	{
		pre[i] = i;
	}
}

int Find(int x)
{
	if(x != pre[x])
	{
		pre[x] = Find(pre[x]);
	}
	return pre[x];
}

void join(int x, int y)
{
	int fx = Find(x);
	int fy = Find(y);
	if(fx != fy)
	{
		pre[fy] = fx;
	}
}

int main(int argc, char *argv[])
{
	int n, m;
	while(cin >> n >> m)
	{
		node s[1005];
		for(int i = 0; i < m; i++)
		{
			cin >> s[i].x >> s[i].y >> s[i].s;
		}
		sort(s, s + m, cmp);
		int k;
		cin >> k;
		for(int i = 0; i < k; i++)
		{
			int x, y;
			cin >> x >> y;
			int ans = maxn;
			for(int j = 0; j < m; j++)
			{
				fun();
				int maxx = 0, minn = 0x3f3f3f3f;
				for(int p = j; p < m; p++)
				{
					join(s[p].x, s[p].y);
					maxx = max(maxx, s[p].s);
					minn = min(minn, s[p].s);
					if(Find(x) == Find(y))
					{
						ans = min(ans, maxx - minn);
						break;
					}
				}
			}
			if(ans == maxn)
			{
				cout << -1 << endl;
			}
			else
			{
				cout << ans << endl;
			}
		}
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值