Http://poj.org/problem? Id = 3259
Use Bellman-Ford and spfa to determine negative loops...
Spfa
[856kb 125ms]
# Include
Bellman-Ford [736kb 125ms]
# Include
Label: style color for SP strong data Div on
For convenience, the bellman-Ford queue optimization is called spfa, = Capture zmf (zmf. this is an easy-to-use night. zmf kids are always stuck in a dark corner for the e-sports career in the computer room (no doubt they have started again ). Of course, another dark shadow that never comes to the fore is also secretly shown in a backdoor ...... Our beloved Mr. li began to worry about how to catch zmf: In
Mileage of about 160,000 km Ford Mondeo 2.0l sedan. the vehicle can not be started due to engine maintenance into the store.Troubleshooting: After loading the car, when you start the engine, start the machineNissan PIN code readerRunning strong, but when you turn the ignition switch, can not hear the voice of the Main relay, can not hear the sound of the fuel pump operation. carefully check the relevant fuse found in the passenger side of the fuse box
If you use the Dijkstra algorithm, you cannot get to the shortest path1.a->c->d 52.a->b ... NoThe nearest path is 5. But actually the path of the B->c is-2. The minimum cost of a->b->c->d is 3The Dijkstra algorithm is unable to determine the shortest path of a graph with negative weight edges. If the negative power is encountered, when there is no negative power loop (the meaning of the negative right loop is that the weight value of the loop and the negative. Even if there is a negative right s
Bellman Ford Shortest Path algorithm
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>The following table records the distance from S to each node:The first iteration,S->a = 4, because S->a is now INF. So update min (s->a) is 4S->b = 6. Because S->b is now INF. So update min (s->b) is 6S->c=inf (indicates unreachable)S->d=inf
MIN (S->s)
MIN (S->a)
MI
Post a link that will be the shortest path to the problem: http://www.cnblogs.com/Yan-C/p/3916281.htmlBellman-ford algorithm the time complexity of finding a single source shortest path is O (v*e).This algorithm is based on the idea of dynamic programming. This means using the smallest path now to update the minimum distance for other paths.One of the main points is to relax the edge of the operation, in fact, is to update the path bar ~Use the DIS ar
Title Link: http://poj.org/problem?id=1860The main idea: to give you some way to exchange, ask you whether you can make money to profit?Use the Ford algorithm to return Yes when it comes to making money and return no if you can't make moneyShould be able to stop, but I will not analyze the complexity, who will teach me?1#include 2#include 3#include 4#include 5#include 6#include Set>7#include 8#include 9#include Ten#include One#include A#include - u
Input
3
USDollar
britishpound
frenchfranc
3
USDollar 0.5 britishpound
britishpound 10.0 Frenchfranc
Frenchfranc 0.21 USDollar
3
USDollar
britishpound
frenchfranc
6
USDollar 0.5 britishpound
USDollar 4.9 frenchfranc
britishpound 10.0 frenchfranc britishpound
1.99 USDollar
Frenchfranc 0.09 britishpound
frenchfranc 0.19 USDollar
0
Sample Output
Case 1:yes
Case 2:no
Test instructions
It is known that n currencies, as well as the exchange rate and mode of M currencies, ask whether the currency
log the path, then add a pointer to the parent node of the array, each time the relaxation, update the parent node is OK.The actual code is as follows:
void Dijkstra (int start)
{
clr (vis, 0);
for (int i = 1; I
The above code complexity is O (n^2), in the actual application can use the priority queue each time to select No Label D value of the smallest point, to relax, the complexity of O (Mlogn), each time out of the queue, if it has been marked, directly ignore the node is no longe
USDollar3USDollarBritishpoundFrenchfranc6USDollar 0.5 BritishpoundUSDollar 4.9 FrenchfrancBritishpound 10.0 FrenchfrancBritishpound 1.99 USDollarFrenchfranc 0.09 BritishpoundFrenchfranc 0.19 USDollar0Sample OutputCase 1:yesCase 2:noSimilar to poj1860, the right value is a positive loop. It's just a node deal, I used a hash table here.#include #include #include #include #include #include #include #include #include #include #define INF 0x3f3f3f3f#define MAXN 5005#define MOD 1000000007using namesp
Source:#include #include int m (1), n,k,i[1001],x[1001],y[1001],f[1001];int main (){scanf ("%d%d", n,k);for (int a=1;afor (int b=1;b {scanf ("%d", i[m]);if (i[m]!=-1) {X[m]=a;y[m]=b;m++;}}//adjacency matrix input. memset (f,0x7f,sizeof (f));f[k]=0;//Update the start path length to 0. for (int a=2;afor (int b=1;bif (F[x[b]]+i[b]f[y[b]]=f[x[b]]+i[b];//traverse all edge weights to update the shortest path from the starting point to the node. for (int a=1;aprintf ("%d--%d =%d\n", K,a,f[a]);return 0;
The Bellman-Ford algorithm can be used to solve the situation where the required shortest path graph contains a negative edge. The basic idea of the algorithm: if there is a shortest path between two nodes, each node in this path goes through at most once (because if more than once, there is a ring in the path, if it is a positive number ring, the path weight will increase. If it is a negative ring, the shortest path does not exist. If it is a zero ri
This seems to be similar to the nature of the Ek algorithm, but a dfs, a bfs, generally seems to be better with bfs.Program:#include #include#include#includeusing namespacestd;structedge{int from, to,cap,flow,nod;}; Vector20000];ints,t;BOOLvis[20000];voidAddintUintVintW) {g[u].push_back (edge) {u,v,w,0, G[v].size ()}); G[v].push_back (edge) {v,u,0,0, G[u].size ()-1});}intDfsintNowintmaxx) { if(now==t)returnmaxx; vis[now]=true; for(intI=0; I) {edge e=g[now][i]; if((!vis[e.to]) (e.cap>E.flow))
This problem is slightly more complicated, you need to master the processing of string input + limit the time you can walk.
ZOJ1791 (POJ1613)-cave RaiderShortest way to limit travel time//poj1613-zoj1791//time:16msmemory:324k#includeACM/ICPC's Bellman Ford exercises (ZOJ1791 (POJ1613))
http://poj.org/problem?id=2240Basic and POJ 1860 the same just put a single point into any point done 1860 and then do this problem completely put the idea on the way overFinish only to find the online solution is Floyd but the whole idea is very similarBut it seems to be too low in efficiency.#include #include#include#include#include#include#includeusing namespacestd;Charstr[ -][ -];Doubleu[ +],v[ +],w[ +];Doubled[ -];intn,m;intFindChar*s) { for(intI=1; i) { if(strcmp (s,str[i]) = =0
; - if(SPFA (n, mid)) R=mid;//have negative ring A ElseL=mid; + } the returnl; - } $ the intMain () the { the //freopen ("Input.txt", "R", stdin); the intN, M, T, a, B, C, j=0; -Cin>>T; in while(t--) the { thescanf"%d%d",n,m); AboutEdge_cnt=0; the for(intI=0; i) vect[i].clear (); theMemset (Edge,0,sizeof(Edge)); thebig=0; +Small=INF; - the for(intI=0; i)Bayi { thescanf"%d%d%d",a,b,c); the Add_node (a,b,c); -Small=min (small,c);
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.