题解

(搬运一个原来博客的论文题)

抱着板题的心情去,结果有大坑

就是S == T的时候也一定要走,++K

我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学A*快,不开心啊(或者我松教水平不高啊)

论文里主要是怎么样呢,把所有边反向,从T开始求最短路,然后求一个最短路树,求法就是把新边权改成

原来的边权 + 终点最短路 - 起点最短路

如果新边权是0,那么这条边就在最短路树里,如果有很多条边边权是0就随便选一条

然后我们对于每个点走一条不同于最短路的路径,发现是走过的非树边的边权加上S到T的最短路

我们记录每个点拓展出去的边都有哪些,拓展出去的边从小到大排序,一个点同时可以拓展它父亲可以拓展的边

这样我们可以用一个可持久化左偏树来维护,按照左儿子右兄弟的方法扩展,删掉一个点然后把左右儿子合起来选次大,或者再从当前点走一条非树边

不过删掉一个点把左右儿子加进去也可以

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <cmath>
#define MAXN 5005
#define PII pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64; int N,M,St,T,K,dis[MAXN];
vector<PII > G[MAXN],G_rev[MAXN];
vector<int> son[MAXN];
int fa[MAXN];
bool vis[MAXN],ct[MAXN];
PII line[200005];
struct cmp1 {
bool operator ()(const PII &a, const PII &b) {
return a.se > b.se;
}
}; struct Left_Tree {
Left_Tree *lc,*rc;
int v,to,dis;
}pool[8000005],*tail = pool;
vector<Left_Tree*> rt[MAXN];
struct node {
int s,t,cnt,v1,v0;
friend bool operator < (const node &a,const node &b) {
if(a.v1 != b.v1) return a.v1 < b.v1;
else if(a.v0 != b.v0) return a.v0 < b.v0;
else if(a.s != a.s) return a.s < b.s;
else return a.t < b.t;
}
};
multiset<node> S;
priority_queue<PII,vector<PII >,cmp1> Q;
Left_Tree *Newnode(PII k) {
Left_Tree *res = tail++;
res->v = k.se;res->to = k.fi;
res->lc = res->rc = NULL;
res->dis = 0;
return res;
}
int get_dist(Left_Tree *r) {
if(!r) return -1;
else return r->dis;
}
Left_Tree* Merge(Left_Tree *A,Left_Tree *B) {
if(!A) return B;
if(!B) return A;
if(A->v > B->v) swap(A,B);
Left_Tree *res = tail++;
*res = *A;
res->rc = Merge(A->rc,B);
if(get_dist(res->lc) < get_dist(res->rc)) swap(res->lc,res->rc);
res->dis = get_dist(res->rc) + 1;
return res;
}
Left_Tree* build(int u,int tot) {
if(u > tot) return NULL;
Left_Tree *res = Newnode(line[u]);
res->lc = build(u << 1,tot);
res->rc = build(u << 1 | 1,tot);
if(get_dist(res->lc) < get_dist(res->rc)) swap(res->lc,res->rc);
res->dis = get_dist(res->lc) + 1;
return res;
}
void dfs(int u) {
int tot = 0;
for(int i = 0 ; i < G[u].size() ; ++i) {
PII k = G[u][i];
if(ct[k.fi]) continue;
if(k.se != 0 || vis[u]) {
line[++tot] = k;
int t = tot;
while(t != 1) {
if(line[t].se < line[t >> 1].se) {
swap(line[t],line[t >> 1]);
t >>= 1;
}
else break;
}
}
else vis[u] = 1;
}
rt[u].pb(build(1,tot));
if(fa[u]) rt[u][0] = Merge(rt[u][0],rt[fa[u]][0]);
for(int i = 0 ; i < son[u].size() ; ++i) dfs(son[u][i]);
}
void Dijkstra() {
for(int i = 1 ; i <= N ; ++i) dis[i] = 0X7fffffff;
dis[T] = 0;
Q.push(mp(T,0));
while(!Q.empty()) {
PII u = Q.top();Q.pop();
if(vis[u.fi]) continue;
vis[u.fi] = 1;
for(int i = 0 ; i < G_rev[u.fi].size() ; ++i) {
PII k = G_rev[u.fi][i];
if(!vis[k.fi] && u.se + k.se < dis[k.fi]) {
dis[k.fi] = u.se + k.se;
Q.push(mp(k.fi,dis[k.fi]));
}
}
}
}
void Init() {
scanf("%d%d",&N,&M);
int s,t,c;
for(int i = 1 ; i <= M ; ++i) {
scanf("%d%d%d",&s,&t,&c);
G[s].pb(mp(t,c));
G_rev[t].pb(mp(s,c));
}
scanf("%d%d%d",&St,&T,&K);
Dijkstra();
for(int i = 1 ; i <= N ; ++i) {
if(dis[i] == 0x7fffffff) {ct[i] = 1;continue;}
for(int j = 0 ; j < G[i].size() ; ++j) {
G[i][j].se += dis[G[i][j].fi] - dis[i];
if(G[i][j].se == 0 && !fa[i]) {
fa[i] = G[i][j].fi;
son[fa[i]].pb(i);
}
}
}
memset(vis,0,sizeof(vis));
dfs(T);
}
void Solve() {
Init();
if(dis[St] == 0x7fffffff) {puts("-1");return;}
if(St == T) ++K;
if(K == 1) {printf("%d\n",dis[St]);return;}
if(rt[St][0]) {
S.insert((node){St,rt[St][0]->to,0,rt[St][0]->v,0});
}
int C = K - 2;
while(C && !S.empty()) {
C--;
node Now = *S.begin();
S.erase(S.begin());
while(rt[Now.s].size() <= Now.cnt + 1) {
int s = rt[Now.s].size() - 1;
rt[Now.s].pb(Merge(rt[Now.s][s]->lc,rt[Now.s][s]->rc));
}
if(rt[Now.s][Now.cnt + 1]) {
S.insert((node){Now.s,rt[Now.s][Now.cnt + 1]->to,Now.cnt + 1,Now.v0 + rt[Now.s][Now.cnt + 1]->v,Now.v0});
}
if(rt[Now.t][0]){
S.insert((node){Now.t,rt[Now.t][0]->to,0,Now.v1 + rt[Now.t][0]->v,Now.v1});
}
}
if(C || S.empty()) {
puts("-1");
}
else {
node Now = *S.begin();
printf("%d\n",Now.v1 + dis[St]);
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}

【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)的更多相关文章

  1. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  2. POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

  3. poj 2449 Remmarguts' Date (k短路模板)

    Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  4. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  5. 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  6. poj 2449 Remmarguts' Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  7. poj 2449 Remmarguts' Date(K短路,A*算法)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...

  8. K短路模板POJ 2449 Remmarguts' Date

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description &qu ...

  9. POJ 2449 Remmarguts' Date (K短路 A*算法)

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

  10. POJ 2449 Remmarguts' Date(第k短路のA*算法)

    Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...

随机推荐

  1. js原型解释图

  2. np.isin判断数组元素在另一数组中是否存在

    np.isin用法 觉得有用的话,欢迎一起讨论相互学习~Follow Me np.isin(a,b) 用于判定a中的元素在b中是否出现过,如果出现过返回True,否则返回False,最终结果为一个形状 ...

  3. python---web微信开发

    一:轮询,长轮询,WebSocket了解 轮询: 在前端,设置时间内,一直向后端发送请求.例如:使用setInterval方法设置定时器,一秒向后端发送一次请求,去主动获取数据,进行更新由于前端一直请 ...

  4. Ubuntu16.04.2安装Tensorflow

    安装aptitude $ sudo apt-get install aptitude 安装python-pip python-dev $ sudo aptitude install python-pi ...

  5. vscode nodejs智能提示功能

    1.依赖一些第三方的插件,先安装typings这个包,如果使用的是淘宝镜像,输入cnpm.cmd执行: cnpm i typings -g //cnpm install typings -global ...

  6. soj1036. Crypto Columns

    1036. Crypto Columns Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The columnar en ...

  7. bzoj 1044 [HAOI2008]木棍分割(二分+贪心,DP+优化)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1044 [题意] n根木棍拼到一起,最多可以切m刀,问切成后最大段的最小值及其方案数. ...

  8. 20155213 2016-2017-2 《Java程序设计》第七周学习总结

    20155213 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 Lambda 如果使用JDK8的话,可以使用Lambda特性去除重复的信息. 在只有Lamb ...

  9. 天梯赛 L2-022. (数组模拟链表) 重排链表

    题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...

  10. C/S模式和B/S模式

    C/S模式和B/S模式 1.C/S模式(Client/Server,客户机/服务器模式) 如QQ 暴风影音,PPlive等应用软件都是C/S模式 是一种软件系统结构的一种,C/S模式是基于企业内部网络 ...