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

1 篇文章 0 订阅
1 篇文章 0 订阅

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

题目来源

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

题意:给你一个n个点,m条边的图,让你最多把k条边的边权变为0。求从1到n的最短路。

题解:感觉OI退役以后智商就不断下降了QAQ。还以为是什么dp之类的。k<=10,直接建分层图呀QAQ。每一层是正常的图,如果要把一条边边权变为0,就直接从当前层走到下一层就行了,层与层之间全为边权为0的边。

代码:

#include<bits/stdc++.h>
#define pi pair<LL,int>
#define mp make_pair
#define fir first
#define sec second
using namespace std;
#define LL long long
#define INF 1e15
int NN;
struct node
{
    int begin,end,value,next;
}edge[4201010];
int U[200010],V[200010],C[200010],cnt,Head[1101010];
LL dis[1101010];
priority_queue<pi,vector<pi>,greater<pi> > q;
int read()
{
    int s=0,fh=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')fh=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){s=s*10+(ch-'0');ch=getchar();}
    return s*fh;
}
void addedge(int bb,int ee,int vv)
{
    edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].value=vv;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
void dijkstra(int start)
{
    int i,u,v;
    for(i=1;i<=NN;i++)dis[i]=INF;dis[start]=0LL;
    q.push(mp(dis[start],start));
    while(!q.empty())
    {
        u=q.top().sec;q.pop();
        for(i=Head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].end;
            if(dis[v]>dis[u]+1LL*edge[i].value)
            {
                dis[v]=dis[u]+1LL*edge[i].value;
                q.push(mp(dis[v],v));
            }
        }
    }
}
int main()
{
    int T,N,M,K,j,i;
    LL ans;
    T=read();
    while(T--)
    {
        N=read();M=read();K=read();
        memset(Head,-1,sizeof(Head));cnt=1;
        for(i=1;i<=M;i++){U[i]=read();V[i]=read();C[i]=read();}
        for(j=0;j<=K;j++)
        {
            for(i=1;i<=M;i++)
            {
                addedge(U[i]+j*N,V[i]+j*N,C[i]);
            }
        }
        for(j=0;j<K;j++)
        {
            for(i=1;i<=M;i++)addedge(U[i]+j*N,V[i]+(j+1)*N,0);
        }
        NN=(K+1)*N;
        dijkstra(1);
        ans=INF;
        for(i=0;i<=K;i++)ans=min(ans,dis[(i+1)*N]);
        printf("%lld\n",ans);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值