The algorithm is not interpreted here, only the code template is written.
Dijkstra
The realization of adjacency matrix
#include <iostream> #include <cstdio> #include <cstring> using namespace std;
const int max=0x3f3f3f3f;
int map[110][110];
int dis[110];
int visit[110];
/* About three arrays: The map array is stored as the point of information, such as map[1][2]=3, 1th points and 2nd points of distance of 3 dis array stored as the starting point and each point of the shortest distance, such as dis[3]=5, indicating the starting point and 3rd point of the shortest distance of 5
The visit array is stored as 0 or 1,1 says it has passed this point.
*/int n,m;
int Dijkstra () {int i,j,pos=1,min,sum=0;
memset (visit,0,sizeof (visit));//initialized to 0, indicating that the start did not go through for (i=1; i<=n; i++) {dis[i]=map[1][i];
} visit[1]=1;
dis[1]=0;
int t=n-1;
while (t--) {Min=max; For (J=1 j<=n; j + +) {if (visit[j]==0&&min>dis[j)) {min=dis[
J];
Pos=j; } visit[pos]=1;//indicates that this point has passed for (J=1 j<=n; j + +) {if (visit[j]==0&&
DIS[J]>MIN+MAP[POS][J])//update the value of dis dis[j]=map[pos][j]+min;
} return Dis[n];
int main () {int i,j; while (cin>>N>>M)//n represents n Dots, m represents the M bar edge {memset (map,max,sizeof (map));
int a,b,c;
For (I=1 i<=m; i++) {cin>>a>>b>>c;
if (C<map[a][b])//Prevent heavy edge map[a][b]=map[b][a]=c;
int Sum=dijkstra ();
cout<<sum<<endl;
return 0; }
Vector adjacency Table implementation
#include <stdio.h> #include <string.h> #include <string> #include <vector> #include <
Algorithm> #define INF 0x3f3f3f3f using namespace std;
struct Node {int end;//endpoint int power;//weight} t; int n;//n for edge number vector<node>q[500001];//adjacency table store graph information int dis[500001];//distance array bool vis[500001];//tag array void Dijkstra (int
Start, int end) {memset (Vis, false, sizeof (VIS));
for (int i=0; i<=n; i++) {dis[i] = INF;
int len=q[start].size (); for (int i=0; i<len; i++) {if (Q[start][i].power < dis[q[start][i].end)) Dis[q[start][i].end ]=q[start][i].power;
Dis array update from starting point vis[start]=true;//start Mark is 1 for (int k=0; k<n-1; k++) {int pos, min=inf;
for (int i=1; i<=n; i++) {if (!vis[i] && dis[i]<min) {
The current node has not been accessed and the weight value is smaller min=dis[i];
Pos=i; }} vis[pos]=true;
Update dis array len=q[pos].size () again; for (int j=0; j<len; J + +) {if (!vis[q[pos][j].end) && dis[q[pos][j].end]>q[pos][j].pow
Er+dis[pos]) Dis[q[pos][j].end] = Q[pos][j].power + Dis[pos];
} printf ("%d\n", Dis[end]);
int main () {int m; while (scanf ("%d", &n, &m) &&n&&m)//input point and Edge {for (int i=0; i<=n; i++) q[
I].clear ();//Empty vector array for (int i=0; i<m; i++) {int begin,end; scanf ("%d%d%d", &begin, &end, &power);//input/*t as a node-type temporary variable, in order to facilitate indentation, the following code is the input edge of the non-map * * * t.en
D=end;
T.power=power;
Q[begin].push_back (t);
T.end=begin;
T.power=power;
Q[end].push_back (t);
}//dijkstra (1, N); int start, end;//itself to determine the starting point and end point scanf ("%d%d", &start, &end);//Enter start and end point Dijkstra (sTart, end);
return 0;
}
Floyd
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int max=0x3f3f3f3f;
int map[1100][1100];
void Floyd (int n)
{for
(int k=1; k<=n; k++) for (
int i=1; i<=n; i++) for
(int j=1; j<=n; j + +)
map[i][j]=min (Map[i][j],map[i][k]+map[k][j]); This uses the Min function, which can be a little more time-consuming.
}
int main ()
{
int i,j,n,m; Reads N and m,n to represent the number of vertices, and m represents the number of bars on the edge
cin>>n>>m;
memset (map,max,sizeof (map)); Initializes the for
(I=1 i<=n; i++) for
(j=1; j<=n; j + +)
if (i==j)
map[i][j]=0;
int a,b,c;
For (I=1 i<=m; i++)
{
cin>>a>>b>>c;
map[a][b]=c;//This is a forward graph
}
Floyd (n);
For (I=1 i<=n; i++)
{for
(j=1; j<=n; j + +)
{
cout<<map[i][j]<< "";
}
cout<<endl;
}
return 0;
}
Bellman-ford
#include <iostream> #include <cstdio> #include <cstring> using namespace std;
const int max=0x3f3f3f3f;
const int n=1010; int Nodenum, edgenum, source;
Point, Edge, beginning typedef struct EDGE {int st;
int Ed;
int power;
} Edge;
Edge Edge[n];
int dis[n];
void Inite ()//initialization diagram {cin>>nodenum>>edgenum>>source;
memset (dis,max,sizeof (dis));
dis[source]=0;
for (int i=1; i<=edgenum; i++) {cin>>edge[i].st>>edge[i].ed>>edge[i].power;
if (Edge[i].st = = source)//Note Set the initial situation here Dis[edge[i].ed] = Edge[i].power; } void Relax (int st,int ed,int Power)//slack calculation {if (dis[ed]>dis[st]+power) dis[ed]=dis[st]+power;} bool B Ellman_ford () {for (int i = 1; I <= nodenum-1, i++) for (int j = 1; J <= Edgenum; j +) RelA
X (Edge[j].st,edge[j].ed,edge[j].power); for (int i = 1; I <= edgenum ++i) {if (Dis[edge[i].ed] > Dis[edge[i].st] + edgE[i].power) {return 0;
} return 1;
int main () {inite (); if (Bellman_ford ())//If there is no negative right {for (int i = 1; I <= nodenum; ++i)///per point shortest circuit {cout<<d
is[i]<< "";
} cout<<endl;
return 0;
}
Spfa
POJ 2387
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string
> #include <cmath> #include <iomanip> #include <map> #include <stack> #include <vector> #include <queue> #include <set> #include <utility> #include <algorithm> #define MAX (a,b) (a>b A:B) #define MIN (a,b) (A<B?A:B) #define SWAP (A,B) (a=a+b,b=a-b,a=a-b)//#define MEMSET (a) memset (A,0,sizeof (a)) #d
Efine X (sqrt (5) +1)/2.0//wythoff #define Pi acos ( -1) #define E 2.718281828459045 using namespace std;
typedef long long int LL;
typedef pair<int,int>pa;
const int MAXL (1E3);
const int INF (0X3F3F3F3F);
const int mod (1E9+7);
int dir[4][2]= {{ -1,0},{1,0},{0,1},{0,-1}};
vector<pa>v[maxl+50];
int VIS[MAXL+50];
int DIS[MAXL+50];
int SPFA (int s,int n) {queue<int>q;
memset (dis,inf,sizeof (dis));
memset (Vis) (vis,0,sizeof);
dis[s]=0;
Vis[s]=1;
Q.push (s);
while (!q.empty ()) {int U=q.front ();
Q.pop ();
vis[u]=0;
for (int i=0;i<v[u].size (); i++) {int x=v[u][i].first,y=v[u][i].second;
if (Dis[u]+y<dis[x]) {dis[x]=dis[u]+y;
if (!vis[x]) {q.push (x);
Vis[x]=1;
}}} return dis[n];
int main () {int t,n;
cin>>t>>n;
for (int i=1;i<=t;i++) {int x,y,z;
cin>>x>>y>>z;
PA p;
P=make_pair (Y,Z);
V[x].push_back (P);
P=make_pair (X,Z);
V[y].push_back (P);
int ANS=SPFA (1,N);
cout<<ans<<endl; }