求最短路径


求最短路径的两个常见算法:
1,Floyd算法
代码如下:dis[i][j]保存顶点i与j之间的距离,如果距离等于-1则表示两点不可达;n表示图中的结点数
for(int ik = 1; k <= n; k++){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(dis[i][k] == -1 || dia[k][j] == -1) continue; if(dis[i][j] == -1 || dis[i][k] + dis[k][j] < dis[i][j]) dis[i][j] = dis[i][k] + dis[k][j]; } } }


2,Dijkstra算法求单源最短路径
首先初始化,将源点加入集合k,从源点s出发到其它节点的距离是-1,
然后遍历与集合k中的节点直接相连的边,找出其中最短的边,边最小的节点被选为下一个最短路径确定的点,然后将该点加入集合k
代码如下:map[][]表示存储图的邻接矩阵,dis[i]存储源点到节点的最短路径;used[i] == 1表示节点i已经加入了集合k
void dijkstra(){ //初始化 for(int i = 1; i <= n; i++){ dis[i] = map[i][j]; used[i] = 0; } used[1] = 1; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(used[j] == 0 && dis[j] <= min){ min = dis[j]; k = j; } } } used[k] = 1; for(int j = 1; j <= n; j++){ if(dis[k] + map[k][j] < dis[j]) dis[j] = dis[k] + map[k][j]; } }

九度1447:求最短路 题目地址: http://ac.jobdu.com/problem.php?pid=1447 题目描述: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?
输入:
输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。输入保证至少存在1条商店到赛场的路线。
当输入为两个0时,输入结束。
输出:
对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间。
样例输入:
2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0

样例输出:
3 2

#include using namespace std; #define N 101 int dis[N][N]; #define MAX 1000000 int main(){ int n,m; while(cin >> n >> m){ if(n == 0 &&m == 0) break; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){ if(i == j) dis[i][j] = 0; else dis[i][j] = MAX; }for(int i = 0; i < m; i++){ int c1,c2,t; cin >> c1 >> c2 >> t; if(t < dis[c1][c2]) dis[c1][c2] = dis[c2][c1] = t; }for(int k = 2; k < n; k++){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(dis[i][k] == MAX || dis[k][j] == MAX) continue; if(dis[i][j] == MAX || dis[i][k] + dis[k][j] < dis[i][j]) dis[i][j] = dis[i][k] + dis[k][j]; } } } int ans = dis[1][n]; cout << ans << endl; } return 0; } /************************************************************** Problem: 1447 User: cherish Language: C++ Result: Accepted Time:40 ms Memory:1560 kb ****************************************************************/

上面这是Floyd解法,下面再给出Dijkstra解法: 【求最短路径】

#include using namespace std; #define N 101 int map[N][N]; int dis[N]; int used[N]; #define MAX 1000000 int main(){ int n,m; while(cin >> n >> m){ if(n == 0 &&m == 0) break; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){ if(i == j) map[i][j] = 0; else map[i][j] = MAX; }for(int i = 0; i < m; i++){ int c1,c2,t; cin >> c1 >> c2 >> t; if(t < map[c1][c2]) map[c1][c2] = map[c2][c1] = t; }//Dijkstra算法 //初始化 dij_dis存储从节点1到任意节点的距离 for(int i = 1; i <= n; i++) dis[i] = map[1][i]; for(int i = 2; i <= n; i++) used[i] = 0; used[1] = 1; for(int i = 1; i <= n; i++){ int min = MAX; int k; //遍历所有没有加入集合的节点,找到最短的边 for(int j = 1; j <= n; j++){ if(used[j] == 0 && dis[j] < min){ min = dis[j]; k = j; } } //把最短的边对应的节点加入集合中 used[k] = 1; //根据新加入的最短路计算源点到其他节点的最短路径 for(int j = 1; j <= n; j++){ if(dis[k] + map[k][j] < dis[j]) dis[j] = dis[k] + map[k][j]; }} int ans = dis[n]; cout << ans << endl; } return 0; } /************************************************************** Problem: 1447 User: cherish Language: C++ Result: Accepted Time:30 ms Memory:1560 kb ****************************************************************/


九度1008:最短路径问题 题目地址:http://ac.jobdu.com/problem.php?pid=1008
题目描述:
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
输入:
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t; 起点s,终点t。n和m为0时输入结束。
(1
输出:
输出 一行有两个数, 最短距离及其花费。
样例输入:
3 2 1 2 5 6 2 3 4 5 1 3 0 0

样例输出:
9 11

解题思路:也是求最短路径,只要增加一个数组在记录最短路径的时候顺便记录一下最少花费,在路径长度相等的情况下选择花费比较少的路径。

#include #include using namespace std; #define N 1001 int map[N][N]; //邻接矩阵 int cost[N][N]; //存储每条边对应的花费 int used[N]; //记录是否已经计算出最短路径 int dis[N]; //保存源点到任意点的最短距离 int c[N]; //保存源点到任意点的最少花费 #define MAX 100000000 int main(){ int n,m; while(cin >> n >> m){ if(n == 0 && m == 0) break; //初始化邻接矩阵map和花费cost for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(i == j) { cost[i][j] = 0; map[i][j] = 0; } else{ cost[i][j] = MAX; map[i][j] = MAX; } } }memset(used,0,sizeof(used)); //数据输入 for(int i = 0; i < m; i++){ int v1,v2,dis,c; cin >> v1 >> v2 >> dis >> c; map[v1][v2] = map[v2][v1] = dis; cost[v1][v2] = cost[v2][v2] = c; }//输入源点和目的点 int start,end; cin >> start >> end; //dijkstra算法初始化 在记录距离dis的同时还要记录花费c for(int i = 1; i <= n; i++){ c[i] = cost[start][i]; dis[i] = map[start][i]; } used[start] = 1; //源点记录为已访问 for(int i = 1; i <= n; i++){ int min = MAX; int k; for(int j = 1; j <= n; j++){ if(used[j] == 0&&dis[j] <= min){ min = dis[j]; k = j; } } used[k] = 1; for(int j = 1; j <= n; j++){ if(dis[j] > dis[k] + map[k][j]){ dis[j] = dis[k] + map[k][j]; c[j] = c[k] + cost[k][j]; } else if(dis[j] == dis[k] + map[k][j]){ if(c[j] > c[k] + cost[k][j]){ c[j] = c[k] + cost[k][j]; } } }} cout << dis[end] << " " << c[end] << endl; }return 0; } /************************************************************** Problem: 1008 User: cherish Language: C++ Result: Accepted Time:20 ms Memory:9360 kb ****************************************************************/


九度1162:I wanna go home 题目地址:http://ac.jobdu.com/problem.php?pid=1162
题目描述:
The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.
"For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
Would you please tell Mr. M at least how long will it take to reach his sweet home?
输入:
The input contains multiple test cases.
The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
The second line contains one integer M (0<=M<=10000), which is the number of roads.
The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.
To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.
Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.
输出:
For each test case, output one integer representing the minimum time to reach home.
If it is impossible to reach home according to Mr. M's demands, output -1 instead.
样例输入:
2 1 1 2 100 1 2 3 3 1 2 100 1 3 40 2 3 50 1 2 1 5 5 3 1 200 5 3 150 2 5 160 4 3 170 4 2 170 1 2 2 2 1 0

样例输出:
100 90 540

解题思路:因为Mr.M不想再两个阵营之间来回穿梭,但是出发城市归属于阵营1,而目的城市归属于阵营2,所以只要把所有从阵营2到阵营1的道路设置为不可达,这样就又回到一般的求最短路径的问题了。

#include using namespace std; #define N 610 int map[N][N]; int dis[N]; //用来记录从源点到该点的最短路 int leader[N]; //记录城市i归属的阵营 int used[N]; //记录该城市是否已经计算过最短路径 int main(){ int n,m; while(cin >> n){ if(n == 0) break; cin >> m; //邻接矩阵初始化 for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(i == j) map[i][j] = 0; else map[i][j] = -1; } used[i] = 0; } for(int i = 0; i < m; i++){ int a,b,t; cin >> a >> b >> t; map[a][b] = map[b][a] = t; } for(int i = 1; i <= n; i++) cin >> leader[i]; //因为Mr.M不想在两个阵营之间来回穿梭,但是出发城市归属于阵营1而目的城市归属于阵营2 //所以要把所有从阵营2到阵营1的道路设为不可达 for(int i = 1; i <= n; i++){ if(leader[i] == 2){ for(int j = 1; j <= n; j++){ if(leader[j] == 1) map[i][j] = -1; } } dis[i] = map[1][i]; //dijsktra算法初始化 }used[1] = 1; for(int i =1; i <= n; i++){ int min = 123123123; int k; for(int j = 1; j <= n; j++) if(used[j] == 0 && dis[j] != -1 && dis[j] <= min){ min = dis[j]; k = j; }used[k] = 1; for(int j = 1; j <= n; j++){ if(map[k][j] != -1 && (dis[k] + map[k][j] < dis[j] || dis[j] == -1)){ dis[j] = dis[k] + map[k][j]; } } } cout << dis[2] << endl; }return 0; } /************************************************************** Problem: 1162 User: cherish Language: C++ Result: Accepted Time:20 ms Memory:2980 kb ****************************************************************/


总结:Floyd算法的时间复杂度是O(n3),在一般允许的时间复杂度范围内可以计算大约200个节点的图;
Floyd算法能够算出任意两点之间的最短路径,适用于求多个节点之间的最短路径问题;Floyd算法用邻接矩阵表示图。
Dijkstra算法适用于求单源最短路径,可以采用邻接矩阵存储图也可以采用邻接表表示图,视情况而定。

    推荐阅读