• ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图


    类似题解

    There are NN cities in the country, and MM directional roads from uu to v(1le u, vle n)v(1u,vn). Every road has a distance c_ici. 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)T(1T5), 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_iUi,Vi,Ci. There might be multiple edges between uu and vv.

    It is guaranteed that N le 100000, M le 200000, K le 10N100000,M200000,K10,
    0 le C_i le 1e90Ci1e9. There is at least one path between City 11 and City NN.

    Output

    For each test case, print the minimum distance.

    样例输入

    1
    5 6 1
    1 2 2
    1 3 4
    2 4 3
    3 4 1
    3 5 6
    4 5 2

    样例输出

    3
    还是一个最短路 只是可以将其中的k条路 权值变为0,那么就可以 用一个d[maxn][K] 跑K次最短路 就能得到结果了,算法思路并不复杂
    在此认识了 链式前向星,以及 爆int 之类的 问题,另外本题 卡Vector,所以不得不用 前向星
    #include <bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    typedef pair<ll,int> pli;
    const int N = 100000+10;
    const int M = 200000+10;
    
    int n,m,k,tot,head[N];
    ll d[N][12];bool vis[N][12];
    
    struct node {
        int to,nex,val;
    }mp[M];
    
    void init() {
        tot = 0;
        memset(head,0,sizeof(head));
        memset(vis,0,sizeof(vis));
    }
    
    void addedge(int u,int v,int val) {
        tot++;
        mp[tot].to = v;
        mp[tot].nex = head[u];
        mp[tot].val = val;
        head[u] = tot;
    }
    
    void solve() {
        priority_queue<pli, vector<pli>, greater<pli> > que;
        //while(que.size()) que.pop();
        memset(d,0x3f,sizeof(d));
        d[1][0] = 0; que.push(pli(0, 1));
        while (!que.empty()) {
            int u = que.top().second;
            ll dis = que.top().first;
            int level = u/(n+1); u %= (n+1);
            que.pop();
            if(vis[u][level]) continue;
            vis[u][level] = 1;
            for(int i=head[u]; i; i=mp[i].nex) {
                int v = mp[i].to;
                if(dis + mp[i].val <= d[v][level]) {
                    d[v][level] = dis + mp[i].val;
                    que.push({d[v][level], level*(n+1)+v});
                }
                if(level==k) continue;
                if(dis <= d[v][level+1]) {
                    d[v][level+1] = dis;
                    que.push({dis, (level+1)*(n+1)+ v});
                }
            }
        }
        cout << d[n][k] <<endl;
    }
    
    int main () {
        freopen("in.txt","r",stdin);
        int T; scanf("%d", &T);
        while(T--) {
            init();
            scanf("%d %d %d", &n, &m, &k);
            for(int i=1; i<=m; i++) {
                int u,v,val;
                scanf("%d %d %d", &u,&v,&val);
                addedge(u,v,val);
            }
            solve();
        }
        return 0;
    }
  • 相关阅读:
    云HBase发布全文索引服务,轻松应对复杂查询
    eclipse调试断点
    IntelliJ IDEA使用小技巧
    jQuery遍历not的用法
    request.querystring和request.form的区别
    ppt打不开,显示发现文件中的内容有问题。可尝试修复此演示文稿
    中文乱码 encodeURI来解决URL传递时的中文问题
    Http请求中Content-Type讲解以及在Spring MVC中的应用
    Display Tag Lib Table进行分页
    js刷新页面
  • 原文地址:https://www.cnblogs.com/Draymonder/p/9596406.html
Copyright © 2020-2023  润新知