POJ2449 比较裸的K短路问题

K短路听起来高大上 实际思路并不复杂

首先对终点t到其他所有点求最短路 即为dist[]

然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即为第K短路

代码也很简单

//数组开的不够 不一定是运行时错误! 可能也会WA
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long int LL;
const int maxn=(1e+3)+1,maxm=(1e+5)+1;
vector<int > son[maxn];vector<int >fa[maxn];
int ea[maxm],eb[maxm],len[maxm];
LL dist[maxn];
bool vis[maxn];
typedef pair<int,int>po;
const LL INF=1e+18;
priority_queue<po,vector<po>,greater<po> >q;
int times;
po p[maxn];
struct pi
{
LL f;
LL dis;
int th;
pi(LL a,LL b,int c)
{
f=a;
dis=b;
th=c;
}
bool operator<(const pi b)const
{
if(f==b.f)return dis>b.dis;
return f>b.f;
}
};
priority_queue<pi>Q;
int main()
{freopen("t.txt","r",stdin);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{son[i].clear();fa[i].clear();}
for(int i=1;i<=m;i++)
{
int a,b,l;
scanf("%d%d%d",&a,&b,&l);
ea[i]=a;eb[i]=b;len[i]=l;
son[a].push_back(i);fa[b].push_back(i);
}
int s,t,k;
scanf("%d%d%d",&s,&t,&k);
for(int i=1;i<=n;i++)
{dist[i]=INF;}
while(!q.empty())q.pop();
dist[t]=0;
for(int i=1;i<=n;i++)
p[i]=po(dist[i],i);
q.push(p[t]);
memset(vis,0,sizeof(vis));
while(!q.empty())
{
int mark;
po now=q.top();
q.pop();
mark=now.second;
if(vis[mark])continue;
vis[mark]=true;
for(int i=0;i<fa[mark].size();i++)
{
int e=fa[mark][i];
int father=ea[e];
if(dist[father]>(dist[mark]+len[e]))
{dist[father]=dist[mark]+len[e];po np(dist[father],father);q.push(np);} }
}
while(!Q.empty())Q.pop();
pi start(dist[s],0,s);
Q.push(start);
LL ans=-1;
if(s==t)k++;
while(!Q.empty())
{
if(dist[s]==INF)break;
pi now=Q.top();
Q.pop();
int npo=now.th;
if(npo==t) times++;
if(npo==t&&times==k){ans=now.dis;break;}
for(int i=0;i<son[npo].size();i++)
{
int e=son[npo][i];
int so=eb[e];
pi ps(now.dis+len[e]+dist[so],now.dis+len[e],so);
Q.push(ps);
}
}
cout<<ans<<endl;
return 0;
}

  

POJ2449 Remmarguts' Date 第K短路的更多相关文章

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

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

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

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

  3. 【POJ】2449 Remmarguts' Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

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

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

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

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

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

    题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...

  7. 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)

    题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...

  8. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  9. poj2449 Remmarguts' Date K短路 A*

    K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...

随机推荐

  1. 如何使用fio模拟线上环境

    线上表现 这里我想通过fio来模拟线上的IO场景,那么如何模拟呢? 首先使用iostat看线上某个盘的 使用情况,这里我们需要关注的是 avgrq-sz, avgrq-qz. #iostat -dx ...

  2. HTML5 实现Link跳线效果

    之前我们推出过Flex版本的Link跳线效果,现在基于HTML5新版本的跳线效果也实现了,细微之处我们进行了改进,如link倾斜的时候Offset方向始终保持垂直等.先看效果.实现的算法和Flex基本 ...

  3. 暴力搜索+散列--P1008 三连击

    题目描述 将1,2, ⋯,9共9个数分成3组,分别组成3个三位数,且使这3个三位数构成1:2:3的比例,试求出所有满足条件的3个三位数. 输入输出格式 输入格式: 木有输入 输出格式: 若干行,每行3 ...

  4. <MySQL>入门二 增删改 DML

    -- DML语言 /* 数据操作的语言 插入:insert 修改:update 删除:delete */ 1.插入 -- 插入语句 /* 语法:insert into 表名(列名...) values ...

  5. Linux之iptables(五、firewall命令及配置)

    firewalld服务 firewalld是CentOS 7.0新推出的管理netfilter的工具 firewalld是配置和监控防火墙规则的系统守护进程.可以实现iptables,ip6table ...

  6. for 循环新的写法==列表解析

    1. (for x in L1) 是一个可迭代对象: 2. 列表解析比for 循环快,列表解析的迭代在解释器内部是以C语言速度执行, 而不是手动python代码执行: (x+10 for x in L ...

  7. uva 1444 Knowledge for the masses

    uva 1444 Description   You are in a library equipped with bookracks that move on rails. There are ma ...

  8. Hashing - Hard Version

    Hashing - Hard Version Given a hash table of size N, we can define a hash function . Suppose that th ...

  9. IIS301重定向:将不带www的域名跳转到带www上

    首先你的域名有这两条解析记录 进入服务器IIS,添加2个站点,如下图 第一个正常绑定你的域名:www.baidu.com 第二个绑定不带www的域名:baidu.com 然后点开ncgd-no-www ...

  10. 浅谈href=与href=javascript_void(0)的区别

    "#"包含了一个位置信息.默认的锚点是#top 也就是网页的顶端.而javascript:void(0)  仅仅表示一个死链接,这就是为什么有的时候页面很长,浏览链接明明是#可是跳 ...