nyoj-115-City insurgency (Dijkstra algorithm)

Source: Internet
Author: User

Topic links

1 /*2 name:nyoj-115-City Insurgency3 Copyright:4 Author:5 DATE:2018/4/25 17:28:066 Description:7 Dijkstra Template Questions8 enumeration from the city where the troops are located to the rebel city to take the minimum value9 */Ten#include <iostream> One#include <cstdio> A#include <cstring> -#include <cmath> - using namespacestd; the Const intMAXN =1005, inf=0x3f3f3f3f; - intDIS[MAXN], G[MAXN][MAXN], N, M, P, Q, ARMY[MAXN], SRC; - BOOLV[MAXN]; -  + voidDijkstra () { -     intN =M; +      for(intI=1; i<=n; i++) Dis[i] =INF; ADIS[SRC] =0; atmemset (V,0,sizeof(v)); -      for(intI=1; i<=n; ++i) { -         intMark =-1, mindis=INF; -          for(intj=1; j<=n; J + +) { -             if(!v[j] && dis[j]<Mindis) { -Mindis =Dis[j]; inMark =J; -             } to         } +V[mark] =1; -          for(intj=1; j<=n; J + +) { the             if(!V[j]) { *Dis[j] = min (Dis[j], Dis[mark] +g[mark][j]); $             }Panax Notoginseng         } -     } the } + intMain () A { the     intT; +Cin>>T; -      while(t--) { $Memset (G,0x3f,sizeof(g)); $memset (Army,0,sizeof(Army)); -Cin>>n>>m>>p>>Q; -           for(intI=0; i<n; i++) { theCin>>Army[i]; -          }Wuyi           for(intI=0; i<p; i++) { the              intx, y, cost; -Cin>>x>>y>>Cost ; Wu             if(G[x][y] < cost)Continue; -G[x][y] =Cost ; AboutG[Y][X] =Cost ; $          } -          intMindis =0x3f3f3f3f; -           for(intI=0; i<n; i++) { -memset (DIS,0,sizeof(DIS)); Asrc =Army[i]; + Dijkstra (); theMindis =min (Mindis, dis[q]); -          } $cout<<mindis<<Endl; the     } the     return 0; the}

nyoj-115-City insurgency (Dijkstra algorithm)

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.