Original title: http://poj.org/problem?id=1511
Main topic:
One-way diagram, need from point 1 to each point to go once, go back immediately, then go to the next point, to find a round-trip path and.
If there are only 100 points, run once Floyd can, here are 10w points, no.
The simple Dijkstra is the complexity of n^2, which is timed out here.
So here we use the SPFA approach to the 2N algorithm.
Because the two-dimensional array space is not enough, so can only use a vector or adjacency table, because the vector is suitable for frequent modification, itself is a linked list structure, each time the insertion of data will consume time to request memory and contact with the front, although can support subscript access, but the efficiency will not catch the array of subscript access, So sometimes we use vectors to time out, and here we use adjacency tables to save.
As we ask for the distance from the origin to each point, SPFA we can only get one-way distance and return the way we want it?
First of all, let's say that we are drawing on paper, as an example:
We start with a, run SPFA can find the distance from a to each point.
Now the B to a road is only one, is b->c->d->a, if we look at the order in turn, we can find that A->d->c->b is his circuit. If we turn the arrows of the whole map in reverse and ask for a SPFA again, we can get the final result.
The reference code is as follows:
#include "stdio.h"#include "iostream"#include "string.h"#include "queue"using namespace STD;typedef Long Long intLintConstLint INF =0x3f3f3f3f3f3f3f3f;Const intn=1000005;structnode{intEnintLenintNext;};intN,m;BOOLVis[n];lint Dis[n];intHead1[n];intHead2[n];node Maze1[n];node Maze2[n];voidSPFA1 () {memset(Vis,false,sizeof(VIS)); for(intI=0; i<=n; i++) Dis[i]=inf; Queue <int>Q dis[1]=0; vis[1]=true; Q.push (1); while(!q.empty ()) {intX=q.front (); Q.pop (); vis[x]=false; for(inti=head1[x];i!=-1; i=maze1[i].next) {intEn=maze1[i].en;if(Dis[en]>dis[x]+maze1[i].len) {Dis[en]=dis[x]+maze1[i].len;if(vis[en]==false) {vis[en]=true; Q.push (en); } } } }}voidSPFA2 () {memset(Vis,false,sizeof(VIS)); for(intI=0; i<=n; i++) Dis[i]=inf; Queue <int>Q dis[1]=0; vis[1]=true; Q.push (1); while(!q.empty ()) {intX=q.front (); Q.pop (); vis[x]=false; for(inti=head2[x];i!=-1; i=maze2[i].next) {intEn=maze2[i].en;if(Dis[en]>dis[x]+maze2[i].len) {Dis[en]=dis[x]+maze2[i].len;if(vis[en]==false) {vis[en]=true; Q.push (en); } } } }}intMain () {intTscanf("%d", &t); while(t--) {memset(head1,-1,sizeof(HEAD1));memset(head2,-1,sizeof(head2));scanf("%d%d", &n,&m); for(intI=1; i<=m; i++) {intX, Y, Zscanf(" %d%d%d", &x,&y,&z); Maze1[i].len=z; Maze1[i].en=y; MAZE1[I].NEXT=HEAD1[X]; Head1[x]=i; Maze2[i].len=z; Maze2[i].en=x; Maze2[i].next=head2[y]; Head2[y]=i; } Lint ans=0; SPFA1 (); for(intI=1; i<=n; i++) ans=ans+dis[i]; SPFA2 (); for(intI=1; i<=n; i++) ans=ans+dis[i];printf("%i64d\n", ans); }return 0;}
The following is the syntax of the vector, although the data will time out, you can also refer to:
#include "stdio.h"#include "iostream"#include "string.h"#include "queue"using namespace STD;typedef Long Long intLintConstLint INF =0x3f3f3f3f3f3f3f3f;Const intn=1000005;structnode{intEnintLenintNext;};intN,m;BOOLVis[n];lint Dis[n];intHead1[n];intHead2[n];node Maze1[n];node Maze2[n];voidSPFA1 () {memset(Vis,false,sizeof(VIS)); for(intI=0; i<=n; i++) Dis[i]=inf; Queue <int>Q dis[1]=0; vis[1]=true; Q.push (1); while(!q.empty ()) {intX=q.front (); Q.pop (); vis[x]=false; for(inti=head1[x];i!=-1; i=maze1[i].next) {intEn=maze1[i].en;if(Dis[en]>dis[x]+maze1[i].len) {Dis[en]=dis[x]+maze1[i].len;if(vis[en]==false) {vis[en]=true; Q.push (en); } } } }}voidSPFA2 () {memset(Vis,false,sizeof(VIS)); for(intI=0; i<=n; i++) Dis[i]=inf; Queue <int>Q dis[1]=0; vis[1]=true; Q.push (1); while(!q.empty ()) {intX=q.front (); Q.pop (); vis[x]=false; for(inti=head2[x];i!=-1; i=maze2[i].next) {intEn=maze2[i].en;if(Dis[en]>dis[x]+maze2[i].len) {Dis[en]=dis[x]+maze2[i].len;if(vis[en]==false) {vis[en]=true; Q.push (en); } } } }}intMain () {intTscanf("%d", &t); while(t--) {memset(head1,-1,sizeof(HEAD1));memset(head2,-1,sizeof(head2));scanf("%d%d", &n,&m); for(intI=1; i<=m; i++) {intX, Y, Zscanf(" %d%d%d", &x,&y,&z); Maze1[i].len=z; Maze1[i].en=y; MAZE1[I].NEXT=HEAD1[X]; Head1[x]=i; Maze2[i].len=z; Maze2[i].en=x; Maze2[i].next=head2[y]; Head2[y]=i; } Lint ans=0; SPFA1 (); for(intI=1; i<=n; i++) ans=ans+dis[i]; SPFA2 (); for(intI=1; i<=n; i++) ans=ans+dis[i];printf("%i64d\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1511 Invitation Cards adjacency table SPFA algorithm