bzoj2763飞行路线(dijkstra和SPFA两个版本)

这个题可以当成学习分层图的入门题,包括bzoj的2662冻结,很裸也很好帮助我们理解分层图思想。
SPFA版:

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 100000;//不要开太大,会T  
int head[MAXN],dis[MAXN][20],nxt[MAXN<<1],tot,n,m,k,st,se;
bool vis[MAXN][20];
struct Edge
{
    int from,to,cost;
}edge[MAXN<<1];
queue <int >q;
void build(int f,int t,int d)
{
    edge[++tot].from = f;
    edge[tot].to = t;
    edge[tot].cost = d;
    nxt[tot] = head[f];
    head[f] = tot;
}
void spfa(int s)
{
    memset(dis,0x7f,sizeof(dis));
    dis[s][0] = 0;
    q.push(s);
    q.push(0);
    vis[s][0] = 1;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        int cs = q.front();
        q.pop();
        vis[x][cs] = 0;
        for(int i = head[x]; i; i = nxt[i])
        {
            int v = edge[i].to;
            if(dis[v][cs] > dis[x][cs] + edge[i].cost)
            {
                dis[v][cs] = dis[x][cs] + edge[i].cost;
                if(!vis[v][cs])
                {
                    vis[v][cs] = 1;
                    q.push(v);
                    q.push(cs);     
                }
            }
            if(cs < k)
            if(dis[v][cs+1] > dis[x][cs])
            {
                dis[v][cs+1] = dis[x][cs];
                if(!vis[v][cs+1])
                {
                    q.push(v);
                    q.push(cs+1);
                    vis[v][cs+1] = 1;
                }
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    scanf("%d%d",&st,&se);
    for(int i = 1; i <= m; i ++)
    {
        int aa,bb,cc;
        scanf("%d%d%d",&aa,&bb,&cc);
        build(aa,bb,cc);
        build(bb,aa,cc);
    }
    spfa(st);
    int ans = 214748364;
    for(int i = 0; i <= k; i ++)
    ans = min(ans,dis[se][i]);
    printf("%d",ans);
}

dijkstra

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 3000000+5;//不要太小,会RE。。 
int head[MAXN],tot,n,m,k,dis[MAXN],nxt[MAXN<<1];
bool vis[MAXN];
struct Edge
{
    int from,to,cost;
}edge[MAXN << 1];
struct zt
{
    int u,v;
    bool operator < (const zt &b) const 
    {
        return v > b.v;
    };
};
priority_queue  q;
void build(int f,int t,int d)
{
    edge[++tot] = (Edge){f,t,d};
    nxt[tot] = head[f];
    head[f] = tot;
}
void dijkstra(int s)
{
    memset(dis,0x7f,sizeof(dis));
    dis[s] = 0;
    q.push((zt){s,0});
    while(!q.empty())
    {
        int x = q.top().u;q.pop();
        if(vis[x])
        continue;
        vis[x] = 1;
        for(int i = head[x]; i ; i = nxt[i])
        {
            int v = edge[i].to;
            if(dis[v] > dis[x]+edge[i].cost)
            {
                dis[v] = dis[x]+edge[i].cost;
                q.push((zt){v,dis[v]});
            }
        }
    }
}
int main()
{
    int s, t;
    scanf("%d%d%d",&n,&m,&k);
    scanf("%d%d",&s,&t);
    for(int i = 1; i <= m; i ++)
    {
        int aa,bb,cc;
        scanf("%d%d%d",&aa,&bb,&cc);
        for(int j = 0; j <= k; j ++)
        {
            build(j*n+aa,j*n+bb,cc);
            build(j*n+bb,j*n+aa,cc);
            if(j < k)
            {
                build(j*n+aa ,(j+1)*n+bb,0);
                build(j*n+bb ,(j+1)*n+aa,0);
            }
        }
    }
    dijkstra(s);
    int ans = 214748364;
    for(int i = 0; i <= k; i ++)
    ans = min(ans,dis[i*n+t]);
    printf("%d",ans);
    return 0;
}

你可能感兴趣的:(图论,spfa,dijkstra,分层图,bzoj)