1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<vector>
using namespace std; typedef long long ll; #define endl '\n' #define Turnoff std::ios::sync_with_stdio(false) #define P pair<int,int> const ll Max=1e3+5; const int Mod=998244353; const int inf=0x3f3f3f3f;
vector<P>mp[Max],invmp[Max]; bool vis[Max]; int n,m; int disT[Max],vistime[Max]; void Dijkstra(int start){ for(int i=0;i<=n;i++)disT[i]=inf; priority_queue<P,vector<P>,less<P> >q; disT[start]=0; q.push({0,start}); while(!q.empty()){ P temp=q.top();q.pop(); int now=temp.second; if(vis[now])continue; vis[now]=1; for(auto edge:invmp[now]){ int cost=edge.second,to=edge.first; if(disT[now]+cost<disT[to]){ disT[to]=disT[now]+cost; q.push({disT[to],to}); } } } } int Astar(int start,int endpoint,int k){ if(disT[start]==inf)return -1; priority_queue<P,vector<P>,greater<P> >q; q.push({disT[start],start}); while(!q.empty()){ P temp=q.top();q.pop(); int now=temp.second,ExpectCost=temp.first; vistime[now]++; if(vistime[now]==k&&now==endpoint)return ExpectCost; if(vistime[now]>k)continue; for(auto edge:mp[now]){ int Cost=edge.second,to=edge.first; q.push({ExpectCost-disT[now]+Cost+disT[to],to}); } } return -1; } int main() { Turnoff; cin>>n>>m; for(int i=1;i<=m;i++){ int u,v,val; cin>>u>>v>>val; mp[u].push_back({v,val}); invmp[v].push_back({u,val}); } int S,T,K;cin>>S>>T>>K; Dijkstra(T); int ans=Astar(S,T,K); cout<<ans<<endl; }
|