The shortest circuit template DIJKSTRA+FLOYD+SPFA

Source: Internet
Author: User

Dijkstra and Floyd should pay attention to the heavy side.

Dijkstra

#define INF 0X3FFFFFFF
#define M-

int dist[m], map[m][m], N;
BOOL Mark[m];

void init ()
{
	int i, J;
	for (i = 1; I <= n; i++)    //i==j can also be initialized to 0, only sometimes unsuitable for
		(j = 1; J <= N; j +)
			map[i][j] = inf;
}

void Dijk (int u)
{
	int i, j, mins, V;
	for (i = 1; I <= n; i++)
	{
		Dist[i] = map[u][i];
		Mark[i] = false;
	}
	Mark[u] = true;
	Dist[u] = 0;    Since the map above is not 0 when i==j, this is the while
	(1)
	{
		mins = inf;
		for (j = 1; J <= N; j +)
			if (!mark[j] && dist[j] < mins)
				mins = Dist[j], v = j;
		if (mins = = inf) break
			;
		MARK[V] = true;
		for (j = 1; J <= N; j +)
			if (!mark[j] && dist[v] + map[v][j] < Dist[j])
				dist[j] = Dist[v] + map[v] [j];
	}

Floyd

#define INF 0X3FFFFFFF	//note, too assembly overflow
#define M				//MAX points
int n, dist[m][m];           N: Actual points

void init ()			//Sometimes need to initialize
{
    int i, J;
    for (i = 1; I <= n; i++) for
        (j = i + 1; j <= N; j +)
            dist[i][j] = dist[j][i] = inf;
}

void Floyd ()
{
    int i, j, K;
    for (k = 1; k <= N; k++)
        for (i = 1; I <= n; i++) for
            (j = 1; J <= N; j +)    //Some of the problems will overflow.
                I F (Dist[i][k] + dist[k][j] < Dist[i][j])
	                    dist[i][j] = Dist[i][k] + dist[k][j];

Spfa
#define INF 0x3fffffff
#define M    //Max points
struct son{
    int v, W;
Vector<son> G[m];
BOOL Inq[m];       Into the queue Mark
int dist[m], n;    N: Actual points

void init ()
{for
	(int i = 1; I <= n; i++)
		g[i].clear ();
}

void Spfa (int u)
{
    int i, V, W;
    for (i = 1; I <= n; i++)
    {
        dist[i] = inf;
        Inq[i] = false;
    }
    queue<int> Q;
    Q.push (u);
    Inq[u] = true;
    Dist[u] = 0;
    while (!q.empty ())
    {
        u = q.front ();
        Q.pop ();
        Inq[u] = false;
        for (i = 0; i < g[u].size (); i++)
        {
            v = g[u][i].v;
            w = G[U][I].W;
            if (Dist[u] + W < Dist[v])
            {
                Dist[v] = Dist[u] + W;
                if (!inq[v])
                {
                    Q.push (v);
                    INQ[V] = true;}}}


SPFA optimization

#define INF 0X3FFFFFFF #define M 1005//MAX points struct edge{int V, W, next;}		E[10005];
Estimate how many edges int pre[m], CNT, dist[m], N;
BOOL Inq[m];
    Note Initialize void init () {cnt = 0;
memset (Pre,-1, sizeof (pre));
    }//Note bi-directional plus-side void Addedge (int u, int v, int w)//Add-edge function, slowly simulation will understand {e[cnt].v = V;
    E[CNT].W = W;		E[cnt].next = Pre[u];				Replace existing side pre[u] = cnt++;
    Self-inserted into the U-derived first side} void spfa (int u) {int V, W, I;
    for (i = 1; I <= n; i++)//for numbering from 1 to n dist[i] = inf, inq[i] = false;
    Dist[u] = 0;
    Queue<int> Q;
    Q.push (U);
    Inq[u] = true;
        while (!q.empty ()) {u = Q.front ();
        Q.pop ();
        Inq[u] = false;
            for (i = pre[u]; I!=-1; i = e[i].next) {w = E[I].W;
            v = e[i].v;
                if (Dist[u] + W < Dist[v]) {Dist[v] = Dist[u] + W;
                    if (!inq[v]) {Q.push (v); INQ[V] = trUe
 }
            }
        }
    }
}


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.