【PTA刷题】甲级 拓扑排序+多源Dijkstra

题源:7-4 Professional Ability Test (30point(s))

大意:
给一个图,有两个权重dis和value。前一个权重越小越好,后一个越大越好。
节点之间有前置关系(拓扑排序),最后一行给出查询的点,问每次查询的最短路径(Dijkstra)。

输入样例:

/*Each input file contains one test case. For each case, the first line gives two positive integers N (≤1000) and M, being the total numbers of tests and prerequisite relations, respectively. Then M lines follow, each describes a prerequisite relation in the following format:
T1 T2 S D
where T1 and T2 are the indices (from 0 to N−1) of the two distinct tests; S is the minimum score (in the range (0, 100]) required to pass T1 in order to be qualified to take T2; and D is the value of the voucher (in the range (0, 500]) one can receive if one passes T1 with a score no less than S and plan to take T2. It is guaranteed that at most one pair of S and D are defined for a prerequisite relation.

Then another positive integer K (≤N) is given, followed by K queries of tests. All the numbers in a line are separated by spaces.*/

8 15
0 1 50 50
1 2 20 20
3 4 90 90
3 7 90 80
4 5 20 20
7 5 10 10
5 6 10 10
0 4 80 60
3 1 50 45
1 4 30 20
1 5 50 20
2 4 10 10
7 2 10 30
2 5 30 20
2 6 40 60
8
0 1 2 3 4 5 6 7

输出样例:

Okay.
You may take test 0 directly.
0->1
0->1->2
You may take test 3 directly.
0->1->2->4
0->1->2->4->5
0->1->2->6
3->7

输入样例2:

4 5
0 1 1 10
1 2 2 10
3 0 4 10
3 2 5 10
2 0 3 10
2
3 1

输出样例2:

Impossible.
You may take test 3 directly.
Error.

参考题解:https://blog.csdn.net/weixin_45748610/article/details/110259317
思路:一开始没做出来,一直卡在多源如何求最短路径上,因为往常的Dijkstra都会给一个固定的起点。
后来看了大佬的题解,发现核心解法就是-- 在Dijkstra的时候加入一个虚拟节点N(原本最大是N-1),让N成为节点。连接好N和当前入度为0的节点,就是一道普通的最短路径题了。 在这里,用int pre[maxnum]直接维护前驱节点就好了(如果建树再DFS更麻烦了)。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>

using namespace std;

const int maxnum = 1005;
const int INF = 99999;
int N, M, K;
int G[maxnum][maxnum];
int V[maxnum][maxnum];

int d[maxnum];  //第一权重
int sfed[maxnum]; //第二权重
bool viewed[maxnum];

vector<int> ling[maxnum]; //临接矩阵
int inDegree[maxnum] = { 0 }; //入度数组

bool toprank()  //拓扑排序判环
{
	queue<int> Q;
	vector<int> tmpIn(inDegree, inDegree + N);  //搞个副本避免影响全局

	for (int i = 0; i < N; i++)
		if (tmpIn[i] == 0)
			Q.push(i);

	int cnt = 0;
	while (!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		cnt++; //进过队列的数量

		for (int i = 0; i < ling[u].size(); i++)
		{
			int v = ling[u][i]; //临接点

			tmpIn[v]--;  //更新入度信息
			if (tmpIn[v] == 0)
				Q.push(v);
		}
	}
	if (cnt == N)
		return true;
	else
		return false;
}

int pre[maxnum];
void Dijkstra()  //加一个不存在的点N,使得只有N的入度为0
{
	fill(viewed, viewed + maxnum, false);
	fill(d, d + maxnum, INF);
	fill(sfed, sfed + maxnum, INF);
	d[N] = 0;
	
	for (int i = 0; i < N; i++)  //把新加入的虚拟节点N与之前入度为0的点连起来
	{
		if (inDegree[i] == 0)
		{
			G[N][i] = 0;
			V[N][i] = 0;
		}
	}

	for (int i = 0; i < N + 1; i++)
	{
		int u = -1, MIN = INF;
		for (int j = 0; j < N + 1; j++)
		{
			if (viewed[j] == false && d[j] < MIN)
			{
				u = j;
				MIN = d[j];
			}
		}

		if (u == -1) return;
		viewed[u] = true;

		for (int v = 0; v < N + 1; v++)
		{
			if (viewed[v] == false && G[u][v] != INF)
			{
				if (d[u] + G[u][v] < d[v])
				{
					d[v] = d[u] + G[u][v];
					sfed[v] = sfed[u] + V[u][v]; //更新两个权重
					pre[v] = u;
				}
				else if (d[u] + G[u][v] == d[v] && sfed[u] + V[u][v] > sfed[v])
				{
					sfed[v] = sfed[u] + V[u][v];
					pre[v] = u;
				}
			}
		}
	}
}

void no_consist()
{
	int tmp;
	for (int i = 0; i < K; i++)
	{
		cin >> tmp;
		if (inDegree[tmp] == 0)
			printf("You may take test %d directly.\n", tmp);
		else
			printf("Error.\n");
	}
}

void consist()
{
	int tmp;
	vector<int> path;

	for (int i = 0; i < K; i++)
	{
		cin >> tmp;
		if(inDegree[tmp] == 0)
		{
			printf("You may take test %d directly.\n", tmp);
			continue;
		}

		path.clear();
		while (tmp != N)  //追溯路径
		{
			path.push_back(tmp);
			tmp = pre[tmp];
		}
		for (int j = path.size() - 1; j >= 0; --j)
		{
			if (j == path.size() - 1)
				printf("%d", path[j]);
			else
				printf("->%d", path[j]);
		}
		printf("\n");
	}


}

int main()
{
	cin >> N >> M;
	fill(G[0], G[0] + maxnum * maxnum, INF);
	fill(V[0], V[0] + maxnum * maxnum, INF);

	for (int i = 0; i < M; i++)
	{
		int a, b, dis, val;
		cin >> a >> b >> dis >> val;

		G[a][b] = dis;
		V[a][b] = val;
		ling[a].push_back(b);//a的临接矩阵中存b
		inDegree[b]++; //b的入度加一
	}

	Dijkstra();
	//for (int i = 0; i <= N; i++)
	//	cout << " " << pre[i];

	cin >> K;

	if (toprank())
	{
		cout << "Okay.\n";
		consist();
	}
	else
	{
		cout << "Impossible.\n";
		no_consist();
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值