# 求次短路和k短路（模板）

xiaoxiao2021-04-05  41

Dijkstra的dist数组和vis数组再加一维，松弛的时候讨论当前的路小于最短路，或者大于最短路但小于次短路这两种情况，就能维护一个次短路了

#include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> using namespace std; const int maxn = 1000 + 5; const int INF = 0x3f3f3f3f; struct Node { int v, c, flag; Node (int _v = 0, int _c = 0, int _flag = 0) : v(_v), c(_c), flag(_flag) {} bool operator < (const Node &rhs) const { return c > rhs.c; } }; struct Edge { int v, cost; Edge (int _v = 0, int _cost = 0) : v(_v), cost(_cost) {} }; vector<Edge>E[maxn]; bool vis[maxn]; int dist[maxn]; void Dijkstra(int n, int s) { memset(vis, false, sizeof(vis)); for (int i = 1; i <= n; i++) { dist[i] = INF; dist[i] = INF; } priority_queue<Node>que; dist[s] = 0; que.push(Node(s, 0, 0)); while (!que.empty()) { Node tep = que.top(); que.pop(); int u = tep.v; int flag = tep.flag; if (vis[u][flag]) continue; vis[u][flag] = true; for (int i = 0; i < (int)E[u].size(); i++) { int v = E[u][i].v; int cost = E[u][i].cost; if (!vis[v] && dist[v] > dist[u][flag] + cost) { dist[v] = dist[v]; dist[v] = dist[u][flag] + cost; que.push(Node(v, dist[v], 0)); que.push(Node(v, dist[v], 1)); } else if (!vis[v] && dist[v] > dist[u][flag] + cost) { dist[v] = dist[u][flag] + cost; que.push(Node(v, dist[v], 1)); } } } } void addedge(int u, int v, int w) { E[u].push_back(Edge(v, w)); } int main() { //freopen("in.txt", "r", stdin); int n, m, v, w; while (scanf("%d", &n) != EOF) { for (int i = 0; i <= n; i++) E[i].clear(); for (int u = 1; u <= n; u++) { scanf("%d", &m); for (int j = 0; j < m; j++) { scanf("%d%d", &v, &w); addedge(u, v, w); } } Dijkstra(n, 1); printf("%d\n", dist[n]); } return 0; }

K短路：

Remmarguts' Date

Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 37898 Accepted: 10439

Description

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. "Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission." "Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)" Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help! DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate.

Input

The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). 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 (1 <= S, T <= N, 1 <= K <= 1000).

Output

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.

Sample Input

2 2 1 2 5 2 1 4 1 2 2

Sample Output

14

Source

POJ Monthly,Zeyuan Zhu

K短路的思想没有懂欸，先贴个模板吧。

#include<bits/stdc++.h> using namespace std; #define Maxn 10010 #define INF 1000000000 struct node{ int to,val; node() {} node(int a,int b) { to = a; val = b; } }; vector<node> adj[Maxn],_adj[Maxn]; int n,m,k; bool vis[Maxn]; int dis[Maxn]; void AddEdge(int x,int y,int val) { adj[x].push_back(node(y,val)); _adj[y].push_back(node(x,val));//把图反向 } void Dijkstra(int s,int t) { priority_queue<int , vector<int> , greater<int> > q; while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) vis[i] = false,dis[i] = INF; vis[t] = true; dis[t] = 0; q.push(t); int u,len; while(!q.empty()) { u = q.top(); q.pop(); len = _adj[u].size(); for(int i=0;i<len;i++) { node v = _adj[u][i]; if(dis[v.to] > dis[u] + v.val) { dis[v.to] = dis[u] + v.val; if(!vis[v.to]) { q.push(v.to); vis[v.to] = true; } } } vis[u] = false; } } struct Anode{ int h,g,id; Anode(int a,int b,int c) {h=a; g=b; id=c;} bool operator < (Anode a) const { return h+g > a.h+a.g; } }; priority_queue<Anode> Q; int Astar(int s,int t)//A*算法过程 { while(!Q.empty()) Q.pop(); Q.push(Anode(0,dis[s],s)); int len,num; num = 0; while(!Q.empty()) { Anode u = Q.top(); Q.pop(); if(u.id==t) ++num; if(num>=k) return u.h; len = adj[u.id].size(); for(int i=0;i<len;i++) { node v = adj[u.id][i]; Q.push(Anode(u.h+v.val,dis[v.to],v.to)); } } return -1;//不能连通或者没有第K短路 } int main() { while(scanf("%d%d",&n,&m)!=-1) { for(int i=0;i<Maxn;i++) adj[i].clear(),_adj[i].clear(); int x,y,v,s,t; for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&v); AddEdge(x,y,v); } scanf("%d%d%d",&s,&t,&k); if(s==t) k++; Dijkstra(s,t); printf("%d\n",Astar(s,t)); } return 0; }

Processed: 0.018, SQL: 10