题目描述
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;
}