It is obvious that the shortest path of a single source node is short. My practice for this question is to apply the spfa algorithm directly. This algorithm is better and can handle the situation where the edge weight is negative. This topic is sorted by a structure. If the distance is smaller than the current distance, you can modify it. If the distance is equal, you can determine the size of the cost. This type of question is actually a template question.
[Cpp] <SPAN style = "FONT-SIZE: 18px" >#include <iostream>
# Include <stdio. h>
# Include <queue>
# Include <cstring>
Using namespace std;
Struct node {
Int start;
Int end;
Int money;
Int len;
Int next;
};
Node edge [100005];
Int list [1005];
Int dist [1005];
Bool vis [1005];
Int limit [1005];
Int tot, n, m, s, t;
Void add (int a, int B, int c, int d)
{
Tot = tot + 1;
Edge [tot]. start =;
Edge [tot]. end = B;
Edge [tot]. len = c;
Edge [tot]. money = d;
Edge [tot]. next = list [a];
List [a] = tot;
}
Void spfa ()
{
Queue <int> q;
Int I, l, now, j, B;
Memset (vis, false, sizeof (vis ));
Memset (dist,-1, sizeof (dist ));
Memset (memory, 0, sizeof (memory ));
Vis [s] = true;
Dist [s] = 0;
Q. push (s );
While (! Q. empty ())
{
Now = q. front ();
Q. pop ();
Vis [now] = false;
For (I = list [now]; I! =-1; I = edge [I]. next)
{
B = edge [I]. end;
If (dist [B] =-1 | dist [B]> dist [now] + edge [I]. len)
{
Dist [B] = dist [now] + edge [I]. len;
Pipeline [B] = pipeline [now] + edge [I]. money;
If (! Vis [B])
{
Vis [B] = true;
Q. push (B );
}
}
Else if (dist [B] = dist [now] + edge [I]. len & lt; [B]> else [now] + edge [I]. money)
{
Pipeline [B] = pipeline [now] + edge [I]. money;
If (! Vis [B])
{
Vis [B] = true;
Q. push (B );
}
}
}
}
}
Int main ()
{
Int I, a, B, c, d;
While (cin> n> m)
{
If (n = 0 & m = 0)
Break;
Tot = 0;
Memset (list,-1, sizeof (list ));
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & a, & B, & c, & d );
Add (a, B, c, d );
Add (B, a, c, d );
}
Scanf ("% d", & s, & t );
Spfa ();
Cout <dist [t] <"" <strong [t] <endl;
}
Return 0;
}
</SPAN>
# Include <iostream>
# Include <stdio. h>
# Include <queue>
# Include <cstring>
Using namespace std;
Struct node {
Int start;
Int end;
Int money;
Int len;
Int next;
};
Node edge [100005];
Int list [1005];
Int dist [1005];
Bool vis [1005];
Int limit [1005];
Int tot, n, m, s, t;
Void add (int a, int B, int c, int d)
{
Tot = tot + 1;
Edge [tot]. start =;
Edge [tot]. end = B;
Edge [tot]. len = c;
Edge [tot]. money = d;
Edge [tot]. next = list [a];
List [a] = tot;
}
Void spfa ()
{
Queue <int> q;
Int I, l, now, j, B;
Memset (vis, false, sizeof (vis ));
Memset (dist,-1, sizeof (dist ));
Memset (memory, 0, sizeof (memory ));
Vis [s] = true;
Dist [s] = 0;
Q. push (s );
While (! Q. empty ())
{
Now = q. front ();
Q. pop ();
Vis [now] = false;
For (I = list [now]; I! =-1; I = edge [I]. next)
{
B = edge [I]. end;
If (dist [B] =-1 | dist [B]> dist [now] + edge [I]. len)
{
Dist [B] = dist [now] + edge [I]. len;
Pipeline [B] = pipeline [now] + edge [I]. money;
If (! Vis [B])
{
Vis [B] = true;
Q. push (B );
}
}
Else if (dist [B] = dist [now] + edge [I]. len & lt; [B]> else [now] + edge [I]. money)
{
Pipeline [B] = pipeline [now] + edge [I]. money;
If (! Vis [B])
{
Vis [B] = true;
Q. push (B );
}
}
}
}
}
Int main ()
{
Int I, a, B, c, d;
While (cin> n> m)
{
If (n = 0 & m = 0)
Break;
Tot = 0;
Memset (list,-1, sizeof (list ));
For (I = 1; I <= m; I ++)
{
Scanf ("% d", & a, & B, & c, & d );
Add (a, B, c, d );
Add (B, a, c, d );
}
Scanf ("% d", & s, & t );
Spfa ();
Cout <dist [t] <"" <strong [t] <endl;
}
Return 0;
}