ACM-ICPC 2018 南京赛区网络预赛-L:Magical Girl Haze(DP+最短路)

10 篇文章 0 订阅

There are N N cities in the country, and M directional roads from u u to v(1u,vn). Every road has a distance ci c i . Haze is a Magical Girl that lives in City 1, she can choose no more than K K roads and make their distances become 0. Now she wants to go to City N, please help her calculate the minimum distance.

Input
The first line has one integer T(1T5) T ( 1 ≤ T ≤ 5 ) , then following T T cases.

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

Then the following M lines each line has three integers, describe a road, Ui,Vi,Ci. There might be multiple edges between u u and v.

It is guaranteed that N 100000,M200000,K10 ≤ 100000 , M ≤ 200000 , K ≤ 10 0Ci1e9 0 ≤ C i ≤ 1 e 9 . There is at least one path between City 1 and City N.

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

思路:DP+最短路。 d[i][j] d [ i ] [ j ] 表示到达第 i i 个城市,且已经使用了j次机会的最短距离。

然后其它的就和最短路一样求就行了。

#include<bits/stdc++.h>
const int MAX=2e5+10;
const int MOD=1e9+7;
using namespace std;
typedef long long ll;
vector<pair<int,int> >e[MAX];
struct lenka
{
    ll now,cost,t;
    bool operator<(const lenka& p)const{return p.cost<cost;}
};
priority_queue<lenka>p;
int Time;
ll d[MAX][14];
void bfs()
{
    d[1][0]=0;
    p.push({1,0,0});
    while(!p.empty())
    {
        lenka A=p.top();p.pop();
        if(A.cost!=d[A.now][A.t])continue;
        for(int i=0;i<e[A.now].size();i++)
        {
            pair<int,int>nex=e[A.now][i];
            if(d[nex.first][A.t]==-1||d[nex.first][A.t]>A.cost+nex.second)//不把这条边的权值变为0
            {
                d[nex.first][A.t]=A.cost+nex.second;
                p.push({nex.first,A.cost+nex.second,A.t});
            }
            if(A.t+1<=Time)
            {
                if(d[nex.first][A.t+1]==-1||d[nex.first][A.t+1]>A.cost)//把这条边的权值变为0
                {
                    d[nex.first][A.t+1]=A.cost;
                    p.push({nex.first,A.cost,A.t+1});
                }
            }
        }
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=n;i++)e[i].clear();
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            e[x].push_back({y,z});
        }
        Time=k;
        for(int i=1;i<=n;i++)
        for(int j=0;j<=Time;j++)d[i][j]=-1;
        bfs();
        ll ans=1e18;
        for(int i=0;i<=Time;i++)
        {
            if(d[n][i]!=-1)ans=min(ans,d[n][i]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值