A* + dijkstra/spfa

第K短路的模板题,就是直接把最短路当成估价函数,保证估价函数的性质(从当前状态转移的估计值一定不大于实际值)

我们建反图从终点跑最短路,就能求出从各个点到终点的最短距离,这样就能满足估价函数的性质了

要注意一点,当起点和终点一样的时候第k短路就变成k+1短了,因为0也算一条。。。

话说回来为啥我用pair就MLE了呢。。。。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C yql){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % yql)if(p & 1)ans = 1LL * x * ans % yql;
return ans;
}
const int N = 1005;
int n, m, k, s, t, cnt1, cnt2, head1[N], head2[N], dist[N], num[N], w[N];
bool vis[N];
struct Edge{ int v, next, dis; } edge1[100005], edge2[100005]; struct Point{
int s, dis;
bool operator < (const Point &rhs) const {
return dis > rhs.dis;
}
Point(int s, int dis): s(s), dis(dis){}
}; struct Node{
int v, f, g;
Node(int v, int f, int g): v(v), f(f), g(g){}
bool operator < (const Node &rhs) const{
return f + g > rhs.f + rhs.g;
}
}; void addEdge1(int a, int b, int c){
edge1[cnt1].v = b, edge1[cnt1].next = head1[a], edge1[cnt1].dis = c;
head1[a] = cnt1 ++;
} void addEdge2(int a, int b, int c){
edge2[cnt2].v = b, edge2[cnt2].next = head2[a], edge2[cnt2].dis = c;
head2[a] = cnt2 ++;
} void dijkstra(){
fill(dist, dist + N, INF);
priority_queue<Point> pq;
dist[t] = 0;
pq.push(Point(t, dist[t]));
while(!pq.empty()){
int s = pq.top().s, d = pq.top().dis; pq.pop();
if(vis[s]) continue;
vis[s] = true;
for(int i = head2[s]; i != -1; i = edge2[i].next){
int u = edge2[i].v;
if(dist[u] > d + edge2[i].dis){
dist[u] = d + edge2[i].dis;
pq.push(Point(u, dist[u]));
}
}
}
} int astar(){
if(dist[s] == INF) return -1;
if(s == t) k ++;
priority_queue<Node> pq;
pq.push(Node(s, dist[s], 0));
while(!pq.empty()){
Node cur = pq.top(); pq.pop();
num[cur.v] ++;
if(cur.v == t && num[cur.v] == k) return cur.f + cur.g;
if(num[cur.v] > k) continue;
for(int i = head1[cur.v]; i != -1; i = edge1[i].next){
int u = edge1[i].v;
if(num[u] == k) continue;
pq.push(Node(u, dist[u], cur.g + edge1[i].dis));
}
}
return -1;
} void init(){
cnt1 = cnt2 = 0;
memset(head1, -1, sizeof head1);
memset(head2, -1, sizeof head2);
} int main(){ while(scanf("%d%d", &n, &m) != EOF){
init();
for(int i = 0; i < m; i++){
int a, b, c; scanf("%d%d%d", &a, &b, &c);
addEdge1(a, b, c), addEdge2(b, a, c);
}
scanf("%d%d%d", &s, &t, &k);
dijkstra();
printf("%d\n", astar());
}
return 0;
}

POJ 2449 Remmarguts' Date (算竞进阶习题)的更多相关文章

  1. POJ 1015 Jury Compromise (算竞进阶习题)

    01背包 我们对于这类选或者不选的模型应该先思考能否用01背包来解. 毫无疑问物体的价值可以看成最大的d+p值,那么体积呢?题目的另一个限制条件是d-p的和的绝对值最小,这启发我们把每个物体的d-p的 ...

  2. POJ 2245 Addition Chains(算竞进阶习题)

    迭代加深dfs 每次控制序列的长度,依次加深搜索 有几个剪枝: 优化搜索顺序,从大往下枚举i, j这样能够让序列中的数尽快逼近n 对于不同i,j和可能是相等的,在枚举的时候用过的数肯定不会再被填上所以 ...

  3. poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)

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

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

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

  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短路径)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:35025   Accepted: 9467 ...

  8. POJ 1821 Fence (算竞进阶习题)

    单调队列优化dp 我们把状态定位F[i][j]表示前i个工人涂了前j块木板的最大报酬(中间可以有不涂的木板). 第i个工人不涂的话有两种情况: 那么F[i - 1][j], F[i][j - 1]就成 ...

  9. POJ 3974 Palindrome (算竞进阶习题)

    hash + 二分答案 数据范围肯定不能暴力,所以考虑哈希. 把前缀和后缀都哈希过之后,扫描一边字符串,对每个字符串二分枚举回文串长度,注意要分奇数和偶数 #include <iostream& ...

随机推荐

  1. Jmeter(三十六)_运行过程中改变负载

    顾名思义,jmeter在做性能测试时,可以在不停止脚本的情况下修改负载压力,达到期望的测试效果.我们将通过Constant Throughput Timer(吞吐量计时器)和Beanshell服务器来 ...

  2. Git简易的命令入门

    Git 全局设置: git config --global user.name "kszsa" git config --global user.email "duyon ...

  3. Jq相关常用操作

    1.select下拉列表操作 $(".kstitle").live('change', function () { var workType = $(this).val(); // ...

  4. hybrid App cordova打包webapp PhoneGap

    Hybrid APP基础篇(一)->什么是Hybrid App APP三种开发模式--之--HybridApp解决方案 Hybrid App开发 四大主流平台分析 Hybrid App 开发模式 ...

  5. JAVA项目中的常用的异常处理情况

    NO.1 java.lang.NullPointerException 这个异常比较容易遇到,此异常的解释是“程序遇上了空指针”,简单地说就是调用了未经初始化的对象或者是不存在的对象,这个错误经常出现 ...

  6. HDU 3947 Assign the task

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Problem Description There is a company that has N emp ...

  7. Zabbix appliance One Stop

    Download Zabbix appliancehttps://www.zabbix.com/download_appliance

  8. 简述HTTP协议

    引言 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网服务器传输超文本到本地浏览器的传送协议.HTTP 是基于 TCP/IP 协议通信协议 ...

  9. 为什么说Java中只有值传递(转载)

    出处:https://www.hollischuang.com/archives/2275 关于这个问题,在StackOverflow上也引发过广泛的讨论,看来很多程序员对于这个问题的理解都不尽相同, ...

  10. [转帖]nginx配置ssl加密(单/双向认证、部分https)

    nginx配置ssl加密(单/双向认证.部分https) https://segmentfault.com/a/1190000002866627   nginx下配置ssl本来是很简单的,无论是去认证 ...