题解

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

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

就是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. Spring Cloud微服务实战阅读笔记(一) 基础知识

    本文系<Spring Cloud微服务实战>作者:翟永超,一书的阅读笔记. 一:基础知识   1:什么是微服务架构     是一种架构设计风格,主旨是将一个原本独立的系统拆分成多个小型服务 ...

  2. vue 开发过程中遇到的问题

    1. gitlab团队协作开发 2. element ui 问题集锦 3. 使用vue和ElementUI快速开发后台管理系统

  3. COGS 513 八

    513. 八 http://www.cogs.pro/cogs/problem/problem.php?pid=513 ★☆   输入文件:eight.in   输出文件:eight.out   简单 ...

  4. Spring MVC处理响应的 header

    我们经常需要在HttpResponse中设置一些headers,我们使用Spring MVC框架的时候我们如何给Response设置Header呢? So easy, 看下面的代码: @Request ...

  5. Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location

    spring-boot项目,生产环境运行一段时间后,上传图片报错,如下: threw exception [Request processing failed; nested exception is ...

  6. LintCode 158: Anagram

    LintCode 158: Anagram 题目描述 写出一个函数anagram(s, t)判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 样例 给出s = "abcd" ...

  7. 截取汉字 mb_sbstr()

    一.中文截取:mb_substr() mb_substr( $str, $start, $length, $encoding ) $str,需要截断的字符串 $start,截断开始处,起始处为0 $l ...

  8. mysql修改表操作

    一: 修改表信息 1.修改表名 alter table test_a rename to sys_app; 2.修改表注释 alter table sys_application comment '系 ...

  9. Hive笔记之导出查询结果

    一.导出到本地 导出查询结果到本地: INSERT OVERWRITE LOCAL DIRECTORY "/tmp/hive-result/t_visit_video" SELEC ...

  10. python练习-Socket实现远程cmd命令

    需求:基于tcp的套接字实现远程执行命令的操作 代码示例: # 编辑者:闫龙 #Client端部分 import socket #导入骚凯特模块 CmdObj = socket.socket(sock ...