ACM-ICPC 2018 南京赛区网络预赛 Magical Girl Haze 最短路

ACM-ICPC 2018 南京赛区网络预赛 Magical Girl Haze 最短路

题面

There are N cities in the country, and M directional roads from uu to v(1\(\le\) u, v\(\le\) n)v(1≤u,v≤n). Every road has a distance \(c_i\). Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances become 00. Now she wants to go to City NN, please help her calculate the minimum distance.

Input
The first line has one integer T(\(1 \le T\le 5\)), then following TT cases.

For each test case, the first line has three integers N, MN,M and KK.

Then the following MM lines each line has three integers, describe a road, \(U_i\), \(V_i\), \(C_i\). There might be multiple edges between uu and vv.

It is guaranteed that \(N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10, 0 \le C_i \le 1e9≤C i≤1e9\). There is at least one path between City 1 and City N.

Output
For each test case, print the minimum distance.

题意:

求1到n的最短路,路径中的k跳路可以不花费代价

思路:

考虑k很小,我们可以用dist[i][k]表示到i点,使用了k次路径免费的最短路径,二维最短路。然后魔改dijkstra。听说别人spfa被卡了。。。
#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 10;
const int MAXM = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;

int T, n, m, first[MAXN], sign, k;

struct Edge {
    int to, next;
    LL w;
} edge[MAXM * 2];

void init() {
    for(int i = 1; i <= n; i ++ ) {
        first[i] = 0;
    }
    sign = 1;
}

void add_edge(int u,int v,int w) {
    edge[sign].to = v;
    edge[sign].w  = w;
    edge[sign].next = first[u];
    first[u] = sign ++;
}

struct Node {
    int to, cnt;
    LL cost;
    Node(){}
    Node(int f, int s, int c):to(f), cost(s), cnt(c) {}
    friend bool operator < (const Node &a, const Node &b) {
        return a.cost > b.cost;
    }
};

LL dis[MAXN][11];

bool vis[MAXN][11];

LL dijkstra(int s) {
    priority_queue<Node> heap;
    memset(vis, 0, sizeof(vis));
    memset(dis, 0x3f, sizeof(dis));
    heap.push(Node(s, 0, 0));
    while(!heap.empty()) {
        Node now = heap.top();
        heap.pop();
        if(!vis[now.to][now.cnt]) {
            vis[now.to][now.cnt] = 1;
            dis[now.to][now.cnt] = now.cost;
            for(int i = first[now.to]; i; i = edge[i].next) {
                int to = edge[i].to;
                LL ww = edge[i].w;
                if(now.cnt + 1 <= k && !vis[to][now.cnt + 1]) {
                    heap.push(Node(to, now.cost, now.cnt + 1));
                }
                if(now.cnt <= k && !vis[to][now.cnt]) {
                    heap.push(Node(to, now.cost + ww, now.cnt));
                }
            }
        }
    }
    LL ans = INF;
    for(int i = 0; i <= k; i++ ) {
        ans = min(ans, dis[n][i]);
    }
    return ans;
}


int main() {

    while(~scanf("%d", &T)) {
        while(T--) {
            scanf("%d %d %d", &n, &m, &k);
            init();
            for(int i = 1; i <= m; i++ ) {
                int u, v, w;
                scanf("%d %d %d", &u, &v, &w);
                add_edge(u, v, w);
            }
            printf("%lld\n", dijkstra(1));
        }
    }


    return 0;
}
全部评论

相关推荐

找到实习就改名4月17日下午更改:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
04-13 18:10
门头沟学院 Java
想熬夜的小飞象在秋招:被腾讯挂了后爸妈以为我失联了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务