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

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(1≤u,v≤n). 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(1≤T≤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_iUi​,Vi​,Ci​. 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 1e90≤Ci​≤1e9. 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,求 s 到 t 的最短路。

思路:在迪杰斯特拉的 dis 数组上多加一维表示更改的次数,松弛的时候多一种松弛操作。。

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

const int maxn = 1e5 + 10;
const LL inf = 0x3f3f3f3f3f3f3f3f;
int head[maxn],cnt = 0;
struct xx{
    int v,nex;
    LL w;
    xx(int v = 0,LL w = 0,int nex = 0):
        v(v),w(w),nex(nex){}
}edge[maxn << 1];
LL dis[maxn][20];

void init(int n){
    for(int i = 0;i < n + 5;++ i)
        head[i] = -1;
    cnt = 0;
}

struct Point{
    int q,k;
    Point(int q = 0,int k = 0):
        q(q),k(k){}
    friend bool operator < (const Point &A,const Point &B){
        return dis[A.q][A.k] > dis[B.q][B.k];
    }
};

void dij(int s,int t,int k){
    clr(dis,inf);
    dis[s][0] = 0;
    priority_queue<Point>Q;
    Q.push(Point(s,0));
    while(!Q.empty()){
        Point now = Q.top();
        Q.pop();
        for(int i = head[now.q]; ~i;i = edge[i].nex){
            int v = edge[i].v;
            LL w = edge[i].w;
            if(dis[v][now.k] > dis[now.q][now.k] + w){
                dis[v][now.k] = dis[now.q][now.k] + w;
                Q.push(Point(v,now.k));
            }
            if(now.k + 1 <= k){
                if(dis[v][now.k + 1] > dis[now.q][now.k]){
                    dis[v][now.k + 1] = dis[now.q][now.k];
                    Q.push(Point(v,now.k + 1));
                }
            }
        }
    }
    printf("%lld\n",dis[t][k]);
}

int main() {
    int t; scanf("%d",&t);
    while(t --){
        int n,m,k; scanf("%d%d%d",&n,&m,&k);
        init(n);
        for(int i = 1;i <= m;++ i){
            int u,v; LL w;
            scanf("%d%d%lld",&u,&v,&w);
            edge[cnt] = xx(v,w,head[u]);
            head[u] = cnt ++;
        }
        dij(1,n,k);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值