数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )

时间:2022-05-24 02:12:28

数据结构实验之图论七:驴友计划

Time Limit: 1000 ms           Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

做为一个资深驴友,小新有一张珍藏的自驾游线路图,图上详细的标注了全国各个城市之间的高速公路距离和公路收费情况,现在请你编写一个程序,找出一条出发地到目的地之间的最短路径,如果有多条路径最短,则输出过路费最少的一条路径。

Input

连续T组数据输入,每组输入数据的第一行给出四个正整数N,M,s,d,其中N(2 <= N <= 500)是城市数目,城市编号从0~N-1,M是城市间高速公路的条数,s是出发地的城市编号,d是目的地的城市编号;随后M行,每行给出一条高速公路的信息,表示城市1、城市2、高速公路长度、收费额,中间以空格间隔,数字均为整数且不超过500,输入数据均保证有解。

Output

在同一行中输出路径长度和收费总额,数据间用空格间隔。

Sample Input

1
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output

3 40

Dijkstra算法:

#include <iostream>
#include <queue>
#define INF 999999
#define ERROR -1
using namespace std; int n, m, s, d;
struct node
{
int length;
int cost;
} G[1001][1001];
int Min, i, j;
int V, W;
bool collected[1001];
int dist[1001], spend[1001]; int FindMinDist( )
{
Min = INF;
for(i=0; i<n; i++)
if( !collected[i] && dist[i] < Min )
{
Min = dist[i];
V = i;
}
if( Min == INF )
V = ERROR;
return V;
} void Dijkstra( )
{
while(1)
{
V = FindMinDist( );
if( V == ERROR )
break;
collected[V] = true;
for( W=0; W<n; W++ )
if( collected[W] == false )
{
if( dist[V] + G[V][W].length < dist[W] )
{
dist[W] = dist[V] + G[V][W].length;
spend[W] = spend[V] + G[V][W].cost;
}
else if( dist[V] + G[V][W].length == dist[W] )
{
if( spend[V] + G[V][W].cost < spend[W] )
{
dist[W] = dist[V] + G[V][W].length;
spend[W] = spend[V] + G[V][W].cost;
}
}
}
}
} int main()
{
int t;
cin >> t;
while( t-- )
{
cin >> n >> m >> s >> d;
for( i=0; i<n; i++ )
{
collected[i] = false;
for(j=0; j<n; j++ )
{
if( i == j )
{
G[i][j].length = 0;
G[i][j].cost = 0;
}
else
{
G[i][j].length = INF;
G[i][j].cost = INF;
}
}
dist[i] = INF;
spend[i] = INF;
} dist[s] = 0;
spend[s] = 0;
while( m-- )
{
int a, b, le, mo;
cin >> a >> b >> le >> mo;
if(le < G[a][b].length)
{
G[a][b].length = le;
G[b][a].length = le;
G[a][b].cost = mo;
G[b][a].cost = mo;
}
}
Dijkstra( );
cout << dist[d] << " " << spend[d] << endl;
}
return 0;
}

Floyd算法:

#include <iostream>
#include <string.h>
#define INF 99999
#define ERROR -1
using namespace std; int n, m, s, d;
int length[101][101];
int cost[101][101];
int a[101][101], b[101][101]; void Floyd ( )
{
int k, i, j;
for( k=0; k<n; k++ )
{
for( i=0; i<n; i++ )
{
for( j=0; j<n; j++)
{
if( i!=j )
{
if( length[i][j] > length[i][k] + length[k][j] )
{
length[i][j] = length[i][k] + length[k][j];
cost[i][j] = cost[i][k] + cost[k][j];
}
else if( length[i][j] == length[i][k] + length[k][j] )
{
if( cost[i][j] > cost[i][k] + cost[k][j] )
{
cost[i][j] = cost[i][k] + cost[k][j];
}
}
}
}
}
}
} int main()
{
int i, j;
int t;
cin >> t;
while ( t-- )
{
cin >> n >> m >> s >> d;
for( i=0; i<n; i++ )
{
for( j=0; j<n; j++ )
{
if( i!=j )
{
length[i][j] = INF;
cost[i][j] = INF;
}
else
{
length[i][j] = 0;
cost[i][j] = 0;
}
}
}
while ( m-- )
{
int a, b, x, y;
cin >> a >> b >> x >> y;
if( x < length[a][b] )
{
length[a][b] = x;
length[b][a] = x;
cost[a][b] = y;
cost[b][a] = y;
}
}
Floyd ( );
cout << length[s][d] << " " << cost[s][d] << endl;
}
return 0;
}

有关这两种算法:最短路径 Dijkstra算法 AND Floyd算法