Title Description
Title, given a direction graph, output the shortest path length from a point to all points.
Input/output format
Input format:
The first line contains three integers n, M, S, respectively, the number of points, the number of forward edges, the number of starting point.
The next M-line contains three integers of fi, Gi, Wi, respectively, indicating the starting point, target point and length of the forward edge of article I.
Output format:
A row that contains n spaces separated by integers, where the I integer indicates the shortest path length from point s to points I (if s=i, the shortest path length is 0, and if from point S cannot reach point I, the shortest path length is 2147483647)
Input and Output Sample input example # #:
4 6 11 2 22 3 22 4 11 3 53 4 31 4 4
Sample # # of output:
0 2 4 3
Description
Time limit: 1000ms,128m
Data size:
For 20% data: n<=5,m<=15
For 40% data: n<=100,m<=10000
For 70% data: n<=1000,m<=100000
For 100% data: n<=10000,m<=500000
Sample Description:
60 min SPFA Code archive:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 #defineMAXN 21474836476 #defineN 100107 using namespacestd;8 structnode{9 intu,v,w;Ten intNext; One}e[500010]; A intN,m,s,head[n],dis[n],ei; - BOOLExist[n]; - voidAddintUintVintW) { thee[++ei].u=u;e[ei].v=v;e[ei].w=W; -e[ei].next=head[u];head[u]=ei; - } -queue<int>Q; + intMain () - { +scanf"%d%d%d",&n,&m,&s); Amemset (DIS,0x3f3f3f3f,sizeof(DIS)); atmemset (exist,false,sizeof(exist)); - for(intI=1, x,y,z;i<=m;i++){ -scanf"%d%d%d",&x,&y,&z); - Add (x, y, z); - } -exist[s]=true;d is[s]=0; Q.push (s); in while(!Q.empty ()) { - intP=q.front (); Q.pop (); exist[p]=false; to for(intI=head[p];i;i=E[i].next) { + intv=e[i].v; - if(dis[v]>dis[p]+E[I].W) { thedis[v]=dis[p]+E[I].W; * if(!Exist[v]) { $ Q.push (v);Panax Notoginsengexist[v]=true; - } the } + } A } the for(intI=1; i<=n;i++){ + if(dis[i]==0x3f) printf ("%d", MAXN); - Elseprintf"%d", Dis[i]); $ } $ return 0; -}
The head array is a little bigger, it's 90.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 #defineMAXN 21474836476 #defineN 100107 using namespacestd;8 structnode{9 intu,v,w;Ten intNext; One}e[500010]; A intn,m,s,head[500010],ei; - Long LongDis[n]; - BOOLExist[n]; the voidAddintUintVintW) { -e[++ei].u=u;e[ei].v=v;e[ei].w=W; -e[ei].next=head[u];head[u]=ei; - } +queue<int>Q; - intMain () + { Ascanf"%d%d%d",&n,&m,&s); atmemset (DIS,0x3f3f3f3f,sizeof(DIS)); -memset (exist,false,sizeof(exist)); - for(intI=1, x,y,z;i<=m;i++){ -scanf"%d%d%d",&x,&y,&z); - Add (x, y, z); - } inexist[s]=true;d is[s]=0; Q.push (s); - while(!Q.empty ()) { to intP=q.front (); Q.pop (); exist[p]=false; + for(intI=head[p];i;i=E[i].next) { - intv=e[i].v; the if(dis[v]>dis[p]+E[I].W) { *dis[v]=dis[p]+E[I].W; $ if(!Exist[v]) {Panax Notoginseng Q.push (v); -exist[v]=true; the } + } A } the } + for(intI=1; i<=n;i++){ - if(dis[i]==0x3f3f3f3f) printf ("%lld", MAXN); $ Elseprintf"%lld", Dis[i]); $ } - return 0; -}
Rokua P 3371 Unit shortest circuit