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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GYH0730/article/details/82289927

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

边去重+分层最短路

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 100005;
const int MAXM = 200005;
int k;
struct node1
{
    int u,v;
    ll w;
}e[MAXM];
bool cmp(struct node1 a,struct node1 b)
{
    if(a.u == b.u && a.v == b.v) return a.w < b.w;
    else if(a.u == b.u) return a.v < b.v;
    return a.u < b.u;
}
struct node2
{
    int to,Next;
    ll w;
}edge[MAXM];
int tot,head[MAXN];
void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,ll w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].Next = head[u];
    head[u] = tot++;
}
struct node3
{
    int x,y;
    ll dis;
    node3(){}
    node3(int _x,int _y,ll _dis)
    {
        x = _x;
        y = _y;
        dis = _dis;
    }
    friend bool operator < (const node3 a,const node3 b)
    {
        return a.dis > b.dis;
    }
};
ll dis[MAXN][15];
bool vis[MAXN][15];
void dijkstra()
{
    memset(vis,false,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    dis[1][0] = 0;
    priority_queue<node3> pq;
    pq.push(node3(1,0,0));
    while(!pq.empty()) {
        struct node3 temp = pq.top();
        pq.pop();
        int x = temp.x;
        int y = temp.y;
        if(vis[x][y]) continue;
        vis[x][y] = true;
        for(int i = head[x]; i != -1; i = edge[i].Next) {
            int to = edge[i].to;
            if(dis[x][y] + edge[i].w < dis[to][y]) {
                dis[to][y] = dis[x][y] + edge[i].w;
                pq.push(node3(to,y,dis[to][y]));
            }
            if(y + 1 <= k && dis[x][y] < dis[to][y + 1]) {
                dis[to][y + 1] = dis[x][y];
                pq.push(node3(to,y + 1,dis[to][y + 1]));
            }
        }
    }
}
int main(void)
{
    int T,n,m;
    ll ans;
    scanf("%d",&T);
    while(T--) {
        init();
        scanf("%d %d %d",&n,&m,&k);
        for(int i = 1; i <= m; i++) {
            scanf("%d %d %lld",&e[i].u,&e[i].v,&e[i].w);
        }
        sort(e + 1,e + 1 + m,cmp);
        int preu = -1,prev = -1;
        for(int i = 1; i <= m; i++) {
            if(!(e[i].u == preu && e[i].v == prev)) {
                addedge(e[i].u,e[i].v,e[i].w);
                preu = e[i].u;
                prev = e[i].v;
            }
        }
        dijkstra();
        ans = 1e18;
        for(int i = 0; i <= k; i++) ans = min(ans,dis[n][i]);
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/82289927
今日推荐