ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze

There are NNN cities in the country, and MMM directional roads from uuu to v(1≤u,v≤n)v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance cic_ici​. Haze is a Magical Girl that lives in City 111, she can choose no more than KKK roads and make their distances become 000. Now she wants to go to City NNN, please help her calculate the minimum distance.

Input

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

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

Then the following MMM lines each line has three integers, describe a road, Ui,Vi,CiU_i, V_i, C_iUi​,Vi​,Ci​. There might be multiple edges between uuu and vvv.

It is guaranteed that N≤100000,M≤200000,K≤10N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0≤Ci≤1e90 \le C_i \le 1e90≤Ci​≤1e9. There is at least one path between City 111 and City NNN.

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

AC代码

//#include<bits/stdc++.h>
#define _CRT_SBCURE_NO_DEPRECATE
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
//#define UP(i,x,y) for(int i=x;i<=y;i++)
//#define DOWN(i,x,y) for(int i=x;i>=y;i--)
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lson k<<1
#define rson k<<1|1
#define mid (1+r)/2
#define ms(x, y) memset(x, y, sizeof x)
#define MOD 142857
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 201000;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

int n, m, t, k;
struct edge
{
    int u, v, c;
    int next;
};
struct node
{
    int v, cost;
    node(int v, int c):v(v),cost(c){}
    friend bool operator<(node a, node b)
    {
        return a.cost > b.cost;
    }
};
edge G[maxn*100];
int h[maxn*5];
int len;
int d[maxn*5];
void add(int u, int v, int c)
{
    G[len].u = u;
    G[len].v = v;
    G[len].c = c;
    G[len].next = h[u];
    h[u] = len++;
}
void init()
{
    fill(h, h+maxn*5, -1);
    len = 0;
}
void dij()
{
    fill(d, d+maxn*5, INF);
    d[1] = 0;
    priority_queue<node> pque;
    pque.push(node(1,0));
    while(pque.size())
    {
        node p = pque.top();pque.pop();
        int u = p.v;
        if(d[u] < p.cost) continue;

        for(int i = h[u]; i!=-1; i = G[i].next)
        {
            edge e = G[i];
            if(d[e.v] > d[u]+e.c)
            {
                d[e.v] = d[u]+e.c;
                pque.push(node(e.v, d[e.v]));
            }
        }
    }
}
int main()
{
	//freopen("out.txt", "w", stdout);
	sd(t);
	while(t--)
    {
        init();
        sddd(n, m, k);
        int ta, tb, tc;
        for(int i = 0; i < m; i++)
        {
            sddd(ta, tb, tc);
            add(ta,tb,tc);
            for(int j = 1; j <= k; j++)
            {
                add(ta+j*n, tb+j*n, tc);
            }
        }
        for(int i = 1; i <= k; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                for(int x = h[j]; x!=-1; x = G[x].next)
                {
                    edge e = G[x];
                    add(e.u, e.v+i*n, 0);
                }
            }
        }
        dij();
        int ans = INF;
        for(int i = 0; i <=k; i++)
        {
            ans = min(ans, d[n+i*n]);
        }
        printf("%d\n", ans);
    }
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值