Blue Bridge cup algorithm training shortest path [Bellman]

Source: Internet
Author: User

Transmission Door

Algorithm training Shortest time limit: 1.0s memory limit: 256.0MB 1 brocade sac 2 Jin sac 3 problem description

Given an n vertex, the forward graph of the M-Edge (some of which may be negative, but no negative ring is guaranteed). Please calculate the shortest path from point 1th to other points (vertices are numbered from 1 to n).

Input format

First line two integers n, M.

The next M-line, each line has three integers u, V, L, indicating that u to V has an edge of length L.

The output format is a total of n-1 lines, and line I represents the shortest path from point 1th to I+1. Sample Input 3 3 1 2-1 2 3-1 3 1 2 Sample output-1-2 data size and conventions

For 10% of data, n = 2,m = 2.

For 30% of data, n <= 5,m <= 10.

For 100% of data, 1 <= n <= 20000,1 <= m <= 200000,-10000 <= L <= 10000, guaranteeing that all vertices can be reached from any vertex.

Exercises

Because of the ring, there is negative power, so with Bellman-ford

Because the data is a bit large, it is optimized with the queue

506328 [Email protected] Shortest circuit 04-07 17:24 1.055KB C++ That's right 100 171ms 4.003MB Evaluation details

1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <queue>5#include <vector>6 7 using namespacestd;8 9 Const intN =20005;Ten Const intINF =0x3f3f3f3f; One  Atypedefstruct - { -     intto ; the     intdis; - }pp; -  - intn,m; +Vector<pp>Bian[n]; - intV[n]; + intVis[n]; A  at voidBellman () - { -queue<int>que; -     intte,nt; -Que.push (1); -vis[1]=1; in     inti; -     intdis; to      while(Que.size () >=1) +     { -Te=Que.front (); the Que.pop (); *          for(i=0; I<bian[te].size (); i++){ $Nt=bian[te][i].to;Panax Notoginsengdis=Bian[te][i].dis; -             if(v[nt]>v[te]+dis) { thev[nt]=v[te]+dis; +                 if(vis[nt]==0){ Avis[nt]=1; the Que.push (NT); +                 } -             } $         } $vis[te]=0; -     } - } the  - intMain ()Wuyi { the     inti,j; -     intuu,vv,l; Wu PP te; -     //freopen ("data.in", "R", stdin); About     //while (scanf ("%d%d", &n,&m)!=eof) $scanf"%d%d",&n,&m); -     { -memset (Vis,0,sizeof(Vis)); -Fill (v,v+n+1, INF); Av[1]=0; +          for(i=0; i<=n;i++){ the bian[i].clear (); -             //printf ("i=%d v=%d\n", I,v[i]); $         } the          for(i=1; i<=m;i++){ thescanf"%d%d%d",&uu,&vv,&l); thete.to=vv;te.dis=l; the bian[uu].push_back (TE); -         } in Bellman (); the          for(i=2; i<=n;i++){ theprintf"%d\n", V[i]); About         } the     } the     return 0; the}

Blue Bridge cup algorithm training shortest path [Bellman]

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.