The shortest path, Dijkstra, SPFA - hdu1535

Topic Link

Training seniors did not give a negative title ring today, so all of a sudden all used Dijkstra did, did not SPFA to practice, so this special card SPFA title track does not bring thought

Title meaning

Seeking a point to another and the shortest distance, with the shortest distance to the point 1 and the other

Topic analysis

Seeking a point to another and the shortest distance, i.e. single-source shortest path, and the Dijkstra both rows should SPFA

The shortest distance and the other to a point, in fact, is to find a distance from the other points, but because the length of the path in both directions are not the same, so like a reverse achievements

Topic Code

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=1e6+7;
const int INF=0x3f3f3f3f;
typedef long long LL;
int head[maxn],dis[maxn];
int tot,u[maxn],v[maxn],w[maxn];
bool vis[maxn];
LL ans;
int t,n,m;
struct edge{
    int to,w,next;
}e[maxn];
struct node{
    int pos,dis;
    bool operator<(const node &x)const{
        return dis>x.dis;
    }
};
void add(int u,int v,int w){
    e[tot].to=v;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;
}
void init(){
    tot=0;
    memset(dis,INF,sizeof(dis));
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
}
void Dijkstra(){
    dis[1]=0;
    priority_queue<node>q;
    q.push((node){1,0});
    while(!q.empty()){
        node temp=q.top();q.pop();
        int u=temp.pos;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to;
            if(dis[v]>dis[u]+e[i].w){
                dis[v]=dis[u]+e[i].w;
                if(!vis[v])q.push((node){v,dis[v]});
            }
        }
    }
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        ans=0;
        init();
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&u[i],&v[i],&w[i]);
            add(u[i],v[i],w[i]);
        }
        Dijkstra();
        for(int i=2;i<=n;i++)
            ans+=dis[i];
        init();
        for(int i=1;i<=m;i++){
            add(v[i],u[i],w[i]);
        }
        Dijkstra();
        for(int i=2;i<=n;i++)
            ans+=dis[i];
        printf("%lld\n",ans);
    }
    return 0;
}
Dijkstra practice

Dijkstra really do much, again pass by

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int maxn=1e6+7;
const int INF=0x3f3f3f3f;
typedef long long LL;
int head[maxn],dis[maxn],num[maxn];
bool vis[maxn];
LL ans;
int t,n,m,tot,u[maxn],v[maxn],w[maxn];
struct edge{
    int to,w,next;
}e[maxn];
void add(int u,int v,int w){
    e[tot].to=v;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;
}
void init(){
    tot=0;
    memset(head,-1,sizeof(head));
    memset(vis,false,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    memset(num,0,sizeof(num));
}
bool SPFA(){
    queue<int>q;
    dis[1]=0;num[1]++;vis[1]=true;
    q.push(1);
    while(!q.empty()){
        int u=q.front();q.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to;
            if(dis[v]>dis[u]+e[i].w){
                dis[v]=dis[u]+e[i].w;
                if(!vis[v]){
                    vis[v]=true;
                    num[v]++;
                    q.push(v);
                    if(num[v]>n)return false;
                }
            }
        }
    }
    return true;
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        ans=0;
        init();
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&u[i],&v[i],&w[i]);
            add(u[i],v[i],w[i]);
        }
        SPFA();
        for(int i=2;i<=n;i++)
            ans+=dis[i];
        init();
        for(int i=1;i<=m;i++){
            add(v[i],u[i],w[i]);
        }
        SPFA();
        for(int i=2;i<=n;i++)
            ans+=dis[i];
        printf("%lld\n",ans);
    }
    return 0;
}
SPFA practice

I really wrote the unskilled, according to a template to write

I have thought again

 

Guess you like

Origin www.cnblogs.com/helman/p/11278463.html
Recommended