Zadd grade2 200 item3

Source: Internet
Author: User

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;
}

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.