Test instructions: Give the n points, M edge, the length of each side D and spend P, give the beginning and end of the shortest distance and cost, to find the shortest distance, if there are multiple shortest distance, the output of the least cost
When using Dijkstra to find the shortest distance, then use a f[] array to save the minimum cost.
This problem wa several times.
Because there is no heavy side to be taken into account when building the diagram,
And in the case of a drawing, if you encounter w[a][b] equal, you should update the corresponding cost to a smaller one.
And also
When writing the Dijkstra function, take the end point into the number of points that is wrong because if given the starting point, the end point is St,en
So if you only calculate to the en point, if it's better from St to En+1,en+1 to EN, then it's wrong.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intmaxn=1010; - intW1[MAXN][MAXN],W2[MAXN][MAXN],F[MAXN],D[MAXN],USED[MAXN]; - intW[MAXN][MAXN]; - + voidDijkstraintStinten) { -memset (Used,0,sizeof(used)); + for(intI=1; i<=en;i++) d[i]=INF; Ad[st]=0; at - for(intI=1; i<=en;i++) f[i]=INF; -f[st]=0; - - for(intk=1; k<=en;k++){ - intp,m=INF; in for(intI=1; i<=en;i++)if(!used[i]&&d[i]<m) m=d[p=i]; -used[p]=1; to for(intI=1; i<=en;i++) { + if(d[i]>d[p]+w1[p][i]| | (d[i]==d[p]+w1[p][i]&&f[i]>f[p]+W2[p][i])) { -d[i]=d[p]+W1[p][i]; thef[i]=f[p]+W2[p][i]; * } $ }Panax Notoginseng } - } the + intMain () { A inta,b,c,e; the intn,m; + intSt,en; - while(SCANF ("%d%d", &n,&m)!=eof&&n&&m) { $memset (W1,0x3f,sizeof(W1)); $memset (W2,0x3f,sizeof(W2)); - - for(intI=1; i<=m;i++){ thescanf"%d %d%d%d",&a,&b,&c,&e); - if(W1[a][b] >c)Wuyi { theW1[A][B] = W1[b][a] =C; -W2[A][B] = W2[b][a] =e; Wu } - Else if(W1[a][b] = =c) AboutW2[A][B] = W2[b][a] =min (w2[a][b],e); $ } -scanf"%d%d",&st,&en); - Dijkstra (st,n); -printf"%d%d\n", D[en],f[en]); A } + return 0; the}
View Code
HDU 3790 Shortest path problem "Dijkstra"