zoukankan      html  css  js  c++  java
  • 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 Tle 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
    ", dijkstra(1));
            }
        }
    
    
        return 0;
    }
    
  • 相关阅读:
    关于'for' loop initial declaration used outside C99 mode的说明
    Qt编写安防视频监控系统50-地图配置
    当封装已成为往事
    关于Qt数据库相关开发的一些经验总结
    Qt编写安防视频监控系统49-多数据库支持
    Qt编写安防视频监控系统48-视频参数
    设计测试用例优点和缺点
    常用的七种性能测试方法
    测试资料学习网站推荐
    打印$_SERVER['REQUEST_SCHEME']为空
  • 原文地址:https://www.cnblogs.com/Q1143316492/p/9571546.html
Copyright © 2011-2022 走看看