Dijkstra algorithm-relaxes the edges to find the shortest distance from a point to any point

Source: Internet
Author: User
Tags min

Floyd-w is the shortest distance from any point to any point, because of the high complexity, so in order to only a point to any point of time to easily time out, the Dijkstra algorithm is to solve a point to any point of the distance problem, can be used for the direction of the graph or the graph, only need to pay attention to the initialization of the good, In the same way that the FLOYD-W algorithm uses the critical matrix to store the graph, we also need an array to store the origin (1) distance from any point


Then, in turn, the smallest number of points in the dis begins to find the node and then begins to slack.



The dynamic graph is


For the following figure there is no edge, so the code i<n instead of I<=n


But Dijkstra does not solve graphs with negative weights, such as the following data

Graph without direction

1 2 2

1 3 3

3 2-2


Code

<span style= "FONT-SIZE:14PX;"
> #include <iostream> #include <cstring> using namespace std;
int map[1000][1000];
int visit[1000];
int dis[1000];
int inf=99999999;
    int main () {int n,m,a,b,c,i,j,k,u=0;
    int min;
    cin>>n>>m;
                Initialization of the map for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (i==j) {
            map[i][j]=0;
        } else Map[i][j]=inf;
        }} for (i=1;i<=m;i++) {cin>>a>>b>>c;
    Map[a][b]=c;
    }//control is the shortest distance from the first vertex to any vertex, the following is 1, put the point to all points of the value into the DIS array for (i=1;i<=n;i++) {dis[i]=map[1][i];
    } memset (Visit,0,sizeof (visit));
    Visit[1]=1;
        Make the shortest distance selection for (i=1;i<n;i++)//This piece to note {min=inf;
            for (j=1;j<=n;j++)//Select a point closest to the origin in DIS and record his coordinates {if (visit[j]==0 && dis[j]<min)
                {MIN=DIS[J];
 U=j;           }} visit[u]=1; for (k=1;k<=n;k++)//Then find out if there are other points connected to the point {if (Map[u][k]<inf)//If there is a swap {if
                (Dis[k]>dis[u]+map[u][k])
                {Dis[k]=dis[u]+map[u][k]; }}}} for (i=1;i<=n;i++)//Output from Origin to any point of the shortest distance {cout<<dis[i]<<
    ' '; }}//6 9//1 2 1//1 3//2 3 9//2 4 3//3 5 5//4 3 4//4 5//4 6//5 6 4</span>

About the path output of the DIJ algorithm, because each time its slack edge is not an optional edge, but the selection of the origin of the point to the nearest point of the path of the edge, so each slack edge of the starting point, it must be the shortest path to pass the point, with a pre array to store these numbers, At the time of storage, the last number in the pre is not equal to its phase.

<span style= "FONT-SIZE:14PX;"
> #include <iostream> using namespace std;
int map[1000][1000];
int dis[1000];
int visit[1000];
int inf=99999999;
int pre[1000];
    int main () {int i,j,k=0;;
    int n,m,t1,t2,t3,min,u = 0;
    cin>>n>>m;
            for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (i==j) map[i][j]=0;
        else Map[i][j]=inf;
        }} for (i=1;i<=m;i++) {cin>>t1>>t2>>t3;
    MAP[T1][T2]=T3;
    } for (i=1;i<=n;i++) {dis[i]=map[1][i];
    } dis[1]=0;
    memset (visit,0,sizeof (visit));
    Visit[1]=1;
        pre[k++]=1;//Note that the first digit for (i=1;i<n;i++) {Min=inf is saved; for (j=1;j<=n;j++) {if (visit[j]==0 && min>dis[j]) {min=dis[
                J];
            U=j;
        }} visit[u]=1; for (j=1;j<=n;j++) {if(map[u][j]< INF)
                {if (Dis[j]>dis[u]+map[u][j]) {dis[j]=dis[u]+map[u][j];
            if (pre[k-1]!=u) pre[k++]=u; }}}} pre[k]=6;//note the last digit for (i=0;i<=k;i++) cout<<pre[
i]<< "; }</span>

Multiple Shortest line problems

For the common DIJ algorithm, it outputs the shortest path process, only applicable to a single shortest path, if the other conditions such as the same path to select the time to spend the least cost of the condition of a binding, it can not output the correct answer, and its output is the DIS array of each round selected the smallest, such as A-2-c-1-f and a-1-d-2-f above the code he would choose the latter

But there is often a condition in the problem that restricts you to choose a satisfying condition in all the shortest paths, hd3790 is, the enclosed code is from: http://blog.csdn.net/cambridgeacm/article/details/7831535

This problem is the question of how to choose the shortest path, the condition is the shortest path in the circumstances of the selection of the least expensive, very typical basic problem, this code is very good, like

The idea of this code is as follows, first of all, when using a matrix to store the graph, pay attention to the problem of the heavy edge, and this problem processing, for the problem of another limit variable cost, the code used a map and cost to record the distance and costs of each line, with

Dis and value to record the origin to each point of distance and cost, and then is how to judge the shortest way to determine if it is the most economical, I am so understanding, if said three points are connected by the line, the first point is connected to the third point, because Dij on the edge of the slack, Always choose in the dis array inside away from the nearest and not slack, so from the shortest path, this point is definitely the shortest path point, but if the cost of this condition, then and it walk 123 the same and the more economical way is 1 direct to 3, so when we judge if Dis[1-3] ==DIS[1-2-3], we begin to judge the size of their cost.

<span style= "FONT-SIZE:14PX;" > #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace
Std
#define INF 0x7fffffff int n,m;
int map[1005][1005];
int cost[1005][1005];
    void Dijkstra (int st,int ed) {int i,j,v,min;
    int visit[1005],dis[1005],value[1005];
        for (i=1;i<=n;i++) {dis[i]=map[st][i];
    Value[i]=cost[st][i];
    } memset (Visit,0,sizeof (visit));
    Visit[st]=1;
        for (i=1;i<n;i++) {min=inf;
                for (j=1;j<=n;j++) if (!visit[j]&&dis[j]<min) {v=j;
            MIN=DIS[J];
        } visit[v]=1; for (j=1;j<=n;j++) {if (!visit[j]&&map[v][j]<inf) {if (Dis[j]
                    >dis[v]+map[v][j]) {dis[j]=dis[v]+map[v][j];
                VALUE[J]=VALUE[V]+COST[V][J]; } else if (dis[J]==dis[v]+map[v][j]) {if (value[j]>value[v]+cost[v][j]) VA
                LUE[J]=VALUE[V]+COST[V][J];
}}}} printf ("%d%d\n", dis[ed],value[ed]);
    } int main () {int i,j,st,ed;
    int a,b,c,d; while (scanf ("%d%d", &n,&m), n| |
                m) {for (i=1;i<=n;i++) for (j=1;j<=n;j++) {map[i][j]=inf;
            Cost[i][j]=inf;
            } while (m--) {scanf ("%d%d%d%d", &a,&b,&c,&d);
                if (map[a][b]>c)//This place is to prevent heavy side {map[a][b]=map[b][a]=c;
            Cost[a][b]=cost[b][a]=d; The else if (map[a][b]==c)//is as if the starting point and destination are the same, but they walk differently, taking the nearest route {if (cost[a][b]>
            d) Cost[a][b]=cost[b][a]=d;
        }} scanf ("%d%d", &st,&ed);
    Dijkstra (st,ed); } rEturn 0; } </span>
---------



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.