1 #include <iostream>
2 #include <stdio.h>
3 #include <string.h>
4 #include <cstring>
5 using namespace std;
6
7 const int INF = 1000000;
8 const int MAXSIZE = 1005;
9
10 int map[MAXSIZE][MAXSIZE];
11 int price[MAXSIZE][MAXSIZE];
12 int n;
13
14 void Dijkstra(int s,int e){
15 bool done[MAXSIZE];
16 int dis[MAXSIZE],co[MAXSIZE];
17
18 memset(done,0,sizeof(done));
19
20 for(int i = 0;i <= n;i++)
21 dis[i] = INF;
22 dis[s] = co[s] = 0;
23
24 for(int i = 0;i <= n;i++){//最多执行n+1次操作
25 int pos,minn = INF;
26 for(int j = 0;j <= n;j++){//先找到d[]最小的点
27 if(!done[j] && dis[j] < minn){
28 minn = dis[j];
29 pos = j;
30 }
31 }
32 done[pos] = 1;//将该点加入集合
33 for(int j = 0;j <= n;j++){//再更新所有的d[]
34 //if(!done[j] && d[minx] + map[minx][j] < d[j])
35 //d[j] = d[minx] + map[minx][j];
36
37 if (!done[j] && map[pos][j] != INF){
38 //若存在更小的路径,取更小的路径
39 if (dis[j] > dis[pos] + map[pos][j]){
40 dis[j] = map[pos][j] + dis[pos];
41 co[j] = price[pos][j] + co[pos];
42 }
43 //当最短路径相等时候,取价格小的
44 else if (dis[j] == dis[pos] + map[pos][j] && co[j] > co[pos] + price[pos][j])
45 co[j] = co[pos] + price[pos][j];
46 }
47 }
48 }
49 printf("%d %d\n",dis[e],co[e]);
50 }
51
52 int main(){
53 int m,s;
54 int i,j;
55 int p,q,t,pri;
56 int ww,ans;
57
58 while(scanf("%d%d",&n,&m) , n || m){
59 ans = 100000000;
60 for(i = 0;i < MAXSIZE;i++){
61 for(j = 0;j < MAXSIZE;j++){
62 if(i == j)
63 map[i][j] = 0;
64 else
65 map[i][j] = INF;
66 }
67 }
68 while(m--){
69 scanf("%d%d%d%d",&p,&q,&t,&pri);
70 if(t < map[p][q]){//可能两站间存在多条线路取短的那条路
71 map[p][q] = t;
72 map[q][p] = t;
73 price[p][q] = pri;
74 price[q][p] = pri;
75 }
76 }
77 scanf("%d%d",&ww,&s);
78 map[0][ww] = 0;
79 Dijkstra(0,s);//巧妙之处,加入超级源点0
80 }
81 return 0;
82 }
HDOJ 3790 双权值Dijkstra,布布扣,bubuko.com
时间: 2024-10-02 10:27:01