Eoj 1848 are you ACM? Study on optimization of Dijkstra + SPFA algorithm with binary heap

Source: Internet
Author: User

Description

With the rapid development of China's economy, China's logistics industry has ushered in a developing spring. Especially in Shanghai, an international metropolis with vast domestic hinterland, the logistics industry expands at an unprecedented rate.
Of course, the big cake will attract many chanzui cats, Chanzui more cats will have a brutal competition. When large sums of money flowed into the logistics industry, the KOP group decided to make improvements to the current transportation plan in order to reduce its cost and to keep other competitors out of the country to stabilize the first place in the domestic logistics industry.
As the world's Top 100 Kop Group of course know that to find the best transport solutions, must rely on mathematical and algorithmic good software engineers, so they naturally find ECNU Software Institute. decided to find the best engineer (Acm:ace Coding man) by sponsoring a program competition.
The competition has only one topic, is the simple abstraction of the transportation line, test instructions as follows:
SH City has N transport transit points (simply labeled as the three-way,...., N), the transit point may have a transport line, this line has a special place is from A to B points need to spend C1 units of chakra (SH City's monetary unit), but from B to a may need C2 chakra. Of course C1 is not necessarily equal to C2, and can not return a from B after a to B. You can understand that these lines are "one-way". There is a total of M in the line. Every day there is a N-1 car from the Kop Group headquarters (which is assumed to be a transit point labeled 1), departure, respectively, to N-1 the remaining transit points, and then the same day from the arrival of the transit point back. Your task is to ask for the minimum cost of a day.

Input

The first behavior, C, indicates that there is a C test column. The next one is the C test column.
The format for each test case is as follows:
The first behavior is two integers, n,m, respectively, indicating that there are N points and M roads. (1=< N, M <=1000000.);
The next M-line has three values a B C, respectively, indicating that the chakras from point A to transit point B need to cost C units. (0<= C <= 1000000000).

Output

Your output should include line C, with each row outputting a value that corresponds to the minimum cost of the corresponding column.

Sample Input

2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50

Sample Output

46
210

Topic Analysis: Give a direction to figure, guarantee is Unicom, ask 1 to all points of the minimum distance and add all points to 1 the minimum distance and the sum, only need to separate the side forward storage and reverse storage once, do two single-source shortest path algorithm can. Because of the number of points, the Adjacency table method is used to store the graph, and the adjacency graph can be used to reduce the number of enumerations and improve the efficiency. Available 1. Two fork heap optimization Dijkstra method and 2.SPFA algorithm is completed.

Method 1:2 Fork Heap optimization Dijkstra. The shortest distance from the current to the source point of each point is placed in the heap, each time can be removed in the Logn time complexity, each time the use of new added points update the distance into the heap, in order to distinguish whether a point is added to the source point of the collection, plus a tag array, the overall idea and common Dijkstra See the AC code below.

Method two: SPFA algorithm. Its complexity is O (KE), E is the number of edges, K is the average number of updates per point, usually less than 2. The main idea of the algorithm: to set up a FIFO queue to save the node to be optimized, optimize each time the team first node u, and with u point the current shortest path value to the point of the U point to the node V for relaxation operation, if the shortest path value of the V point is adjusted, and the V point is not in the current queue, the V point This keeps the nodes out of the queue for relaxation operations until the queue is empty. Because each time the point is put into the end of the team, it is achieved by relaxation operation. In other words, each time the optimization will have a point V of the shortest path estimate d[v] becomes smaller. So the execution of the algorithm will make D smaller. Assume that there is no negative weight loop in the diagram, so each node has a shortest path value. Therefore, the algorithm does not execute indefinitely, as the D value gradually becomes smaller, until the shortest path value is reached, the algorithm ends, and the shortest path estimate is the shortest path value of the corresponding node.

Comparison: Method One is to use heap optimization Dijkstra each time to find the shortest estimated distance efficiency, tag array to mark whether a point has been added to the source point collection, that is, the optimal. Method Two is to constantly update the shortest distance estimate of each point, until the position can not be updated, the idea is to approximate the optimal solution. Also requires a tag array to mark whether a point is already in the team (preventing the point in the team from being enqueued again has increased efficiency). In addition, Dijkstra can not handle the graph with negative rights, SPFA may, but need to determine whether there is a negative power of the loop (that is, whether a bit of the queue n times).

Dijkstra AC Code:

#include <iostream>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <string>

#include <vector>

#include <map>

#include <algorithm>

#include <queue>

using namespace Std;

const int maxn=1000001;

Const long Long inf=1000000001;

struct edge{

int from,to,dist;

};

int n,m;

Vector<edge> edge[2];

Vector<int> G[2][MAXN];

Long Long D[2][MAXN];

BOOL VIS[MAXN];

struct node{

Long Long D;

int u;

BOOL operator< (const node& x) const{

Return d>x.d;

}

};

void Addedge (int from,int to,int DIS,INTK) {

Edge[k].push_back (Edge) {From,to,dis});

int t=edge[k].size ();

G[k][from].push_back (t-1);

}

void Dijkstra (int s,int k) {

Priority_queue<node> Q;

memset (vis,false,sizeof (VIS));

for (int i=1;i<=n;++i) D[k][i]=inf;

d[k][s]=0;

Q.push (Node) {0,s});

while (!q.empty ()) {

Node x=q.top (); Q.pop ();

int u=x.u;

if (Vis[u]) continue;

Vis[u]=true;

for (int i=0;i<g[k][u].size (); ++i) {

Edge &e=edge[k][g[k][u][i]];

if (D[k][u]+e.dist<d[k][e.to]) {

D[k][e.to]=d[k][u]+e.dist;

Q.push (Node) {d[k][e.to],e.to});

}

}

}

}

int main ()

{

int t;

scanf ("%d", &t);

while (t--) {

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

Edge[1].clear (); Edge[0].clear ();

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

G[0][i].clear ();

G[1][i].clear ();

}

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

int a,b,c;

scanf ("%d%d%d", &a,&b,&c);

Addedge (a,b,c,0);

Addedge (b,a,c,1);

}

Dijkstra (1,0);

Dijkstra (a);

Long Long ans=0;

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

Ans+=d[0][i]+d[1][i];

printf ("%i64d\n", ans);

}

return 0;

}

SPFA AC Code:

#include <iostream>

#include <cstring>

#include <cstdio>

#include <list>

#include <queue>

using namespace Std;

struct e{

int e,cost,next;

}E[2][1000005];

BOOL vis[1000005];

Long Long low[1000005];

int head[2][1000005];

void Spfa (int k) {

memset (vis,false,sizeof (VIS));

int i;

for (i=0;i<1000005;++i) Low[i]=1e10;

Vis[1]=true;

low[1]=0;

Queue<int> Q;

Q.push (1);

while (!q.empty ()) {

int Tem=q.front ();

Q.pop ();

Vis[tem]=false;

for (I=head[k][tem];i!=-1;i=e[k][i].next) {

int en=e[k][i].e;

if (Low[tem]+e[k][i].cost<low[en]) {

Low[en]=low[tem]+e[k][i].cost;

if (!vis[en]) {

Vis[en]=true;

Q.push (en);

}

}

}

}

}

int main ()

{

int t,i;

scanf ("%d", &t);

while (t--) {

int m,n,a,b,c;

memset (head,-1,sizeof (head));

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

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

scanf ("%d%d%d", &a,&b,&c);

E[0][i].cost=e[1][i].cost=c;

E[0][i].e=b;

E[0][i].next=head[0][a];

Head[0][a]=i;

E[1][i].e=a;

E[1][I].NEXT=HEAD[1][B];

Head[1][b]=i;

}

Long Long ans=0;

SPFA (0);

for (I=2;i<=n;++i) {

Ans+=low[i];

}

SPFA (1);

for (I=2;i<=n;++i)

Ans+=low[i];

printf ("%lld\n", ans);

}

return 0;

}

Eoj 1848 are you ACM? Study on optimization of Dijkstra + SPFA algorithm with binary heap

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.