码迷,mamicode.com
首页 > 其他好文 > 详细

SSL 2677_飞行_spfa

时间:2017-08-10 22:39:44      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:++   ace   logs   城市   names   its   scan   string   spfa   

题目描述

有n个城市,编号为0到n-1。小B想从城市s到城市t。他们选择了一家航空公司,这家公司有m种航线,每种航线连接了两个不同的城市。看在小B是个妹子的份上,航空公司的老总给了小B一点优惠:小B可以免费在最多k种航线上搭乘飞机。问小B最小花费是多少。


 

思路

在普通的spfa上多加一维, state[i][j]表示从起点到i点用了j此免费的最小花费,转移显然

在spfa时多枚举一个j即可


#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define maxn 100001
#define maxm 500001
#define INF 0x3f3f3f3f
#define min(x, y) (x) < (y) ? (x) : (y)
#define fill(x, y) memset(x, y, sizeof(x))
struct edge
{
    int to, w, next;
}e[maxm * 2];
int ls[maxm * 2], n, m, k, s, t, state[maxn][20], exits[maxn]; 
int spfa()
{
    fill(state, INF);
    queue<int> t;
    t.push(s);
    exits[s] = 1;
    for (int i = 0; i <= k; i++)
        state[s][i] = 0;
    while (!t.empty())
    {
        int now = t.front();
        t.pop();
        for (int i = ls[now]; i; i = e[i].next)
            for (int j = 0; j <= k; j++)
            {
                if (state[now][j] + e[i].w < state[e[i].to][j])
                {
                    state[e[i].to][j] = state[now][j] + e[i].w;
                    if (!exits[e[i].to])
                    {
                        exits[e[i].to] = 1;
                        t.push(e[i].to);
                    }
                }
                if (state[now][j] < state[e[i].to][j + 1] && j + 1 <= k)
                {
                    state[e[i].to][j + 1] = state[now][j];
                    if (!exits[e[i].to])
                    {
                        exits[e[i].to] = 1;
                        t.push(e[i].to);
                    }
                }
            }
        exits[now] = 0;
    }
}
int main()
{
    scanf("%d%d%d%d%d", &n, &m, &k, &s, &t);
    int l = 0;
    for (int i = 1;i <=m; i++)
    {
        int x, y, z;
        scanf("%d%d%d", &x, &y, &z);
        e[++l] = (edge) {y, z, ls[x]};
        ls[x] = l;
        e[++l] = (edge) {x, z, ls[y]};
        ls[y] = l;
    }
    spfa();
    int ans = INF;
    for (int i = 0; i <= k; i++)
    {
        ans = min(ans, state[t][i]);
    }
    printf("%d\n", ans);
}

 

SSL 2677_飞行_spfa

标签:++   ace   logs   城市   names   its   scan   string   spfa   

原文地址:http://www.cnblogs.com/nidhogg/p/7341437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!