最短路模板(二)——用拓扑排序解决有向无环图中的最短路

上一篇最短路文章:http://blog.csdn.net/synapse7/article/details/19218865

测试数据:

8 13
5 4 0.35
4 7 0.37
5 7 0.28
5 1 0.32
4 0 0.38
0 2 0.26
3 7 0.39
1 3 0.29
7 2 0.34
6 2 0.40
3 6 0.52
6 0 0.58
6 4 0.93

测试结果:

5 to 0 : 0.73
5 to 1 : 0.32
5 to 2 : 0.62
5 to 3 : 0.61
5 to 4 : 0.35
5 to 5 : 0.00
5 to 6 : 1.13
5 to 7 : 0.28

说明:该算法亦适用于含有负权重边的图。

代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<utility>
#include<functional>
using namespace std;
const int mx = 10005;

struct edge
{
	double cost;
	int to;
	void read()
	{
		scanf("%d%lf", &to, &cost);
	}
} e;

vector<edge> G[mx];
double disTo[mx];
bool vis[mx];
int topo[mx], cnt;

void dfs(int i)
{
	vis[i] = true;
	for (int j = 0; j < G[i].size(); ++j)
		if (!vis[G[i][j].to]) dfs(G[i][j].to);
	topo[cnt++] = i;
}

/// 有向无环图上的最短路
void dagSP(int s)
{
	int i = cnt, j, v;
	while (topo[--i] != s); /// 先在topo中找到s
	memset(disTo, 100, sizeof(disTo));
	disTo[s] = 0.0;
	for (; i >= 0; --i)
	{
		v = topo[i];
		for (j = 0; j < G[v].size(); ++j)
		{
			e = G[v][j]; /// v视作e.from
			disTo[e.to] = min(disTo[e.to], disTo[v] + e.cost);
		}
	}
}

int main()
{
	int n, m, i, a;
	while (~scanf("%d%d", &n, &m))
	{
		for (i = 0; i < n; ++i) G[i].clear();
		while (m--)
		{
			scanf("%d", &a);
			e.read();
			G[a].push_back(e);
		}
		memset(vis, 0, sizeof(vis));
		cnt = 0;
		for (i = 0; i < n; ++i)
			if (!vis[i]) dfs(i);
		dagSP(5);
		for (i = 0; i < n; ++i)
			printf("5 to %d : %.2f\n", i, disTo[i]);
		putchar(10);
	}
	return 0;
}

    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/synapse7/article/details/19284933
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞