HDU 3790 最短路径问题 (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790
简单的最短路问题,这题听说有重边。我用spfa和dijkstra写了一遍,没判重边,速度都差不多。
dijkstra+优先队列:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <vector> 6 using namespace std; 7 const int INF = 1e9 + 7; 8 struct Node { 9 int _node , _dis , _cost; 10 bool operator <(const Node &cmp) const { 11 if(_dis == cmp._dis) 12 return _cost > cmp._cost; 13 return _dis > cmp._dis; 14 } 15 }; 16 struct Edge { 17 int to , _dis , _cost , next; 18 }edge[int(2e5 + 5)]; 19 int dis[1005] , cost[1005] , head[1005] , cnt; 20 21 inline void add(int u , int v , int _dis , int _cost) { 22 edge[cnt].next = head[u]; 23 edge[cnt].to = v; 24 edge[cnt]._cost = _cost; 25 edge[cnt]._dis = _dis; 26 head[u] = cnt++; 27 } 28 29 void dijkstra(int s) { 30 dis[s] = cost[s] = 0; 31 priority_queue <Node> que; 32 while(!que.empty()) { 33 que.pop(); 34 } 35 que.push(Node{s , 0 , 0}); 36 while(!que.empty()) { 37 Node temp = que.top(); 38 que.pop(); 39 int u = temp._node; 40 if(temp._dis > dis[u]) 41 continue; 42 for(int i = head[u] ; ~i ; i = edge[i].next) { 43 int v = edge[i].to; 44 if(dis[v] > dis[u] + edge[i]._dis) { 45 dis[v] = dis[u] + edge[i]._dis; 46 cost[v] = cost[u] + edge[i]._cost; 47 que.push(Node{v , dis[v] , cost[v]}); 48 } 49 else if(dis[v] == dis[u] + edge[i]._dis && cost[v] > cost[u] + edge[i]._cost) { 50 cost[v] = cost[u] + edge[i]._cost; 51 que.push(Node{v , dis[v] , cost[v]}); 52 } 53 } 54 } 55 } 56 57 int main() 58 { 59 int n , m , u , v , d , c; 60 while(~scanf("%d %d" , &n , &m) && (n || m)) { 61 for(int i = 1 ; i <= n ; ++i) 62 cost[i] = dis[i] = INF; 63 memset(head , -1 , sizeof(head)); 64 cnt = 0; 65 while(m--) { 66 scanf("%d %d %d %d" , &u , &v , &d , &c); 67 add(u , v , d , c); 68 add(v , u , d , c); 69 } 70 scanf("%d %d" , &u , &v); 71 dijkstra(u); 72 printf("%d %d\n" , dis[v] , cost[v]); 73 } 74 return 0; 75 }
spfa:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 const int INF = 1e9 + 7; 8 typedef pair <int , pair<int , int> > P; //邻接点 长度 花费 9 int dis[1005] , cost[1005]; 10 vector <P> G[1005]; 11 void spfa(int s) { 12 dis[s] = cost[s] = 0; 13 queue <int> que; 14 while(!que.empty()) { 15 que.pop(); 16 } 17 que.push(s); 18 while(!que.empty()) { 19 int node = que.front(); 20 que.pop(); 21 for(int i = 0 ; i < G[node].size() ; ++i) { 22 int temp = G[node][i].first; 23 if(dis[temp] > dis[node] + G[node][i].second.first) { 24 dis[temp] = dis[node] + G[node][i].second.first; 25 cost[temp] = cost[node] + G[node][i].second.second; 26 que.push(temp); 27 } 28 else if(dis[temp] == dis[node] + G[node][i].second.first && cost[temp] > cost[node] + G[node][i].second.second) { 29 cost[temp] = cost[node] + G[node][i].second.second; 30 que.push(temp); 31 } 32 } 33 } 34 } 35 36 int main() 37 { 38 int n , m , u , v , d , c; 39 while(~scanf("%d %d" , &n , &m) && (n || m)) { 40 for(int i = 1 ; i <= n ; ++i) { 41 dis[i] = cost[i] = INF; 42 G[i].clear(); 43 for(int j = i + 1 ; j <= n ; ++j) 44 to[i][j] = to[j][i] = INF; 45 } 46 for(int i = 0 ; i < m ; ++i) { 47 scanf("%d %d %d %d" , &u , &v , &d , &c); 48 G[u].push_back(P(v , make_pair(d , c))); 49 G[v].push_back(P(u , make_pair(d , c))); 50 } 51 scanf("%d %d" , &u , &v); 52 spfa(u); 53 printf("%d %d\n" , dis[v] , cost[v]); 54 } 55 return 0; 56 }