[SinGuLaRiTy] the shortest-circuit computing code base, singularity code base

Source: Internet
Author: User

[SinGuLaRiTy] the shortest-circuit computing code base, singularity code base

[SinGuLaRiTy-1002]Copyright (c) SinGuLaRiTy 2017. All Rights Reserved.

Dijkstra:

Description

 

Single-Source Vertex Shortest Path of Directed Graph (Dijkstra algorithm)

 

Input

 

Row 1st: an integer n (2 <= n <= 500) and m (10 <= m <= 20000) separated by two spaces ), the number of vertices and edges of the graph.

2nd. m + 1 rows: three separate spaces in each row are integers I, j, and w. I indicates the start point of an edge, j indicates the end point, and w indicates the weight value.

Row m + 2: 2 integers s, t (1 <= s, t <= n), indicating the specified vertex.

 

Output

 

Row 3: Minimum Distance

Row 2nd: Shortest Path (the sequence from the start point to the end point, separated by one space)

 

 

Sample Input Sample output

5 7

1 2 10

1 4 30

1 5 100

2 3 50

3 5 10

4 3 20

4 5 60

1 5

60

1 4 3 5

 

 

 

 

 

 

 

 

 

Code:

#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>using namespace std;int n,m;int G[501][501],dis[501],L[501];bool used[501];void Dijkstra(int s){    for(int i=1;i<=n;i++)    {        used[s]=1;        for(int j=1;j<=n;j++)        {            if(G[s][j]==0||s==j)                continue;            if(dis[j]>dis[s]+G[s][j])                dis[j]=dis[s]+G[s][j],L[j]=s;        }        int themin=0x3f3f3f3f;        for(int i=1;i<=n;i++)                            if(used[i]==0&&dis[i]<themin)                s=i,themin=dis[i];    }}int main(){    int a,b,c,S,E,cnt=1;    scanf("%d%d",&n,&m);    for(int i=1;i<=m;i++)    {        scanf("%d%d%d",&a,&b,&c);        G[a][b]=c;    }    scanf("%d%d",&S,&E);    int e=E;    dis[S]=0;    for(int i=1;i<=n;i++)        if(i!=S) dis[i]=0x3f3f3f3f;                    Dijkstra(S);          printf("%d\n",dis[E]);    while(L[E]!=0)    {        dis[cnt++]=L[E];        E=L[E];    }    for(int i=cnt-1;i>=1;i--)        printf("%d ",dis[i]);    printf("%d",e);    return 0;}

 

SPAF:

Description

The shortest path of a single-source vertex in a directed graph. The Source Vertex number is 1, and the end vertex number is n.

Input

Row 1st: an integer n (2 <= n <= 5000) and m (10 <= m <= 500000) separated by two spaces ), the number of vertices and edges of the graph.

2nd. m + 1 rows: three separate spaces in each row are integers I, j, and w. I indicates the start point of an edge, j indicates the end point, and w indicates the weight value.

Output

Row 1st: 1 integer, indicating the minimum distance

Example input 1 Sample output 1
4 71 2 681 3 191 4 662 3 233 4 653 2 574 1 68 66

 

 

 

 

 

Sample Input

Sample output
3 31 2-72 3 43 1 2 No Solution

 

 

 

 

Code:

#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;struct node{    int v,w,next;}edge[500050];int cnt,head[5050],n,m,dis[5050],tot[5050];bool inq[5050];void addedge(int u,int v,int w){    edge[cnt].v=v;    edge[cnt].w=w;    edge[cnt].next=head[u];    head[u]=cnt++;}bool SPFA(){    memset(dis,0x3f,sizeof dis);    dis[1]=0;    inq[1]=1;    deque<int>q;    q.push_front(1);    while(!q.empty())    {        int u=q.front();        q.pop_front();        inq[u]=0;        for(int i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].v,w=edge[i].w;            if(dis[v]>dis[u]+w)            {                dis[v]=dis[u]+w;                if(!inq[v])                {                    if(dis[v]<dis[u])q.push_front(v);                    else q.push_back(v);                    inq[v]=1;                    if(++tot[v]>n)return 0;                }            }        }    }    return 1;}int main(){    int u,v,w;    scanf("%d%d",&n,&m);    memset(head,-1,sizeof head);    for(int i=1;i<=m;++i)    {        scanf("%d%d%d",&u,&v,&w);        addedge(u,v,w);    }    SPFA()?printf("%d\n",dis[n]):    puts("No Solution");}

 

Floyd:

Code:

#include<cstdio>  #include<cstring>  #define maxn 100  #define INF -1  int map[maxn][maxn];  int n, m, path[maxn][maxn];    void Floyd(int n)  {      int i, j, k;      for(k = 0; k < n; ++k)          for(i = 0; i < n; ++i)              for(j = 0; j < n; ++j)                  if(map[i][k] != INF && map[k][j] != INF && (map[i][k] + map[k][j] < map[i][j] || map[i][j] == INF)){                      map[i][j] = map[i][k] + map[k][j];                      path[i][j] = k;                  }             }    void getPath(int v, int u)  {      int k = path[v][u];      if(k == INF){          printf("%d===>", v);                  return;      }      getPath(v, k);      getPath(k, u);  }    int main()  {       scanf("%d%d", &n, &m);      memset(map, INF, sizeof(map));      memset(path, INF, sizeof(path));      int i, j, a, b, c;      for(i = 0; i < n; ++i) map[i][i] = 0;      for(i = 0; i < m; ++i){          scanf("%d%d%d", &a, &b, &c);          map[a][b] = c;      }      Floyd(n);      for(i = 0; i < n; ++i)          for(j = 0; j < n; ++j)              if(map[i][j] != INF){                  printf("%d->%d:%d\n the path is:", i, j, map[i][j]);                  getPath(i, j);                  printf("%d\n", j);              }      return 0;  }  

 

BellmanFord:

Description

The single-Source Vertex shortest path with negative weight (BellmanFord algorithm) of a directed graph. If there are multiple shortest paths, the output path goes through a smaller number of edges. If the shortest path has the same number of edges, output a sequence with a smaller number.

Input

Row 1st: an integer n (2 <= n <= 500) and m (10 <= m <= 20000) separated by two spaces ), the number of vertices and edges of the graph.

2nd. m + 1 rows: three separate spaces in each row are integers I, j, and w. I indicates the start point of an edge, j indicates the end point, and w indicates the weight value.

Row m + 2: 2 integers s, t (1 <= s, t <= n), indicating the specified vertex.

Output

Row 3: Minimum Distance

Row 2nd: Shortest Path (the sequence from the start point to the end point, separated by one space)

If a negative weight loop appears, the output is No Solution.

Sample Input Sample output
6 7
1 2 2
1 3-1
2 4-3
3 4 3
3 6 7
4 6-2
3 5 6
1 6
-3
1 2 4 6

 

 

 

 

 

 

Code:

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int f[20005][3],dis[505],s[505]; void print(int x){    if(s[x]==0) return;    print(s[x]);    printf(" %d",x);}int main() {    int n,m,i,j,q,z;     scanf("%d%d",&n,&m);     for(i=1;i<=m;i++)        scanf("%d%d%d",&f[i][0],&f[i][1],&f[i][2]);     scanf("%d%d",&q,&z);    memset(dis,1,sizeof(dis));     dis[q]=0;    for(i=1;i<=n;i++){        for(j=1;j<=m;j++){            if(dis[f[j][0]]+f[j][2]<dis[f[j][1]]&&i<n){                dis[f[j][1]]=dis[f[j][0]]+f[j][2];                s[f[j][1]]=f[j][0];            }            if(i==n&&dis[f[j][0]]+f[j][2]<dis[f[j][1]]){                printf("No Solution");                return 0;            }        }    }    printf("%d\n",dis[z]);     printf("%d",q);     print(z);}

 

Time:

Related Article

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.