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 ...
随机推荐
- java_IO_装饰器
装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
- Number String(HDU 4055,动态规划递推,前缀和优化)
点击加号查看代码 #include<bits/stdc++.h>//前缀和优化版本,不易理解 using namespace std; #define ll long long ; ; l ...
- 回车符号 ‘\r’ 的实际应用
由于最近开始研究自动化测试 首先是自动定时去下载安装包,需要实时显示进度. 于是了解了进度条相关的方法. 作下记录. 区别 \r 表示将光标的位置回退到本行的开头位置 \n 表示光标从下一行的开头位置 ...
- 洛谷 1182 数列分段Section II
[题解] 最大值最小化,那么一般要联想到二分.二分一个最大值,然后check一下能否分成小于等于m段即可. #include<cstdio> #include<algorithm&g ...
- StringBuilder的构造方法
/* * String和StringBuilder的区别: * String的内容是固定的 * StringBuilder的内容是可变的 * 构造方法: * StringBuilder() * 成员方 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- HDU 1220 简单数学题
题目大意是 在魔方上找到有多少对小立方块它们之间连接的点不超过两个 因为任意两个立方块之间相连的点就只有0,1,2,4 这样4种情况 那么我们只需要考虑总共的组成立方块对数 sum = C(2 , n ...
- codevs1018 单词接龙
题目描述 Description 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次), ...
- 【tomcat】如何修改tomcat的默认项目
我们知道,在Tomcat安装.配置.启动成功后在浏览器地址栏输入http://localhost:8080会访问到Tomcat的默认主页. 然后我们打开Tomcat的webapps目录时,会发现里面有 ...
- CodeForces 370C
这道题其实是挺简单的.首先很容易发现最多人用的颜色的人数如果大于n/2,就肯定不能让全部人都成功戴上两只不同颜色的手套.反过来想,如果这个人数小于等于n/2又如何呢?的确,这就能让全部人都能戴上两只不 ...