Shortest path problem
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 14405 Accepted Submission (s): 4408
Problem Description
Give you n points, M without the edge, each side has a length D and spend P, give you the beginning of the end of T, the minimum distance from the output starting point to the end point and the cost, if the shortest distance there are multiple routes, the output is the least cost.
Input
Enter N,m, the number of the point is 1~n, then the M line, 4 number per line a,b,d,p, indicating A and B have an edge, and its length is D, the cost is P. The last line is two number s,t; start s, end point. N and m are 0 o'clock input end.
(1<n<=1000, 0<m<100000, s! = t)
Output
The output line has two numbers, the shortest distance and the cost.
Sample Input
3 2 1 2 5 6 2 3 4 5 1 3 0 0
Sample Output
9 11
Source
Computer Postgraduate examination of University of Zhejiang University-2010
1. Consider the case of a heavy edge;
2. The procedure uses the common Dijkstra algorithm, may use the heap optimization Dijkstra algorithm or the SPFA, the complexity may reduce some;
View codeproblem:3790 (Shortest path problem) Judge status:acceptedrunid:12776723 language:c++ Author:grantyuancode Render status:rendered by hdoj C + + Code Render Version 0.01 beta#include<iostream> #include <cstdio> #include & lt;cstring> #include <cstdlib> #include <algorithm> #include <queue> #include <vector>using namespace Std;const int maxn=1007;const int maxm=100007;const int inf=0x3fffffff;int cost[maxn][maxn];int c[Maxn];int Dist[maxn][maxn];int d[maxn];bool used[maxn];int n;void dijkstra (int s,int t) {for (int i=1;i<=n;i++) { D[i]=c[i]=inf; used[i]=0; } d[s]=c[s]=0; while (1) {int v=-1; for (int u=1;u<=n;u++) {if (!used[u]&& (v==-1| | D[U]<D[V]) V=u; if (!used[u]&& (v==-1| | (D[u]==d[v]&&c[u]<c[v]))) V=u; } if (v==-1) break; Used[v]=true; for (int u=1;u<=n;u++) {if (D[u]>d[v]+disT[v][u]) {d[u]=d[v]+dist[v][u];c[u]=c[v]+cost[v][u];} if (D[u]==d[v]+dist[v][u]&&c[u]>c[v]+cost[v][u]) c[u]=c[v]+cost[v][u]; }}}int Main () {int s,t,a,b,cc,dd,m; while (1) {memset (cost,0,sizeof (cost)); memset (dist,0,sizeof (Dist)); scanf ("%d%d", &n,&m); if (!n&&!m) break; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {dist[i][j]=dist[j][i]=inf; Cost[i][j]=cost[j][i]=inf; } for (int i=0;i<m;i++) {scanf ("%d%d%d%d", &A,&B,&CC,&DD); if (DIST[A][B]>CC) {dist[a][b]=dist[b][a]=cc; COST[A][B]=COST[B][A]=DD;} if (DIST[A][B]==CC&&COST[A][B]>DD) {dist[a][b]=dist[b][a]=cc; COST[A][B]=COST[B][A]=DD;} } scanf ("%d%d", &s,&t); Dijkstra (s,t); printf ("%d%d\n", d[t],c[t]); } return 0;}
Zhejiang University computer Postgraduate exam-2010 Shortest Path problem