最短路图论模板

单源最短路dij
O ( ( E + N ) l o g N ) O((E+N)logN) O((E+N)logN)




const int N=3000;
const int M=71000;
int n,m;
int dis[N];
int vis[N];
struct edge
{
    int to,v;
};
struct qnode
{
    int id;
    int v;
    bool operator <(const qnode &r)const
    {
        return v>r.v;
    }
};
vector<edge> G[N];
void addedge(int u,int v,int w){
    G[u].push_back(edge{v,w});
}
void init(){
    for(int i=0;i<=n;i++) G[i].clear();
}


void Dij(int start)
{
    clr(vis,0);
    clr(dis,INF);
    priority_queue<qnode> q;
    dis[start] = 0;
    q.push(qnode{start,0});
    while(!q.empty()){
        qnode cur=q.top();
        q.pop();
        int u=cur.id;
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i].to,w=G[u][i].v;
            if(!vis[v]&&dis[v]>dis[u]+w){
                dis[v]=dis[u]+w;
                q.push(qnode{v,dis[v]});
            }
        }
    }
}
//前向星模板 防止类似poj老oj卡vector 一般不用
const int N=40010;
const int M=150010;
int head[N];
int top;
int n,m;
struct Edge
{
    int val;
    int to;
    int next;
}edge[M];
void addedge(int a,int b,int c)
{
    edge[top].to=b;
    edge[top].val=c;
    edge[top].next=head[a];
    head[a]=top++;
}
void init()
{
    top=0;
    clr(head,-1);
}

int dis[N];
int vis[N];

struct qnode
{
    int id;
    int dis;
    bool operator <(const qnode &r)const
    {
        return dis>r.dis;
    }
};


void Dij(int start)
{
    clr(vis,0);
    clr(dis,INF);
    priority_queue<qnode> q;
    dis[start] = 0;
    q.push(qnode{start,0});
    while(!q.empty()){
        qnode cur=q.top();
        q.pop();
        int u=cur.id;
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=head[u];~i;i=edge[i].next){
            int v=edge[i].to,w=edge[i].val;
            if(!vis[v]&&dis[v] > w+dis[u]){
                dis[v] = w+dis[u];
                q.push(qnode{ v,dis[v]});
            }
        }
    }
}


spfa
O ( k m ) O(km) O(km) k最坏情况下是n


const int N=10010;
const int M=500010;
int n,m;


int dis[N];
struct edge{
    int to;int v;
};
vector<edge> G[N];
int vis[N];
int cnt[N];//每个点的入队次数
void init(){
    for(int i=0;i<=n;i++) G[i].clear();
}
void addedge(int u,int v,int w){
    G[u].push_back(edge{v,w});
}
int spfa(int start){
    clr(vis,0);
    clr(dis,INF);
    queue<int> q;
    dis[start] = 0;
    //vis[start] = 1;
    clr(cnt,0);
    cnt[start] = 1;
    q.push(start);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0; //不在队列里了
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i].to;
            int cost=G[u][i].v;
            if(dis[v]>dis[u]+cost)
            {
                dis[v]=dis[u]+cost;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                    if(++cnt[v]>n) return 0;
                }
            }
        }
    }
    return 1;
}
前向星
const int N=50010;
const int M=50010;

int head[N];
int top;
int n,m;
struct Edge
{
    int val;
    int to;
    int next;
}edge[M];
void addedge(int a,int b,int c)
{
    edge[top].to=b;
    edge[top].val=c;
    edge[top].next=head[a];
    head[a]=top++;
}
void init()
{
    top=0;
    clr(head,-1);
}
int dis[N];
int vis[N];
int cnt[N];//每个点的入队次数

int spfa(int start){
    clr(vis,0);
    clr(dis,INF);
    queue<int> q;
    dis[start] = 0;
    //vis[start] = 1;
    clr(cnt,0);
    cnt[start] = 1;
    q.push(start);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0; //不在队列里了
        for(int i=head[u];~i;i=edge[i].next){
            int v=edge[i].to;
            int cost=edge[i].val;
            if(dis[v]>dis[u]+cost)
            {
                dis[v]=dis[u]+cost;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                    if(++cnt[v]>n) return 0;
                }
            }
        }
    }
    return 1;
}

//读入优化 哎 poj
inline LL read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

你可能感兴趣的:(acm,acm模板,图论,最短路,差分约束)