题意 : 中文题不详述。
思路 :无论是SPFA还是Dijkstra都在更新最短路的那个地方直接将花费更新了就行,还有别忘了判重边,话说因为忘了判重边WA了一次。
1 //3790 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <queue> 7 const int INF = 1 << 28 ; 8 9 using namespace std; 10 11 int n, m,s,t; 12 int cost[1100][1100] ,mapp[1100][1010]; 13 int dist[1100],cost1[1100] ; 14 int vis[1100] ; 15 16 void spfa(int src) 17 { 18 queue<int> Q ; 19 memset(vis,0,sizeof(vis)) ; 20 for(int i = 1 ; i <= n ; i++) 21 { 22 dist[i] = INF ; 23 cost1[i] = INF ; 24 } 25 dist[src] = 0 ; 26 vis[src] = 1 ; 27 cost1[src] = 0; 28 Q.push(src) ; 29 while(!Q.empty()) 30 { 31 int u = Q.front() ; 32 Q.pop() ; 33 vis[u] = 0; 34 for(int i = 1 ; i <= n ; i++) 35 { 36 37 if(dist[i] > dist[u] + mapp[u][i]) 38 { 39 dist[i] = dist[u] + mapp[u][i] ; 40 cost1[i] = cost1[u] + cost[u][i] ; 41 if(!vis[i]) 42 { 43 vis[i] = 1 ; 44 Q.push(i) ; 45 } 46 } 47 else if(dist[i] == dist[u]+mapp[u][i]) 48 { 49 if(cost1[i] > cost1[u] + cost[u][i]) 50 { 51 cost1[i]=cost1[u] + cost[u][i]; 52 if(!vis[i]) 53 { 54 vis[i] = 1 ; 55 Q.push(i) ; 56 } 57 } 58 59 } 60 } 61 } 62 } 63 void Init() 64 { 65 for(int i = 1 ; i <= n ; i++) 66 for(int j = 1 ; j <= n ; j++) 67 { 68 if(i == j) mapp[i][j] = cost[i][j] = 0 ; 69 else mapp[i][j] = cost[i][j] = INF ; 70 } 71 } 72 int main() 73 { 74 // 75 while(cin >> n >> m) 76 { 77 if(n == 0 && m == 0) break ; 78 int a, b , d , p ; 79 Init() ; 80 for(int i = 0 ; i < m ; i++) 81 { 82 cin >> a >> b >> d >> p ; 83 if(mapp[a][b] > d) 84 { 85 mapp[a][b] = mapp[b][a] = d ; 86 cost[a][b] = cost[b][a] = p ; 87 } 88 else if(mapp[a][b] == d&&cost[a][b] > p) 89 cost[a][b] = cost[b][a] = p ; 90 } 91 cin >> s >> t ; 92 spfa(s) ; 93 printf("%d %d\n",dist[t],cost1[t]) ; 94 } 95 return 0; 96 }