Bellman-ford算法队列优化SPFA

题意:

这一晚,TT 做了个美梦!

在梦中,TT 的愿望成真了,他成为了喵星的统领!喵星上有 N 个商业城市,编号 1 ~ N,其中 1 号城市是 TT 所在的城市,即首都。

喵星上共有 M 条有向道路供商业城市相互往来。但是随着喵星商业的日渐繁荣,有些道路变得非常拥挤。正在 TT 为之苦恼之时,他的魔法小猫咪提出了一个解决方案!TT 欣然接受并针对该方案颁布了一项新的政策。

具体政策如下:对每一个商业城市标记一个正整数,表示其繁荣程度,当每一只喵沿道路从一个商业城市走到另一个商业城市时,TT 都会收取它们(目的地繁荣程度 - 出发地繁荣程度)^ 3 的税。

TT 打算测试一下这项政策是否合理,因此他想知道从首都出发,走到其他城市至少要交多少的税,如果总金额小于 3 或者无法到达请悄咪咪地打出 ‘?’。

Input
第一行输入 T,表明共有 T 组数据。(1 <= T <= 50)
对于每一组数据,第一行输入 N,表示点的个数。(1 <= N <= 200)
第二行输入 N 个整数,表示 1 ~ N 点的权值 a[i]。(0 <= a[i] <= 20)
第三行输入 M,表示有向道路的条数。(0 <= M <= 100000)
接下来 M 行,每行有两个整数 A B,表示存在一条 A 到 B 的有向道路。
接下来给出一个整数 Q,表示询问个数。(0 <= Q <= 100000)
每一次询问给出一个 P,表示求 1 号点到 P 号点的最少税费。

Output
每个询问输出一行,如果不可达或税费小于 3 则输出 ‘?’。

Sample Input
2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5
10
1 2 4 4 5 6 7 8 9 10
10
1 2
2 3
3 1
1 4
4 5
5 6
6 7
7 8
8 9
9 10
2
3 10

Sample Output
Case 1:
3
4
Case 2:
?
?


思路:

这道题目给了每座城市的繁荣程度,并且给了道路,并规定走路所交税值的计算方式,因此虽然他没有直接告诉我们边的权值,但是我们可以由两座城市计算出来。

题目需要求得从首都其他城市需要交的税值。即需要求得首都与其他城市之间的最小路,由于有负值,我们不能采用Dijkstra算法,而Bellman-ford算法则可以帮助我们区分出负环。

Bellman-ford算法的正确性基于以下事实 :
最短路经过的路径条数小于图中点的个数
当松弛边(u,v)时,如果dis[u]已经是最短路且u在v的最短 路上,则松弛结束后dis[v]也是最短路,并且从此以后其dis 值不会发生变化

因此若是出现从起点到某个点所经历的边数大于等于n则出现了负环,这时候由于负环可以一直循环导致税越来越少,因此我们需要遍历所有由这个负环可以影响到的点,将他们的与起点的距离设为-inf(无穷小)。

为了建立合适的松弛操作而不进行重复无效的操作,对这个算法进行队列优化,即首先是将与起点相连的点进行第一轮确定,将这些点入队列,与这一轮点相连的点进行第二轮确定,再入队列…为了避免重复入队,设立一个vis数组用以表示节点是否已经入队。与上一轮点没有接触的点没有必要进行讨论,这样在每一轮确定点的时候就可以取消掉其余没有必要的操作。而我们在对点进行操作的时候就要注意他的距离是不是-inf,若是则不操作,注意他到起点经历的边是不是大于等于n,若是则是负环,我们需要遍历所有由这个负环可以影响到的点,将他们的与起点的距离设为-inf(无穷小)。这样我们便能够得到首都到其余城市所交税收。

注意:
注意样例输出有一个Case :1表示第一组数据,因此每一组数据我们需要将这个也输出来,否则WA。


总结:

Floyd算法解决的是多源最短路径问题,对于单源最短路径问题则有很多步骤没有必要, Dijkstra算法在图中存在负权边时不能保证结果的正确性,而Bellman-ford算法则可以帮助我们求得存在负值时的单源最短路径距离问题。


代码:

#include<stdio.h>
#include<vector>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
#define maxcity 205
#define maxroad 100005
#define inf 5*1e8
long long tot;

struct Edge
{
	int u, v, next, w;
};

void add(int& u, int& v, int& w, vector<Edge>& road, vector<int>& head)
{
	road[tot].u = u;
	road[tot].v = v;
	road[tot].w = w;
	road[tot].next = head[u];
	head[u] = tot;
	tot++;
}

void dfs(int s, vector<long long>& dis, vector<Edge>& road, vector<int>& head)
{
	dis[s] = -inf;
	for (int i = head[s]; i != -1; i = road[i].next)
	{
		if (dis[road[i].v] == -inf)continue;
		dfs(road[i].v, dis, road, head);
	}
}

void spfa(int s, vector<long long>& dis,int n, vector<Edge>& road, vector<int>& head, vector<int>&cnt)
{
	dis[s] = 0;
	vector<bool>vis(n, false);
	vis[s] = true;
	queue<int>q;
	q.push(s);

	while (!q.empty())
	{
		int now = q.front();
		vis[now] = false;
		q.pop();
		if (dis[now] == -inf )continue;

		for (int i = head[now]; i != -1; i = road[i].next)
		{
			int u = road[i].u;
			int v = road[i].v;
			int w = road[i].w;
			if (dis[v] == -inf)continue;
			if (dis[v] > dis[u] + w)
			{
				dis[v] = dis[u] + w;
				cnt[v] = cnt[u] + 1;
				if (cnt[v] >= n)
					dfs(v,dis,road,head);
				if (vis[v] == false && dis[v] != -inf)
				{
					q.push(v);
					vis[v] = true;
				}
			}
		}
	}

}

int main()
{
	int T;
	scanf("%d", &T);
	for(int k = 0; k < T; k++)
	{
		tot = 0;
		int n;
		scanf("%d", &n);
		vector<int>city(n);
		for (int i = 0; i < n; i++)scanf("%d", &city[i]);
		long long m;
		scanf("%lld", &m);
		vector<Edge>road(m);
		vector<int>head(n,-1);
		for (long long i = 0; i < m; i++)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			a--;
			b--;
			int w = pow(city[b] - city[a], 3);
			add(a, b, w, road, head);
		}
		vector<long long>dis(n, inf);
		vector<int>cnt(n, 0);
		spfa(0, dis, n, road, head, cnt);
		long long Q;
		scanf("%lld", &Q);
		printf("Case %d:\n", k+1);
		while (Q-- != 0)
		{
			int p;
			scanf("%d", &p);
			p = p - 1;
			if (dis[p] == inf || dis[p] == -inf || dis[p] < 3)
				printf("?\n");
			else
				printf("%lld\n", dis[p]);
		}

	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值