HDU 1874 Spfa/bellmanford/dijkstra/floyd

Source: Internet
Author: User

This problem as a template problem, the solution is many ...

Recently around the people are on the map, I feel good spicy chicken, will only follow the follow-up study.

Only SPFA for the moment.

SPFA (adjacency table version.) Can also be written as a pro-matrix map, but the topic may give parallel edges, so pay attention to find the smallest side of the storage, but also to determine whether a point more than once into the queue) honestly think that SPFA seems to be just a queue optimized search the same.

1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <utility>6#include <vector>7#include <queue>8 #defineMAXX 100109 using namespacestd;Ten  One Const intN = -; A Const intINF =0x3f3f3f3f; -  -  thetypedefstructSion - { -     intdis; -     intNXP; + }sion; -  + intDic[n]; A BOOLVis[n]; atVector<sion>Li[n]; -  -  - intSPFA (int&s,int&t) - { -queue<int>Q; in Q.push (s); -Vis[s] =1; toDic[s] =0; +      while(!q.empty ()) -     { the         intPre =Q.front (); * Q.pop (); $          for(inti =0; I < li[pre].size (); i++)Panax Notoginseng         { -            //cout << Dic[li[pre][i].nxp] << "~"; the             if(Dic[li[pre][i].nxp] > Dic[pre] +Li[pre][i].dis) +             { ADIC[LI[PRE][I].NXP] = Dic[pre] +Li[pre][i].dis; the Q.push (LI[PRE][I].NXP); +                    //cout << DIC[LI[PRE][I].NXP] << "!"; -             } $         } $  -     } -     returnDic[t]; the } - Wuyi voidInit () the { -memset (Vis,false,sizeof(Vis)); Wumemset (Dic,inf,sizeof(DIC)); -      for(inti =0; i < N; i++) About li[i].clear (); $     return ; - } -  - intMain () A { +     intN, M; the     ints, t; -      while(Cin >> N >>m) $     { the init (); the sion A; the         intx; the          for(inti =0; I < m; i++) -         { inscanf"%d%d%d", &x, &AMP;A.NXP, &A.dis); the Li[x].push_back (a); the             inttemp =A.nxp; AboutA.NXP =x; the Li[temp].push_back (a); the         } thescanf"%d%d", &s, &t); +         inty =SPFA (S, t); -         if(Y >=INF) the         {Bayiprintf"-1\n"); the         } the         Elseprintf"%d\n", y); -     } -}

HDU 1874 Spfa/bellmanford/dijkstra/floyd

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.