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 ...
随机推荐
- ThinkPHP---thinkphp文件加载
[一]文件加载在ThinkPHP里提供了三种方式 实际开发里,文件加载方式一般以第一种为主(通过函数库形式自动加载,此时我们仅仅需要定义文件和函数) (1)函数库形式加载 函数库分3种级别,系统函数库 ...
- 小程序 textarea ios兼容解决
今天遇到,在小程序里textarea会存在一定的兼容性问题,textarea有默认的内边距,在安卓和ios显示的时候,ios边距会比安卓的大很多. 解决办法: 通过 wx.getSystemInfoS ...
- Java写时复制CopyOnWriteArrayList
Copy-On-Write是一种程序设计的优化方法,多线程在不修改对象时可以共享一个对象地址空间,如果某一个线程要求修改对象时,需要首先将原来对象复制一份,在新复制的对象地址空间上修改对象内容,其他线 ...
- LearnPython笔记:ex48 代码
赶紧写上 ,一定有人着急要看,啊哈哈哈哈,嘻嘻 哈哈 不枉我起了个大早 利用什么碎片时间啊,真正能深入学习的,是需要大段大段不被打断的时间 1. 完全实现了如下几种输入数据: 2. 遗留:最后一个el ...
- LINUX-文件的特殊属性 - 使用 "+" 设置权限,使用 "-" 用于取消
chattr +a file1 只允许以追加方式读写文件 chattr +c file1 允许这个文件能被内核自动压缩/解压 chattr +d file1 在进行文件系统备份时,dump程序将忽略这 ...
- DAS、NAS、SAN、iSCSI 存储方案概述
(转自https://blog.csdn.net/tianlesoftware/article/details/6201346) 目前服务器所使用的专业存储方案有DAS.NAS.SAN.iSCSI几种 ...
- Python-函数和代码复用
函数的定义与使用 >函数的理解与定义 函数是一段代码的表示 -函数是一段具有特定功能的.可重用的语句组 -函数是一种功能的抽象,一般函数表达特定功能 -两个作用:降低编程难度 和 代码复用 de ...
- 电商架构设计-通过系统和业务拆分,遵循单一职责原则SRP,保障整个系统的可用性和稳定性
个人观察 1.通过系统和业务拆分,遵循单一职责原则SRP,保障整个系统的可用性和稳定性. 2.单一职责原则SRP,真的很关键,广大程序员需要不断深入理解这个原则. 3.架构图是架构师的重要输出,通过图 ...
- 【页面传值6种方式】- 【JSP 页面传值方法总结:4种】 - 【跨页面传值的几种简单方式3种】
阅读目录 1. URL 链接后追加参数 2. Form 3. 设置 Cookie 4. 设置 Session JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧. 试着将各种方式总 ...
- Code(poj 17801)
求出一个长度为10^n+n-1的序列,其中包含了所有的n位数(一共10^n个数,从00000(n个0)~10^n-1) /* 典型的欧拉回路题目 对于n=4为密码想要序列最短 那么 1234 234? ...