Remmarguts' Date(POJ-2449)(可持久化堆解决)
Problem 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 2Sample Output
14
题意:给出 n 个点 m 条边的有向图,再给出一个起点 S、终点 T 与一个值 k,询问从 S 到 T 的第 k 条路最短路径
思路:k 短路模板题,利用 SPFA+可持久化堆 即可解决
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<strinG>
#include<cstrinG>
#include<cmath>
#include<ctime>
#include<alGorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL GetInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 1500+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;
struct Node{
int val,to;
Node *left,*right;
Node(){}
Node(int val, int to, Node *left, Node *right) : val(val), to(to), left(left), right(right) {}
};
#define Limit 1000000
Node pool[Limit];
Node *top = pool;
Node *newNode(int val, int to) {
if (top >= pool + Limit)
return new Node(val, to, NULL, NULL);
top->val = val;
top->to = to;
top->left = NULL;
top->right = NULL;
return top++;
}
Node *meGTe(Node *a, Node *b) {
if (!a)
return b;
if (!b)
return a;
if (a->val > b->val)
swap(a, b);
Node *p = newNode(a->val, a->to);
p->left = a->left;
p->right = a->right;
p->right = meGTe(p->right, b);
swap(p->left, p->right);
return p;
}
struct Status {
int dist;
Node *p;
Status(){}
Status(int dist, Node *p) : dist(dist), p(p) {}
bool operator<(Status b) const { return dist > b.dist; }
};
struct Edge {
int to, next;
int w;
Edge() {}
Edge(int to, int next, int w) : to(to), next(next), w(w) {}
};
struct Map {
int tot;
int *head;
Edge *edge;
Map() {}
Map(int n, int m) : tot(0) {
head = new int[(n + 1)];
edge = new Edge[(m + 5)];
memset(head, 0, sizeof(int) * (n + 1));
}
void addEdge(int x, int y, int w) {
edge[++tot].to = y;
edge[tot].next = head[x];
edge[tot].w = w;
head[x] = tot;
}
Edge &operator[](int pos) { return edge[pos]; }
};
int n, m;
int s, t, k;
Map G, GT;
bool *vis;
int *dis, *pre;
queue<int> SPFA(int S) {
vis = new bool[(n + 1)];
dis = new int[(n + 1)];
pre = new int[(n + 1)];
memset(dis, INF, sizeof(int) * (n + 1));
memset(vis, false, sizeof(bool) * (n + 1));
queue<int> Q;
Q.push(S);
dis[S] = 0;
pre[S] = 0;
while (!Q.empty()) {
int x = Q.front();
Q.pop();
vis[x] = false;
for (int i = GT.head[x]; i; i = GT[i].next) {
int y = GT[i].to;
int w = GT[i].w;
if (dis[x] + w < dis[y]) {
dis[y] = dis[x] + w;
pre[y] = i;
if (!vis[y]) {
vis[y] = true;
Q.push(y);
}
}
}
}
return Q;
}
Node **Hash;
void rebuild(queue<int> Q) { //建堆
for (int i = 1; i <= n; i++) {
for (int j = G.head[i]; j; j = G[j].next) {
int to = G[j].to;
if (pre[i] != j)
G[j].w += dis[to] - dis[i];
}
}
Hash = new Node *[(n + 1)];
Q.push(t);
Hash[t] = NULL;
while (!Q.empty()) {
int x = Q.front();
Q.pop();
if (pre[x])
Hash[x] = Hash[G[pre[x]].to];
for (int i = G.head[x]; i; i = G[i].next)
if (pre[x] != i && dis[G[i].to] != INF)
Hash[x] = meGTe(Hash[x], new Node(G[i].w, G[i].to, NULL, NULL));
for (int i = GT.head[x]; i; i = GT[i].next) {
int y = GT[i].to;
if (pre[y] == i)
Q.push(y);
}
}
}
int kthPath(int k) {
if (s == t)
k++;
if (dis[s] == INF)
return -1;
if (k == 1)
return dis[s];
priority_queue<Status> Q;
if (!Hash[s])
return -1;
Q.push(Status(Hash[s]->val, Hash[s]));
while (--k && !Q.empty()) {
Status x = Q.top();
Q.pop();
if (k == 1)
return x.dist + dis[s];
int y = x.p->to;
if (Hash[y])
Q.push(Status(x.dist + Hash[y]->val, Hash[y]));
if (x.p->left)
Q.push(Status(x.dist - x.p->val + x.p->left->val, x.p->left));
if (x.p->right)
Q.push(Status(x.dist - x.p->val + x.p->right->val, x.p->right));
}
return -1;
}
int main() {
scanf("%d%d", &n, &m);
G = Map(n, m);
GT = Map(n, m);
for (int i = 1, u, v, w; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
G.addEdge(u, v, w);
GT.addEdge(v, u, w);
}
scanf("%d%d%d", &s, &t, &k);
queue<int> Q = SPFA(t);
rebuild(Q);
printf("%d\n", kthPath(k));
return 0;
}