题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=2763
题意:
题解:
最短路,在转移dis的时候多开一维k就好了
dis[i][j]->dis[t][j]+e[i][t]
dis[i][j]->dis[t][j+1] if j<k
代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define MS(a) memset(a,0,sizeof(a)) 5 #define MP make_pair 6 #define PB push_back 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){ 10 ll x=0,f=1;char ch=getchar(); 11 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 13 return x*f; 14 } 15 ////////////////////////////////////////////////////////////////////////// 16 const int maxn = 1e5+10; 17 18 struct node{ 19 int x,y; 20 }; 21 22 vector<node> E[maxn]; 23 ll dis[maxn][15]; 24 int inq[maxn][15]; 25 26 int main(){ 27 int n,m,k,s,t; 28 scanf("%d%d%d%d%d",&n,&m,&k,&s,&t); 29 for(int i=0; i<m; i++){ 30 int u,v,w; scanf("%d%d%d",&u,&v,&w); 31 E[u].push_back(node{v,w}); 32 E[v].push_back(node{u,w}); 33 } 34 35 for(int i=0; i<=n; i++) 36 for(int j=0; j<=k; j++) 37 dis[i][j] = INF; 38 memset(inq,0,sizeof(inq)); 39 queue<node> q; 40 q.push((node){s,0}); 41 inq[s][0] = 1, dis[s][0] = 0; 42 while(!q.empty()){ 43 node now = q.front(); 44 q.pop(); inq[now.x][now.y] = 0; 45 for(int i=0; i<(int)E[now.x].size(); i++){ 46 int v = E[now.x][i].x, w = E[now.x][i].y; 47 if(dis[v][now.y] > dis[now.x][now.y]+w){ // 不用票 48 dis[v][now.y] = dis[now.x][now.y]+w; 49 if(!inq[v][now.y]){ 50 inq[v][now.y] = 1; 51 q.push((node){v,now.y}); 52 } 53 } 54 if(dis[v][now.y+1]>dis[now.x][now.y] && now.y<k){ // 用票 55 dis[v][now.y+1] = dis[now.x][now.y]; 56 if(!inq[v][now.y+1]){ 57 inq[v][now.y+1] = 1; 58 q.push((node){v,now.y+1}); 59 } 60 } 61 } 62 } 63 64 ll ans = INF; 65 for(int i=0; i<=k; i++) 66 if(dis[t][i] < ans) 67 ans = dis[t][i]; 68 69 cout << ans << endl; 70 71 return 0; 72 }