计蒜客 Magical Girl Haze (2018 ICPC亚洲区域赛网络赛 南京 L)(迪杰斯特拉)

版权声明:Why is everything so heavy? https://blog.csdn.net/lzc504603913/article/details/82290967

题目链接:https://nanti.jisuanke.com/t/31001

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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

题意:给你一个图,求1~N的最短路,你可以把其中K条边的权值变为0

解题思路:拆点,把每个点拆成,当前点用了j次变0魔法的的点。然后连边。再跑最短路即可。即

u[0] ----w----v[0]

u[1]-----w----v[1]

u[2] ----w----v[2]

u[3]-----w----v[3]

......

u[0] ----0----v[1]

u[1] ----0----v[2]

u[2] ----0----v[3]

把这些边建起来,然后跑最短路最好了。

这题卡SPFA,所以用堆优化迪杰斯特拉

#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
const int MAXN=5000005;
const ll INF=1e18;

struct edge{
    int u;
    int v;
    int w;
    int next;
}e[MAXN];
int edge_num=0;
int head[MAXN];
void insert_edge(int u,int v,int w){
    e[edge_num].u=u;
    e[edge_num].v=v;
    e[edge_num].w=w;
    e[edge_num].next=head[u];
    head[u]=edge_num++;
}

struct node{
    int v;
    ll dis;
    node (ll a,int b):dis(a),v(b){};
    bool operator <(const node &rhs)const
    {
        return dis > rhs.dis;
    }
};

int N,M,K;
bool vis[MAXN];
ll d[MAXN];

void dijkstra(int S){
    priority_queue<node> que;
    memset(d,0x3f,sizeof(d));
    memset(vis,0,sizeof(vis));
    d[S]=0;
    que.push(node(d[S],S));
    while(!que.empty()){
        node tp=que.top();
        que.pop();
        int v=tp.v;
        if(vis[v])
            continue;
        vis[v]=1;
        for(int i=head[v];~i;i=e[i].next){
            if(d[e[i].v]>d[v]+e[i].w){
                d[e[i].v]=d[v]+e[i].w;
                que.push(node(d[e[i].v],e[i].v));
            }
        }
    }
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&N,&M,&K);
        edge_num=0;
        memset(head,-1,sizeof(head));
        int u,v,w;
        for(int i=0;i<M;i++){
            scanf("%d%d%d",&u,&v,&w);
            u--;v--;
            for(int j=0;j<=K;j++){
                insert_edge(u*(K+1)+j,v*(K+1)+j,w);
                if(j<K)
                    insert_edge(u*(K+1)+j,v*(K+1)+j+1,0);
            }
        }
        dijkstra(0);
        ll ans=INF;
        for(int i=0;i<=K;i++)
            ans=min(ans,d[(N-1)*(K+1)+i]);
        printf("%lld\n",ans);

    }
}

猜你喜欢

转载自blog.csdn.net/lzc504603913/article/details/82290967