Common algorithm Template Program highlights

Source: Internet
Author: User

    1. and check Set template function

int a[100];

The initial state, each point of the father is himself or 0, i.e. each point is a set.

int initset (int membernum)

{

for (int i=0;i<membernum;i++)

A[i]=i;

/*

for (int i=0;i<=membernum-1;i++)

a[i]=0;

*/

}

int rootfind (int x)

{

if (a[x]==x)

return (x);

Else

A[x]=rootfind (A[x]);

Return (A[x]);

or return (A[x]==x?x:a[x]=rootfind (a[x]))

}

Merges two root nodes, which is the merging of two collections. Find the root node first, call this function,

You can also use a function

void Unionroot (int r1,int R2)

{

A[R2] = R1;

}

2. Shortest path problem

2.1 Dijkstra algorithm

#include <iostream>

#include <cstdio>

#include <vector>

#include <algorithm>

using namespace Std;

struct edge{

int to,w;

};

vector<edge>e[1010];//Subscript starting from 1

int dist[1010];//subscript starting from 1

BOOL used[1010];//subscript starting from 1

const int max=1000000010;

int main ()

{

int i,j,u,v,t,n,m,s;

scanf ("%d%d%d", &n,&m);

for (i=1;i<=m;i++)

{

scanf ("%d%d%d", &u,&v,&t);

E[u].push_back (Edge) {v,t});

E[v].push_back (Edge) {u,t});

The direction Plate: E[u].push_back (Edge) {v,t});

No-map to save two edges

}

for (i=1;i<=n;i++)

Dist[i]=max;

dist[1]=0;//Source Point is 1

int k,min;

for (i=0;i<n-1;i++)//Dijkstra algorithm, which controls the number of cycles, must be guaranteed n-1 cycles

{

Find the shortest path point in the point where the shortest path is not determined, and use it as a transit point for the next step

Min=max;

for (j=1;j<=n;j++)

if (Min>dist[j] && used[j]==0)

{

K=j;

MIN=DIST[J];

}

S=k;

Used[s]=1;

Relaxation

For (J=1;j<e[s].size (); j + +)

Dist[e[s][j].to]=min (DIST[E[S][J].TO],DIST[S]+E[S][J].W);

}

printf ("%d", dist[n]);

}

2.2 Dijkstra Algorithm Optimization

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <algorithm>

#include <vector>

#include <queue>

using namespace Std;

const int n=100010;

struct edge{int to,len;};

Vector <Edge> E[n];

int n,m;

Long Long dist[n];

BOOL Used[n];

struct point{

int no;

Long Long Dist;

friend bool operator > (const point A, const point B) {

Return a.dist!=b.dist? a.dist>b.dist:a.no>b.no;

}

};

Priority_queue <point, Vector<point>, greater<point> > Q;

void Dijk (int st) {

memset (Dist, 0x3f, sizeof (Dist));

DIST[ST]=0LL;

Q.push (point) {ST,0LL});

while (! Q.empty ()) {

int Now=q.top (). No;

Q.pop ();

if (Used[now]) continue;

Used[now]=1;

for (int i=0;i<e[now].size (); i++) {

if (Dist[e[now][i].to]>dist[now]+e[now][i].len) {

Dist[e[now][i].to]=dist[now]+e[now][i].len;

Q.push (point) {e[now][i].to, dist[e[now][i].to]});

}

}

}

}

int main () {

int S;

scanf ("%d%d%d", &n,&m,&s);

for (int i=0;i<m;i++) {

int s,t,d;

scanf ("%d%d%d", &s,&t,&d);

E[s].push_back (Edge) {t,d});

}

Dijk (S);

for (int i=1;i<=n;i++) printf (i<n? ") %lld ":"%lld\n ", Dist[i]);

return 0;

}

2.3

Freud algorithm

#include <iostream>

using namespace Std;

const int inf=99999999;

int N, M, dist[1010][1010];

void Floyd () {

for (int k=1;k<=n;k++)

for (int i=1;i<=n;i++)

for (int j=1;j<=n;j++)

Dist[i][j]=min (Dist[i][j],dist[i][k]+dist[k][j]);

}

int main () {

scanf ("%d%d", &n, &m);

for (int i=1;i<=n;i++)

for (int j=1;j<=n;j++)

Dist[i][j]= (I==j?0:inf);

for (int i=0;i<m;i++) {

int s,t,d;

scanf ("%d%d%d", &s, &t, &d);

Dist[s][t]=min (Dist[s][t], D);

}

Floyd ();

for (int i=1;i<=n;i++)

for (int j=1;j<=n;j++)

printf (j<n? ") %d ":"%d\n ", Dist[i][j]);

return 0;

}

Common algorithm Template Program highlights

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.