[BZOJ2763][JLOI2011]飞行路线(图论)

题目:

我是超链接

题解:

分层图来一波!就是在裸dij的基础上加了一步,可以往下一层转移状态

代码:

#include 
#include 
#include 
#include 
using namespace std;
const int N=50005;
struct hh{int x,c,dis;};
int tot,nxt[N*2],point[N],v[N*2],c[N*2];
int s,t,dis[10005][15],k;
bool vis[10005][15];
bool operator <(const hh a,const hh b){return a.dis>b.dis;}
void addline(int x,int y,int z)
{
    ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; c[tot]=z;
    ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; c[tot]=z;
}
int dij()
{
    priority_queueq;
    memset(dis,0x7f,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[s][0]=0;
    q.push((hh){s,0,0});
    while (!q.empty())
    {
        hh now=q.top(); q.pop();
        if (vis[t][k]) return dis[t][k];
        if (vis[now.x][now.c]) continue;
        vis[now.x][now.c]=1;    
        for (int i=point[now.x];i;i=nxt[i])
        {
            if (dis[v[i]][now.c]>dis[now.x][now.c]+c[i])
            {
                dis[v[i]][now.c]=dis[now.x][now.c]+c[i];
                q.push((hh){v[i],now.c,dis[v[i]][now.c]});
            }
            if (now.c+1<=k) dis[v[i]][now.c+1]=now.dis,q.push((hh){v[i],now.c+1,now.dis});
        }         
    }
}
int main()
{
    int n,m,i;
    scanf("%d%d%d",&n,&m,&k);
    scanf("%d%d",&s,&t);s++; t++;
    for (i=1;i<=m;i++)
    {
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        addline(x+1,y+1,z);
    }
    printf("%d",dij());
}

你可能感兴趣的:(图论)