POJ2449 Remmarguts' Date 第K短路
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&×==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短路的更多相关文章
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
- poj 2449 Remmarguts' Date (k短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
- poj 2449 Remmarguts' Date(K短路,A*算法)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013081425/article/details/26729375 http://poj.org/ ...
- POJ 2449 Remmarguts' Date ( 第 k 短路 && A*算法 )
题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include ...
- 【POJ】2449.Remmarguts' Date(K短路 n log n + k log k + m算法,非A*,论文算法)
题解 (搬运一个原来博客的论文题) 抱着板题的心情去,结果有大坑 就是S == T的时候也一定要走,++K 我发现按照论文写得\(O(n \log n + m + k \ log k)\)算法没有玄学 ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- poj2449 Remmarguts' Date K短路 A*
K短路裸题. #include <algorithm> #include <iostream> #include <cstring> #include <cs ...
随机推荐
- 09Java Server Pages 错误处理
Java Server Pages 错误处理 通常JSP在执行的时候,在两个阶段会发生错误.第一个是JSP网页转译成Servlet类的时候,另一个就是Servlet类处理每一个请求的时候.在第一个阶段 ...
- Oracle RAC 后台进程
LMS - Gobal 全局缓存服务进程 LMD - Global Enqueue Service Daemon 全局查询服务守护进程 LMON - 全局服务器监控进程 LCK0 ...
- 还没更换RubyGems镜像?
相信用过Ruby的人都知道 gem install 命令,但是在国内该命令安装的速度甚是不稳定(你懂的),导致尝试数次便是等待数时,记得之前在安装redmine的时候便是如此,之前不懂什么意思,还以为 ...
- [angular1.6]Error: "transition superseded" ui-router 在angular1.6 报错误问题解决
在angular1.6版本里,使用ui-router如果报这个错误,可以将ui-router升级到最近版本即可.ui-router version v0.4.2
- Luogu P1297 [国家集训队]单选错位
P1297 [国家集训队]单选错位 题目背景 原 <网线切割>请前往P1577 题目描述 gx和lc去参加noip初赛,其中有一种题型叫单项选择题,顾名思义,只有一个选项是正确答案.试卷上 ...
- Shell脚本中非交互式修改密码的方法(转)
这篇文章主要介绍了Shell脚本中非交互式修改密码的两种方法,本文讲解了使用chpasswd和使用passwd和--stdin组合两种方法,需要的朋友可以参考下. 对系统定期修改密码是一个很重要的安全 ...
- 截取命令cut命令、awk命令、sed命令
cut命令 截取以制表符tab为分隔符的第一列 cut -f 1test.txt 截取以":"为分隔符的第一列,如果比较规律的文件,可以自己设定分隔符 cut -f 1 -d ': ...
- Java:冒泡排序 | 二分查找
2018-10-29 20:16:46 冒泡排序 例子(对数字排序): 假设有这样一组数字:32, 8, 128, 2, 64 现在对其进行冒泡排序(*表示下次比较的开始数字): 32>8? t ...
- 【Codeforces 246D】Colorful Graph
[链接] 我是链接,点我呀:) [题意] 让你找到所有和x颜色的点中,和该颜色的点颜色不同的相邻的点的个数(重复颜色算一次) 求出哪种颜色的所要求的点的数量最多. [题解] 对于每一条边只会被查到两次 ...
- 神器的方块Magic Squares
题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...