[JLOI2011]飞行路线 最短路

题面

题面

题解

这题不是很难,因为删代价的次数不多,因此我们只需要将最短路中的状态加一维表示已经删了几次,再转移即可

#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 11000
#define ac 130000

int n, m, k, s, t, ans = 1000000000;
int Head[AC], date[ac], Next[ac], len[ac], tot;
int dis[AC][15];
bool vis[AC][15];

struct node{int x, dis, cost;};//编号 + 已经经过的距离 + 已经消耗的次数
struct cmp{bool operator () (node a, node b){return a.dis > b.dis;}};
priority_queue <node, vector<node>, cmp> q;

inline int read()
{
    int x = 0;char c = getchar();
    while(c > '9' || c < '0') c = getchar();
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x;
}

inline void upmin(int &a, int b) {if(b < a) a = b;}

inline void add(int f, int w, int S)
{
    date[++ tot] = w, Next[tot] = Head[f], Head[f] = tot, len[tot] = S;
    date[++ tot] = f, Next[tot] = Head[w], Head[w] = tot, len[tot] = S;
}

void pre()
{
    n = read(), m = read(), k = read();
    s = read() + 1, t = read() + 1;
    for(R i = 1; i <= m; i ++) 
    {
        int x = read() + 1, y = read() + 1, S = read();
        add(x, y, S);
    }
}

void dij()
{
    memset(dis, 127, sizeof(dis)), dis[s][0] = 0;
    q.push((node){s, 0, 0});
    while(!q.empty())
    {
        node x = q.top();
        q.pop();
        while(!q.empty() && vis[x.x][x.cost]) x = q.top(), q.pop();
        if(vis[x.x][x.cost]) break;
        vis[x.x][x.cost] = true;
        for(R i = Head[x.x]; i; i = Next[i])
        {
            int now = date[i];
            if(dis[now][x.cost] > dis[x.x][x.cost] + len[i])//不使用机会
            {
                dis[now][x.cost] = dis[x.x][x.cost] + len[i];
                q.push((node){now, dis[now][x.cost], x.cost});
            }
            if(x.cost < k && dis[now][x.cost + 1] > dis[x.x][x.cost])
            {
                dis[now][x.cost + 1] = dis[x.x][x.cost];
                q.push((node){now, dis[now][x.cost + 1], x.cost + 1});
            }
        }
    }
    for(R i = 0; i <= k; i ++) upmin(ans, dis[t][i]);
    printf("%d\n", ans);
}

int main()
{
//	freopen("in.in", "r", stdin);
    pre();
    dij();
//	fclose(stdin);
    return 0;
}
posted @ 2019-02-07 01:26  ww3113306  阅读(162)  评论(0编辑  收藏  举报
知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议进行许可。