Algorithm Hints
Minimum ring problem
Main
Topic
On a weighted graph, find at least 3 points with a weighted value and a minimum ring, and press the point on the ring on the sequential output ring. There are heavy edges, no self-loops.
Approach Analysis
Refer to the minimum loop problem, when updating dist[i][j], record the update of its point k to facilitate backtracking paths.
Reference Code
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <queue>5#include <algorithm>6 7 using namespacestd;8 9 #defineMAXN 110Ten #defineINF 0x3f3f3f3f One #defineMAX (A, B) (A>B?A:B) A #defineMIN (A, B) (A<B?A:B) - #defineABS (M) (m<0?-m:m) -typedefLong LongLL; the - intN,m,ans; - intUPDATE[MAXN][MAXN],ROAD[MAXN]; - intDIS[MAXN][MAXN],E[MAXN][MAXN]; + - voidInit () + { AMemset (E,0x3f,sizeof(e)); at inti,u,v,d; - for(i=1; i<=m;++i) { -scanf"%d%d%d",&u,&v,&d); -e[u][v]=min (e[u][v],d); -e[v][u]=E[u][v]; - } in } - to voidGetroad (intIintj) + { - if(! UPDATE[I][J]) road[++road[0]]=i; the Elsegetroad (I,update[i][j]), Getroad (update[i][j],j); * } $ Panax Notoginseng voidFloyd () - { the intI,j,k; ans=INF; +memcpy (Dis,e,sizeof(e)); Amemset (Update,0,sizeof(Update)); the for(k=1; k<=n;++k) { + for(i=1; i<k;++i) - for(j=i+1; j<k;++j) $ if(0ll+dis[i][j]+e[i][k]+e[k][j]<ans) { $ans=dis[i][j]+e[i][k]+E[k][j]; -road[0]=0; Getroad (i,j); -road[++road[0]]=J; theroad[++road[0]]=K; - }Wuyi for(i=1; i<=n;++i) the for(j=1; j<=n;++j) - if(dis[i][k]+dis[k][j]<Dis[i][j]) { Wudis[i][j]=dis[i][k]+Dis[k][j]; -update[i][j]=K; About } $ } - if(Ans==inf) printf ("No solution.\n"); - Else{ - for(i=1; i<road[0];++i) printf ("%d", Road[i]); Aprintf"%d\n", Road[i]); + } the } - $ intMain () the { the while(SCANF ("%d", &n), n!=-1){ thescanf"%d",&m); the Init (); - Floyd (); in } the return 0; the}View Code
Topic links
1999 Central European Olympiad on Informatics-sightseeing trip
1999 Central European Olympiad on Informatics-sightseeing trip