【BZOJ 2763 JLOI 2011】飞行路线 (SPFA+分层图)

Description

Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?
Input

数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。
第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t< n)
接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b< n,a与b不相等,0<=c<=1000)
Output

只有一行,包含一个整数,为最少花费。 Sample Input

5 6 1

0 4

0 1 5

1 2 5

2 3 5

3 4 5

2 3 3

0 2 100

Sample Output

8 HINT

对于30%的数据,2<=n<=50,1<=m<=300,k=0;

对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;

对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.

本题的重点是我们需要判断免费次数何时使用
因为免费次数的使用会导致不同的路径 因此该图是一个分层图 也有着一定的DP理念
在第i层,我们可以继续按照原来的路线行走花费原来的权值,也可以用一次免费机会
如果使用,则应将第i层的距离向i+1层转移,不加边权,相当于在这里用了一次免费机会;如果不使用,则应将第i层的距离向i+1层转移,加边权,免费机会不变。
最后分析什么时候使用免费机会可以将权值之和最小,免费次数依次减去1,并且无视该点的权值。

代码如下:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN = 100000, MAXE = 21474836;
struct Edge
{
    int from, to, cost;
}es[MAXN << 2];
int first[MAXN], nxt[MAXN], tot, d[MAXN][15], n, m, k;
bool used[MAXN][15];
queue < int > q; 

void build(int f, int t, int d)
{
    es[++tot].from = f;
    es[tot].to = t;
    es[tot].cost = d;
    nxt[tot] = first[f];
    first[f] = tot;
}

void spfa(int s)
{
    d[s][0] = 0;
    q.push(s);
    q.push(0);
    used[s][0] = 1;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        int time = q.front();
        q.pop();
        used[x][time] = 0;
        for(int i = first[x]; i != -1; i = nxt[i])
        {
            int u = es[i].to;
            if(d[u][time] > d[x][time] + es[i].cost)
            {
                d[u][time] = d[x][time] + es[i].cost;
                if(!used[u][time])
                {
                    q.push(u);
                    q.push(time);
                    used[u][time] = 1;
                }
            }
            if(time < k)
            {
                if(d[u][time + 1] > d[x][time])
                {
                    d[u][time + 1] = d[x][time];
                    if(!used[u][time + 1])
                    {
                        q.push(u);
                        q.push((time + 1));
                        used[u][time + 1] = 1;
                    }                   
                }
            }
        }
    }
}

int main()
{
    scanf("%d%d%d", &n, &m, &k);
    memset(first, -1, sizeof(first));
    int s, e;
    scanf("%d%d", &s, &e);
    for(int i = 1; i <= m; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        build(a, b, c);
        build(b, a, c);
    }
    for(int i = 0; i < n; i++)
        for(int  j = 0; j <= k; j++)
            d[i][j] = MAXE;
    spfa(s);
    cout<<d[e][k];
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值