Original link:
http://www.cnblogs.com/sykline/p/9737881.html
Meaning of the questions:
Ideas: single-source shortest path problem, Dijkstra algorithm to get on it, because to find the shortest cheapest, it is necessary to add a condition in the shortest distance when the update (that is, when the shortest distance equal time, if it takes the path smaller, updated minimum cost) on it. Before the shortest of their level of learning will be limited to the question of the level of the template, we can now add some slight changes in the conditions, do the job data structure, the way to deepen their understanding of the shortest path (Dijkstra) algorithm.
Title schematic given sample (data in the code behind):
Code:
1 #include <iostream> 2 #include <queue> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cmath> 6 #include <cstring> 7 #include <queue> 8 #include <vector> 9 #define INF 0x3f3f3f3f 10 #define FRE() freopen("in.txt","r",stdin) 11 12 using namespace std; 13 typedef long long ll; 14 typedef pair<int,int> P;First is the distance, second is point number// 15 const int maxn = 1000; 16 struct Edge{ 17 int to,c,d; 18 Edge(int t,int cost,int dis):to(t),c(cost),d(dis){} 19 }; 20 vector<Edge> G[maxn]; 21 priority_queue<P, vector<P>, greater<P> > que; 22 int dist[maxn],cost[maxn]; 23 int n,m,st,en; 24 25 26 void init(){ 27 int s,e,d,c; 28 scanf("%d%d%d%d",&n,&m,&st,&en); 29 for(int i = 0; i<m; i++){ 30 scanf("%d%d%d%d",&s,&e,&d,&c); 31 G[s].push_back(Edge(e,c,d)); 32 G[e].push_back(Edge(s,c,d)); 33 } 34 for(int i = 0; i<n; i++){ 35 dist[i] = INF; 36 cost[i] = INF; 37 } 38 } 39 40 int main () { 41 is // FRE (); 42 is the init (); 43 is dist [ST] = 0 ; 44 is cost [ST] = 0 ; 45 que.push (P ( 0 , ST)); // refers to the shortest distance to the point 46 is the while (que.size ()) { 47 P P = que.top (); 48 que.pop (); 49 int V = p.second; // current point 50 IF (p.first> dist [V]) Continue ; 51 is // COUT << "V:" << V; 52 is for ( int I = 0 ; I <G [V] .size (); I ++ ) { 53 is Edge E = G [V] [I]; // if the shortest distance is equal to the time it takes less time, the shortest update spent distance 54 is IF ((dist [e.to]> dist [V] + ED) || (dist [e.to] == dist [V] + ED && cost [e.to]> cost [V] + EC)) { 55 dist [e.to] = dist [V] + ED; 56 is cost [e.to] = cost [V] + EC; 57 is // COUT << "cost:" << cost [E .to]; 58 que.push (P (dist [e.to], e.to)); 59 } 60 // COUT << "" << dist [e.to]; 61 } 62 // COUT << endl; 63 is } 64 the printf ( " % D% D \ n- " , dist [EN], cost [EN]); 65 return 0 ; 66 } 67 / * 68 Sample input: 69 . 4. 5 0 . 3 70 0. 1. 1 20 is 71 is . 1. 3 2 30 72 0 10. 3. 4 73 is 0 2 2 20 is 74 2. 3. 1 20 is 75 sample output: 76 . 3 40 77 * /
Reproduced in: https: //www.cnblogs.com/sykline/p/9737881.html