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

类似题解

There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le 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 T\le 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;
}

 

转载于:https://www.cnblogs.com/Draymonder/p/9596406.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值