BZOJ 2763飞行路线

题目

传送门

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.

题解

这是一个分层图的模板题。
把图分成k层,每层仍然是n个点,每层的连边方式仍然和原图相同。除此以外,层与层之间该如何连边呢?
我们假设现在在第i层,点u和点v之间有一条权值w的边,对于下一层,即i+1层,u’与v’之间仍是有一条权为w的边,且对于u与v’之间应该加上一条边权为0的边。所以如果每一层有m条边,第i层和第i+1层之间也一定是有m条边。
对于我们当前找到的终点,尝试起点的状态去更新,不选择此条边免费的状态和选择此条边免费的状态,再将这两个状态压入队列去更新可以到达的其他状态。

CODE

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <iomanip>    
#include <iostream>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 2e6 + 100;   
typedef long long ull;
typedef pair <int, int > pii;

inline int read() {
    int s = 0, w = 1;
    char ch = getchar();
    while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); }
    while (isdigit(ch)) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
    return s * w;
}

int n, m, s, t, k, num_edge = 0, ans = 0x3f3f3f3f;
struct Edge { int next, to, dis; } edge[maxn << 1];
int head[maxn], dis[maxn], vis[maxn];
priority_queue <pii, vector <pii>, greater<pii> > q;

inline void add(int from, int to, int dis) {
    edge[++num_edge].to = to;
    edge[num_edge].dis = dis;
    edge[num_edge].next = head[from];
    head[from] = num_edge;
}

inline int point(int x, int y) { return x + y * n; }

void dijkstra(int k) {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    dis[k] = 0;
    q.push(make_pair(dis[k], k));
    while (!q.empty()) {
        int u = q.top().second; q.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = head[u]; i; i = edge[i].next) {
            int v = edge[i].to;
            if (dis[v] > dis[u] + edge[i].dis) {
                dis[v] = dis[u] + edge[i].dis;
                if (!vis[v]) q.push(make_pair(dis[v], v));
            }
        }
    }
}

int main() {
    n = read(), m = read(), k = read(), s = read(), t = read();
    for (int i = 1; i <= m; ++i) {
        int a, b, c;
        a = read(), b = read(), c = read();
        for (int j = 0; j <= k; ++j) {
            add(point(a, j), point(b, j), c);
            add(point(b, j), point(a, j), c);
        }
        for (int j = 0; j <= k; ++j) {
            add(point(a, j), point(b, j + 1), 0);
            add(point(b, j), point(a, j + 1), 0);
        }
    }
    dijkstra(s);
    for (int i = 0; i <= k; ++i) {
        ans = min(ans, dis[point(t, i)]);
    }
    printf("%d\n", ans);
    // system("PAUSE");
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值