第k短路和次短路模板

第k短路模板

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<map>
  7 #include<queue>
  8 #include<stack>
  9 #include<vector>
 10 #include<set>
 11 using namespace std;
 12 typedef long long ll;
 13 typedef pair<ll,int> P;
 14 const int maxn=2e5+100,maxm=2e5+100,inf=0x3f3f3f3f,mod=1e9+7;
 15 const ll INF=1e17+7;
 16 struct edge
 17 {
 18     int from,to;
 19     ll w;
 20 };
 21 vector<edge>G[maxn],T[maxn];
 22 priority_queue<P,vector<P>,greater<P> >q;
 23 ll dist[maxn];
 24 void addedge(int u,int v,ll w)
 25 {
 26     G[u].push_back((edge)
 27     {
 28         u,v,w
 29     });
 30     T[v].push_back((edge)
 31     {
 32         v,u,w
 33     });
 34 }
 35 void dij(int s)
 36 {
 37     dist[s]=0LL;
 38     q.push(P(dist[s],s));
 39     while(!q.empty())
 40     {
 41         P p=q.top();
 42         q.pop();
 43         int u=p.second;
 44         for(int i=0; i<T[u].size(); i++)
 45         {
 46             edge e=T[u][i];
 47             if(dist[e.to]>dist[u]+e.w)
 48             {
 49                 dist[e.to]=dist[u]+e.w;
 50                 q.push(P(dist[e.to],e.to));
 51             }
 52         }
 53     }
 54 }
 55 struct node
 56 {
 57     int to;
 58     ///g(p)为当前从s到p所走的路径的长度;dist[p]为点p到t的最短路的长度;
 59     ll g,f;///f=g+dist,f(p)的意义为从s按照当前路径走到p后再走到终点t一共至少要走多远;
 60     bool operator<(const node &x ) const
 61     {
 62         if(x.f==f) return x.g<g;
 63         return x.f<f;
 64     }
 65 };
 66 
 67 ll A_star(int s,int t,int k)
 68 {
 69     priority_queue<node>Q;
 70     if(dist[s]==INF) return -1;
 71     int cnt=0;
 72     if(s==t) k++;
 73     ll g=0LL;
 74     ll f=g+dist[s];
 75     Q.push((node)
 76     {
 77         s, g, f
 78     });
 79     while(!Q.empty())
 80     {
 81         node x=Q.top();
 82         Q.pop();
 83         int u=x.to;
 84         if(u==t) cnt++;
 85         if(cnt==k) return x.g;
 86         for(int i=0; i<G[u].size(); i++)
 87         {
 88             edge e=G[u][i];
 89             ll g=x.g+e.w;
 90             ll f=g+dist[e.to];
 91             Q.push((node)
 92             {
 93                 e.to, g, f
 94             });
 95         }
 96     }
 97     return -1;
 98 }
 99 void init(int n)
100 {
101     for(int i=0; i<=n+10; i++) G[i].clear(),T[i].clear();
102 }
103 int main()
104 {
105         int n,m;
106         scanf("%d%d",&n,&m);
107         for(int i=1; i<=m; i++)
108         {
109             int u,v;
110             ll w;
111             scanf("%d%d%lld",&u,&v,&w);
112             addedge(u,v,w);
113             addedge(v,u,w);
114         }
115         int s,t,k;
116         scanf("%d%d%d",&s,&t,&k);
117         for(int i=0; i<=n; i++) dist[i]=INF;
118         dij(t);
119         printf("%lld\n",A_star(s,t,k));//s到t的第k短路
120         init(n);
121         return 0;
122 }

次短路

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<map>
 7 #include<queue>
 8 #include<stack>
 9 #include<vector>
10 #include<set>
11 using namespace std;
12 typedef pair<int,int> P;
13 typedef long long ll;
14 const int maxn=2e5+100,inf=0x3f3f3f3f,mod=1e9+7;
15 const ll INF=1e15+7;
16 struct edge
17 {
18     int from,to;
19     ll cost;
20 };
21 vector<edge>es;
22 vector<int>G[maxn];
23 priority_queue<P,vector<P>,greater<P> >que;
24 void addedge(int u,int v,ll w)
25 {
26     es.push_back((edge)
27     {
28         u,v,w
29     });
30     es.push_back((edge)
31     {
32         v,u,w
33     });
34     int x=es.size();
35     G[u].push_back(x-2);
36     G[v].push_back(x-1);
37 }
38 ll dist[maxn],dist2[maxn];
39 int pre[maxn];
40 void dij(int s)
41 {
42     dist[s]=0LL;
43     que.push(P(0LL,s));
44     while(!que.empty())
45     {
46         P p=que.top();
47         que.pop();
48         int u=p.second;
49         ll d=p.first;
50         if(dist2[u]<d) continue;
51         for(int i=0; i<G[u].size(); i++)
52         {
53             edge e=es[G[u][i]];
54             ll w=d+e.cost;
55             if(dist[e.to]>w)
56             {
57                 swap(dist[e.to],w);
58                 pre[e.to]=G[u][i];
59                 que.push(P(dist[e.to],e.to));
60             }
61             if(dist2[e.to]>w&&dist[e.to]<w)
62             {
63                 dist2[e.to]=w;
64                 que.push(P(dist[e.to],e.to));
65             }
66         }
67     }
68 }
69 void init(int n)
70 {
71     es.clear();
72     for(int i=0; i<=n+10; i++) G[i].clear();
73 }
74 int main()
75 {
76     int T;
77     scanf("%d",&T);
78     while(T--)
79     {
80         int n,m;
81         scanf("%d%d",&n,&m);
82         for(int i=1; i<=m; i++)
83         {
84             int u,v;
85             ll w;
86             scanf("%d%d%lld",&u,&v,&w);
87             addedge(u,v,w);
88         }
89         for(int i=1; i<=n; i++) dist[i]=dist2[i]=INF;
90         dij(1);//起点是1
91         init(n);
92     }
93     return 0;
94 }

 

posted on 2017-08-24 19:29  hhhhx  阅读(137)  评论(0编辑  收藏  举报

导航