ACM-ICPC 2018 南京赛区网络预赛- L. 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+1个点0表示其本身,到达每一个点时先算出在同一水平nu下的最短路径,nu代表其前面已经有多少条路径被设置为0,假如nu<k更新上一个点到这个点的nu+1个点的最短路径; 最后输出dist[n][k]就是最短路径了;

(在此多谢kuangbin大佬的提示)


#include<cstring>
#include<cstdio>
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<functional>
using namespace std;
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define ps(x) push(x)
#define clr(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f3f3f3f3f3f3f
#define MAX_N 100000+5
#define MAX_M 13
typedef long long ll;
const ll INF=10000000000000000LL;
typedef priority_queue<int,vector<int>,less<int> > pql;
typedef priority_queue<int,vector<int>,greater<int> >pqg;
struct Point//拆分的点
{
    int v,nu;
    long long d;
    Point(){}
    Point(int v,int nu,int d):v(v),nu(nu),d(d){}
	bool operator <(const Point &r)const{
		return d>r.d;
	}
};
struct Edge//边
{
    int to,cost;
    Edge(){}
    Edge(int to,int cost):to(to),cost(cost){}
};
int n,m,k,vis[MAX_N][MAX_M];
ll dist[MAX_N][MAX_M];
vector<Edge> ve[MAX_N];
priority_queue<Point>que;
ll dij(int n,int sta,int k)
{
    clr(vis,0);
    clr(dist,inf);
    dist[sta][0]=0;
    que.ps(Point(sta,0,0));
    Point cur;
    while(!que.empty())
    {
        cur=que.top();que.pop();
        int v=cur.v;
        int nu=cur.nu;
        if(vis[v][nu]){continue;}
        vis[v][nu]=1;
        for(int i=0;i<ve[v].size();i++)
        {
            int to=ve[v][i].to;
            int cost=ve[v][i].cost;
            if(!vis[to][nu]&&dist[to][nu]>dist[v][nu]+cost)//判断同一水平下得最短路径
            {
                dist[to][nu]=dist[v][nu]+cost;
                que.ps(Point(to,nu,dist[to][nu]));
            }
            if(nu<k&&!vis[to][nu+1]&&dist[to][nu+1]>dist[v][nu])//在此将这条边设为0的最短距离
            {
                dist[to][nu+1]=dist[v][nu];
                que.ps(Point(to,nu+1,dist[to][nu+1]));
            }
        }
    }
    return dist[n][k];
}
int main()
{
   // freopen("data.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        clr(ve,0);
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0;i<m;i++)
        {
           int v,u,cost;
           scanf("%d%d%d",&v,&u,&cost);
           ve[v].pb(Edge(u,cost));
        }
        printf("%lld\n",dij(n,1,k));
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值