2019牛客暑期多校训练营(第四场) J 题 free

题目描述
Your are given an undirect connected graph.Every edge has a cost to pass.You should choose a path from S to T and you need to pay for all the edges in your path. However, you can choose at most k edges in the graph and change their costs to zero in the beginning. Please answer the minimal total cost you need to pay.
输入描述:
The first line contains five integers n,m,S,T,K.

For each of the following m lines, there are three integers a,b,l, meaning there is an edge that costs l between a and b.

n is the number of nodes and m is the number of edges.
输出描述:
An integer meaning the minimal total cost.
示例1
输入
3 2 1 3 1
1 2 1
2 3 2
输出
1
题意:就是给出各个边的长度,这是一个无向图,那么问使这m条边中选择最多K条边使其权值为0,那么从s到t点最小的权值是多少;

模板题,没有什么好说的 ,当做模板吧(这叫分层图!!!!!!)

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e5+100;
const ll INF=0x3f3f3f3f3f3f3f3f;
struct node{
    int from,to;ll val;
}num[N];
struct edge{
    int v,use;ll val;
//    bool operator < (const node & a) const{
//        return val>a.val;
//    }
};
bool operator < (edge a,edge b){
    return a.val>b.val;
}
int head[N],cnt,m,n,s,t,k;
ll dis[1010][1010];
bool vis[1010][1010];
void add(int from,int to,int val){
    num[cnt].from=head[from];
    num[cnt].to=to;
    num[cnt].val=val;
    head[from]=cnt++;
}
void di(){
    priority_queue<edge>p;
    memset(dis,INF,sizeof dis);
    p.push((edge){s,0,0});
    dis[s][0];
    vis[s][0]=1;
    while(!p.empty()){
        edge u=p.top();
        p.pop();
        for(int i=head[u.v];i!=-1;i=num[i].from){
            ll d=num[i].val;
            int to=num[i].to,use=u.use;
            if(!vis[to][use+1] && (use+1)<=k && dis[to][use+1]>u.val){
                vis[to][use+1]=1;
                dis[to][use+1]=u.val;
                p.push((edge){to,use+1,u.val});
            }
 
            if(!vis[to][use] && dis[to][use]>u.val+d){
                dis[to][use]=u.val+d;
                p.push((edge){to,use,dis[to][use]});
            }
        }
    }
}
int main(){
    memset(head,-1,sizeof head);
    scanf("%d%d%d%d%d",&n,&m,&s,&t,&k);
    for(int i=1;i<=m;i++){int u,v,val;
        scanf("%d%d%d",&u,&v,&val);
        add(u,v,val);
        add(v,u,val);
    }
    di();
    ll ans=INF;
    for(int i=0;i<=k;i++){
        ans=min(dis[t][i],ans);
//        cout<<dis[t][i]<<" ";
    }
//    cout<<endl;
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值