Dijkstra算法


/**
 * DIJKSTRA(简单版) 单源最短路径算法(不允许存在负边)
 * 输入:(1)图g;        // 有向图或者无向图 
 *         (2)源点s。 
 * 输出:(1)源点s到各点的最短路径长dist; 
 *         (2)源点s到各点的最短路径prev。
 * 结构: 图g用邻接矩阵表示,最短路径长dist用数组表示。 
 * 算法:Dijkstra算法  
 * 复杂度:O(|V|^2) 
 */ 
#include <iostream>
#include <vector>
#include <list>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <functional>
#include <climits>
using namespace std;

int n;                    // n : 顶点个数 
vector<vector<int> > g; // g : 图(graph)(用邻接矩阵(adjacent matrix)表示) 
int s;                    // s : 源点(source) 
vector<bool> known;        // known : 各点是否知道最短路径 
vector<int> dist;        // dist : 源点s到各点的最短路径长 
vector<int> prev;        // prev : 各点最短路径的前一顶点

void Dijkstra()            // 贪心算法(Greedy Algorithm) 
{
    known.assign(n, false);
    dist.assign(n, INT_MAX);
    prev.resize(n);            // 初始化known、dist、prev。 
    dist[s] = 0;            // 初始化源点s到自身的路径长为0。 
    for (;;)
    {
        int min = INT_MAX, v = s;
        for (int i = 0; i < n; ++i)
            if (!known[i] && min > dist[i])
                min = dist[i], v = i;    // 寻找未知的最短路径长的顶点v, 
        if (min == INT_MAX) break;        // 如果找不到,退出; 
        known[v] = true;                // 如果找到,将顶点v设为已知, 
        for (int w = 0; w < n; ++w)        // 遍历所有v指向的顶点w, 
            if (!known[w] && g[v][w] < INT_MAX && dist[w] > dist[v] + g[v][w])
                dist[w] = dist[v] + g[v][w], prev[w] = v;    // 调整顶点w的最短路径长dist和最短路径的前一顶点 prev。 
    }
}

void Print_SP(int v)
{
     if (v != s) Print_SP(prev[v]);
     cout << v << " ";
}

int main()
{
    n = 7;
    g.assign(n, vector<int>(n, INT_MAX));
    g[0][1] = 2; g[0][3] = 1; 
    g[1][3] = 3; g[1][4] = 10; 
    g[2][0] = 4; g[2][5] = 5; 
    g[3][2] = 2; g[3][4] = 2; g[3][5] = 8; g[3][6] = 4; 
    g[4][6] = 6; 
    g[6][5] = 1;
    
    s = 0;
    Dijkstra();
    
    copy(dist.begin(), dist.end(), ostream_iterator<int>(cout, " ")); cout << endl;
    for (int i = 0; i < n; ++i)
        if(dist[i] != INT_MAX)
        {
            cout << s << "->" << i << ": ";
            Print_SP(i); 
            cout << endl; 
        }
    
    system("pause");
    return 0;
}
转自http://www.cppblog.com/lzmagic/archive/2009/04/09/79329.html


另外一道练习题
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
Sample Input

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output

2 4



#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define  NIL (-1)
const int INF = (1<<30);

int matrix[500][500];
int dist[500];
int team[500];
int amount[500];
int count[500];
int used[500];

int i,j,k;

void Dijkstra(int n, int s, int e)
{
	dist[s]=0;
	amount[s]=team[s];
	count[s]=1;

	while (1)
	{
		int p, mini=INF;
		for(i=0;i<n;i++)
		{
			if (!used[i]&&dist[i]<mini)
			{
				p = i;
				mini=dist[i];
			}
		}

		if (mini==INF || p==e)
		{
			return;
		}

		used[p]=1;

		for(i=0;i<n;i++)
		{
			if (!used[i])
			{
				int cost = dist[p]+matrix[p][i];
				if(cost<dist[i])
				{
					dist[i]=cost;
					amount[i]= team[i] + amount[p];
					count[i]=count[p];
					
				}
				else if(cost==dist[i])
				{
					
					if(amount[i] < team[i]+amount[p])
						amount[i] = team[i]+amount[p];
					
					count[i] +=count[p];
					
				}
			}

		}
	}
	

}
int main()
{
	
	int n,m,s,e;


	scanf("%d%d%d%d",&n,&m,&s,&e);

	for(i=0;i<n;i++)
	{
		scanf("%d", team+i);
		dist[i] = INF;
		amount[i]=0;
		count[i]=0;
		used[i]=0;

		for(j=0;j<n;j++)
		{
			if(i!=j)
			{
				matrix[i][j]=INF;
			}
			else
			{
				matrix[i][j]=0;
			}

		}

		
	}

	for(i=0;i<m;i++)
	{
		int a,b,d;
		scanf("%d%d%d",&a,&b,&d);

		matrix[a][b]=matrix[b][a]=d;
	}

	Dijkstra(n,s,e);
	printf("%d %d\n", count[e], amount[e]);



	return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值