• 先讲一个为了少打一些代码而滥用继承终于接受慘痛教训的故事。
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int oo = 1000000000, nil = 0;
int N, M, S, T, K, times[1005];
int u[200010], v[200010], w[200010], nxt[200010], pnt[1005], e;
int d[1005];
bool vis[1005], other[1005];
class node
{
public:
int n, dis;
node(int n = 0, int dis = 0) :n(n), dis(dis) {}
protected:
virtual bool operator < (const node& b) const
{
return dis > b.dis;
}
};
struct astar : public node
{
astar(int xn = 0, int xdis = 0)
{
n = xn; dis = xdis;
}
bool operator < (const astar& b) const
{
return dis + d[n] > b.dis + d[b.n];
}
};
void addedge(int a, int b, int c, bool d)
{
u[++e] = a; v[e] = b; w[e] = c;
nxt[e] = pnt[a]; pnt[a] = e; other[e] = d;
}
void init()
{
int a, b, c;
scanf("%d%d", &N, &M);
for(int i = 1; i <= M; ++i)
{
scanf("%d%d%d", &a, &b, &c);
addedge(a, b, c, false);
addedge(b, a, c, true);
}
scanf("%d%d%d", &S, &T, &K);
if(S == T)
{
++K;
}
}
void getSSSP(int s)
{
memset(d, 0x3f, sizeof(d));
memset(vis, 0, sizeof(vis));
priority_queue <node> Q;
d[s] = 0;
Q.push(node(s, 0));
while(!Q.empty())
{
node t = Q.top();
Q.pop();
vis[t.n] = true;
for(int j = pnt[t.n]; j != nil; j = nxt[j])
{
if((!vis[v[j]]) && (d[v[j]] > t.dis + w[j]) && (other[j]))
{
d[v[j]] = t.dis + w[j];
Q.push(node(v[j], d[v[j]]));
}
}
}
}
void work()
{
getSSSP(T);
memset(times, 0, sizeof(times));
priority_queue <astar> Q;
Q.push(astar(S, 0));
while(!Q.empty())
{
astar t = Q.top();
Q.pop();
++times[t.n];
if((t.n == T) && (times[t.n] == K))
{
printf("%d\n", t.dis);
return;
}
for(int j = pnt[t.n]; j != nil; j = nxt[j])
{
if(!other[j])
{
Q.push(astar(v[j], t.dis + w[j]));
}
}
}
puts("-1");
}
int main()
{
init();
work();
return 0;
}

然后由于node里的小于号被protected了。stl的priority_queue无法调用之而编译失败。

假设去掉protected。node里的小于号覆盖不掉,又会使astar中的小于号没用导致WA。TUT……

  • 题目来源

id=2449">http://poj.org/problem?id=2449

  • 题目大意

    求一张有向图从S到T的第K短路(边权均为正)。

  • 题解

    首先在反向图中最短路出每一个点到终点的距离,然后利用A*的思想,从起点開始可反复地搜索,估价函数为{当前走过的距离+该点到终点的最短路长度}。当终点T被第K次从堆(优先队列)中取出时,输出答案。

  • 注意细节

    S == T时要把K加上1;

    某结点出队次数超过K时,能够不再考虑。

  • Code

#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int oo = 1000000000, nil = 0;
int N, M, S, T, K, times[1005];
int u[200010], v[200010], w[200010], nxt[200010], pnt[1005], e;
int d[1005];
bool vis[1005], other[200010];
struct node
{
int n, dis;
node(int n = 0, int dis = 0) :n(n), dis(dis) {}
bool operator < (const node& b) const
{
return dis > b.dis;
}
};
struct astar
{
int n, dis;
astar(int n = 0, int dis = 0) :n(n), dis(dis) {}
bool operator < (const astar& b) const
{
return dis + d[n] > b.dis + d[b.n];
}
};
void addedge(int a, int b, int c, bool d)
{
u[++e] = a; v[e] = b; w[e] = c;
nxt[e] = pnt[a]; pnt[a] = e; other[e] = d;
}
void init()
{
int a, b, c;
scanf("%d%d", &N, &M);
for(int i = 1; i <= M; ++i)
{
scanf("%d%d%d", &a, &b, &c);
addedge(a, b, c, false);
addedge(b, a, c, true);
}
scanf("%d%d%d", &S, &T, &K);
if(S == T)
{
++K;
}
}
void getSSSP()
{
memset(d, 0x3f, sizeof(d));
memset(vis, 0, sizeof(vis));
priority_queue <node> Q;
d[T] = 0;
Q.push(node(T, 0));
while(!Q.empty())
{
node t = Q.top();
Q.pop();
vis[t.n] = true;
for(int j = pnt[t.n]; j != nil; j = nxt[j])
{
if((!vis[v[j]]) && (d[v[j]] > t.dis + w[j]) && (other[j]))
{
d[v[j]] = t.dis + w[j];
Q.push(node(v[j], d[v[j]]));
}
}
}
}
void work()
{
getSSSP();
memset(times, 0, sizeof(times));
priority_queue <astar> Q;
Q.push(astar(S, 0));
while(!Q.empty())
{
astar t = Q.top();
Q.pop();
++times[t.n];
if((t.n == T) && (times[t.n] == K))
{
printf("%d\n", t.dis);
return;
}
if(times[t.n] > K)
{
continue;
}
for(int j = pnt[t.n]; j != nil; j = nxt[j])
{
if(!other[j])
{
Q.push(astar(v[j], t.dis + w[j]));
}
}
}
puts("-1");
}
int main()
{
init();
work();
return 0;
}
  • 再讲一个悲伤的故事:这段代码我一開始把other数组开成了点数,然后RE了4次,另一次忘了删去while(1)……所以一定要养成提前定义常量和认真静态查错的好习惯。

POJ2449题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. 浅谈cocos2dx(17) 中单例管理模式

    ----我的生活,我的点点滴滴!. 首先明白一个问题.什么是管理者模式,管理类是用来管理一组相关对象的类,他提供了訪问对象的接口,假设这么说比較抽象的话.我们来看下cocos2dx中都有哪些类是管理类 ...

  2. poj--2631--Roads in the North(树的直径 裸模板)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2389   Accepted: 117 ...

  3. 10.QT程序框架与connect

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setup ...

  4. spring事务,TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

    在aop配置事务控制或注解式控制事务中,try...catch...会使事务失效,可在catch中抛出运行时异常throw new RuntimeException(e)或者手动回滚Transacti ...

  5. Django后台创建

    1.首先创建Django工程 创建Django有两种方法我用的是pycharm的创建 2.查看url.py 如下 from django.contrib import admin from djang ...

  6. uploadifive上传文件

    uploadifive是一个款基于H5的上传文件的插件.优点是,可以在PC端,也可以在手机上进行操作.缺点是,IE9以下的兼容性不好. View: <!DOCTYPE html> < ...

  7. JS 限制input框的输入字数,并提示可输入字数

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. 20 个让你效率更高的 CSS 代码技巧

    在这里想与你分享一个由各大CSS网站总结推荐的20个有用的规则和实践经验集合.有一些是面向CSS初学者的,有一些知识点是进阶型的.希望每个人通过这篇文章都能学到对自己有用的知识. 1.注意外边距折叠 ...

  9. node——post提交新闻内容

    获取用户post提交的数据分多次提交,因为post提交数据的时候,数据量可能比较大,会要影响服务器中获取用户所以.提交的所有数据,就必须监听request事件.那么,什么时候才表示浏览器把所有数据提交 ...

  10. NYOJ 737 石子合并(一)

    题意 排成一排的石子,每次合并相邻两堆并由一定的代价,求合并成一堆的最小代价 解法 区间dp 枚举长度 dp[i,j]表示合并石子堆编号从i到j为一堆所需的最小代价(这个题目的代价是sum(i..j) ...