NC51028. 第k短路
描述
输入描述
The first line contains two integer numbers N and M . Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T. It shows that there is a directed sideway from A-th station to B-th station with time T.
The last line consists of three integer numbers S, T and K .
输出描述
A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.
示例1
输入:
2 2 1 2 5 2 1 4 1 2 2
输出:
14
C++14(g++5.4) 解法, 执行用时: 25ms, 内存消耗: 9140K, 提交时间: 2019-11-24 16:00:55
#include<bits/stdc++.h> using namespace std; #define int long long const int maxn=1005; int n,m,s,t,k,dist[maxn],vis[maxn]; vector<pair<int,int> > G[maxn],_G[maxn]; void Dijkstra(int s) { memset(vis,0,sizeof(vis)); memset(dist,0x3F,sizeof(dist)); priority_queue<pair<int,int> > q; q.push(make_pair(0,s)); dist[s]=0; while(!q.empty()) { int u=q.top().second; q.pop(); if(vis[u]) continue; vis[u]=1; for(int i=0;i<(int)_G[u].size();i++) { int v=_G[u][i].first,w=_G[u][i].second; if(dist[v]>dist[u]+w) { dist[v]=dist[u]+w; q.push(make_pair(-dist[v],v)); } } } } int A_Sharp(int s,int t,int k) { if(s==t) k++; memset(vis,0,sizeof(vis)); priority_queue<pair<int,int> > q; q.push(make_pair(-dist[s],s)); while(!q.empty()) { int u=q.top().second,tmp=q.top().first+dist[u]; q.pop(); vis[u]++; //printf("%d %d %d\n",u,vis[u],-tmp); if(u==t&&vis[u]==k) return -tmp; for(int i=0;i<(int)G[u].size();i++) { int v=G[u][i].first,w=-G[u][i].second; q.push(make_pair(tmp+w-dist[v],v)); } } return -1; } signed main() { scanf("%lld%lld",&n,&m); for(int i=1;i<=m;i++) { int a,b,t; scanf("%lld%lld%lld",&a,&b,&t); G[a].push_back(make_pair(b,t)); _G[b].push_back(make_pair(a,t)); } scanf("%lld%lld%lld",&s,&t,&k); Dijkstra(t); printf("%lld\n",A_Sharp(s,t,k)); return 0; }
C++(g++ 7.5.0) 解法, 执行用时: 27ms, 内存消耗: 4928K, 提交时间: 2022-12-09 10:21:29
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include <time.h> #include <sstream> #define pii pair<int,int> #define mp make_pair using namespace std; int n,m,s,t,k,x,y,z,dis[2000],vis[2000],ans,cnt; vector<pii>ed[2000],e[2000]; priority_queue<pii,vector<pii>,greater<pii>>q; int main(){ cin>>n>>m; for(int i=1;i<=m;i++){ cin>>x>>y>>z; ed[x].push_back(mp(y,z)); e[y].push_back(mp(x,z)); } cin>>s>>t>>k; memset(dis,0x3f,sizeof(dis)); dis[t]=0; q.push(mp(0,t)); while(!q.empty()){ int p=q.top().second; q.pop(); if(vis[p])continue; vis[p]=1; for(int i=0;i<e[p].size();i++){ pii a=e[p][i]; if(dis[a.first]>dis[p]+a.second){ dis[a.first]=dis[p]+a.second; q.push(mp(dis[a.first],a.first)); } } } if(dis[s]>1e9){ cout<<-1<<endl; return 0; } q.push(mp(dis[s],s)); while(!q.empty()){ pii p=q.top(); q.pop(); if(p.second==t&&p.first)cnt++; if(cnt==k){ cout<<p.first<<endl; return 0; } for(int i=0;i<ed[p.second].size();i++){ pii a=ed[p.second][i]; q.push(mp(p.first-dis[p.second]+a.second+dis[a.first],a.first)); } } cout<<-1<<endl; }
C++ 解法, 执行用时: 15ms, 内存消耗: 4860K, 提交时间: 2021-09-13 15:32:41
#include<bits/stdc++.h> using namespace std; const int N=1e3+5; int n,m,s,t,k,x,y,z,f[N],c[N]; bool v[N]; vector<pair<int,int> >e[N],re[N]; priority_queue<pair<int,int> >q; void dij(){ memset(f,0x3f,sizeof(f)); f[t]=0,q.push(make_pair(0,t)); while(q.size()){ int x=q.top().second;q.pop(); if(v[x]) continue; v[x]=1; for(pair<int,int> i:re[x]){ int y=i.first,z=i.second; if(f[y]>f[x]+z) f[y]=f[x]+z,q.push(make_pair(-f[y],y)); } } } int A_star(){ if(s==t) k++; q.push(make_pair(-f[s],s)); while(q.size()){ int x=q.top().second,dis=-q.top().first-f[x]; c[x]++,q.pop(); if(x==t&&c[x]==k) return dis; for(pair<int,int> i:e[x]){ int y=i.first,z=i.second; if(c[y]<k) q.push(make_pair(-dis-z-f[y],y)); } } return -1; } signed main(){ scanf("%d%d",&n,&m); for(int i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&z); e[x].push_back(make_pair(y,z)); re[y].push_back(make_pair(x,z)); } scanf("%d%d%d",&s,&t,&k); dij(),printf("%d\n",A_star()); return 0; }