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 21 2 5 62 3 4 51 30 0
Sample Output
9 11
Two points the shortest distance (no negative ring) with the Dijstra algorithm this problem differs in that there are two weights.
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<stack>#include<vector>#include<map>#include<cmath>#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1#defineLL Long Longusing namespacestd;Const intinf=0x3f3f3f3f;Const intmx= ++Ten;intN,M,MAP[MX][MX],PRICE[MX][MX],DIS[MX],PRE[MX];voidinit () { for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) {Map[i][j]=INF; PRICE[I][J]=INF; }}voidDijkstraintv0) { BOOLVIS[MX]; for(intI=1; i<=n;i++) {Dis[i]=Map[v0][i]; Pre[i]=Price[v0][i]; Vis[i]=false; } Dis[v0]=0; Vis[v0]=true; for(intI=1; i<=n;i++){ inttmp=inf,p=Inf,u; for(intj=1; j<=n;j++) if(!vis[j]&&dis[j]<tmp) {u=J; TMP=Dis[j]; } Vis[u]=true; for(intj=1; j<=n;j++) if(!Vis[j]) { if(dis[u]+map[u][j]<Dis[j]) {Dis[j]=dis[u]+Map[u][j]; PRE[J]=pre[u]+Price[u][j]; }Else if(dis[u]+map[u][j]==dis[j]&&pre[u]+price[u][j]<Pre[j]) pre[j]=pre[u]+Price[u][j]; } }}intMain () { while(~SCANF ("%d%d",&n,&m)) { if(n==0&&m==0) Break; Init (); while(m--){ intu,v,w,p; scanf ("%d %d%d%d",&u,&v,&w,&p); if(map[u][v]>W) {Map[u][v]=V; Map[v][u]=W; PRICE[U][V]=p; Price[v][u]=p; }Else if(map[u][v]==w&&price[u][v]>p) {Price[u][v]=p; Price[v][u]=p; } } ints,t; scanf ("%d%d",&s,&t); Dijkstra (s); printf ("%d%d\n", dis[t],pre[t]); } return 0;}
The shortest path problem of HDU 3790 in Hangzhou Electric power ACM