*题目描述:
Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?

*输入:
数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。
第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t<n)
接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b<n,a与b不相等,0<=c<=1000)

*输出:
只有一行,包含一个整数,为最少花费。

*样例输入:
5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100

*样例输出:
8

*提示:
对于30%的数据,2<=n<=50,1<=m<=300,k=0;
对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.

*题解:
有k次免票的最短路。
好像直接spfa多计一个当前免票多少次好像就过了,据说有一种神奇的分层图的做法?

*代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue> #ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif #ifdef CT
#define debug(...) printf(__VA_ARGS__)
#define setfile()
#else
#define debug(...)
#define filename ""
#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout)
#endif #define R register
#define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
#define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
#define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
#define cabs(_x) ((_x) < 0 ? (- (_x)) : (_x))
char B[1 << 15], *S = B, *T = B;
inline int F()
{
R char ch; R int cnt = 0; R bool minus = 0;
while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
ch == '-' ? minus = 1 : cnt = ch - '0';
while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
return minus ? -cnt : cnt;
}
#define maxn 10010
#define maxm 100010
#define P std::pair<int, int>
#define mkp std::make_pair
#define fir first
#define sec second
int d[maxn][20];
struct Edge
{
Edge *next;
int to, w;
}*last[maxn], e[maxm], *ecnt = e;
inline void link(R int a, R int b, R int w)
{
*++ecnt = (Edge) {last[a], b, w}; last[a] = ecnt;
*++ecnt = (Edge) {last[b], a, w}; last[b] = ecnt;
}
std::queue<P> q;
bool inq[maxn][20];
int main()
{
// setfile();
R int n = F(), m = F(), k = F(), s = F(), t = F();
for (R int i = 1; i <= m; ++i)
{
R int a = F(), b = F(), w = F();
link(a, b, w);
}
memset(d, 63, sizeof (d));
q.push(mkp(s, 0)); d[s][0] = 0;
while (!q.empty())
{
R P now = q.front(); q.pop();
R int pos = now.fir, times = now.sec;
inq[pos][times] = 0;
for (R Edge *iter = last[pos]; iter; iter = iter -> next)
{
if (d[iter -> to][times] > d[pos][times] + iter -> w)
{
d[iter -> to][times] = d[pos][times] + iter -> w;
if (!inq[iter -> to][times])
{
q.push(mkp(iter -> to, times));
inq[iter -> to][times] = 1;
}
}
if (times < k && d[iter -> to][times + 1] > d[pos][times])
{
d[iter -> to][times + 1] = d[pos][times];
if (!inq[iter -> to][times + 1])
{
q.push(mkp(iter -> to, times + 1));
inq[iter -> to][times + 1];
}
}
}
}
R int ans = 0x7fffffff;
for (R int i = 0; i <= k; ++i) cmin(ans, d[t][i]);
printf("%d\n", ans );
return 0;
}

【bzoj2763】[JLOI2011]飞行路线的更多相关文章

  1. BZOJ2763 JLOI2011 飞行路线 【最短路+DP】

    BZOJ2763 JLOI2011 飞行路线 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n ...

  2. BZOJ2763[JLOI2011]飞行路线 [分层图最短路]

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2523  Solved: 946[Submit][Statu ...

  3. bzoj千题计划226:bzoj2763: [JLOI2011]飞行路线

    http://www.lydsy.com/JudgeOnline/problem.php?id=2763 这也算分层图最短路? dp[i][j]到城市i,还剩k次免费次数的最短路 #include&l ...

  4. bzoj2763: [JLOI2011]飞行路线(分层图spfa)

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3234  Solved: 1235[Submit][Stat ...

  5. BZOJ2763 [JLOI2011]飞行路线(SPFA + DP)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=2763 Description Alice和Bob现在要乘飞机旅行,他们选择了一家 ...

  6. Bzoj2763 [JLOI2011]飞行路线

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2651  Solved: 1004 Description Alice和Bob现在要乘飞机旅行,他们选 ...

  7. bzoj2763: [JLOI2011]飞行路线 分层图+dij+heap

    分析:d[i][j]代表从起点到点j,用了i次免费机会,那就可以最短路求解 #include <stdio.h> #include <iostream> #include &l ...

  8. [luogu4568][bzoj2763][JLOI2011]飞行路线

    题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为00到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定 ...

  9. bzoj2763: [JLOI2011]飞行路线 最短路

    题意:求最多可以有k条路免费的最短路 题解:用dis[x][k]表示从s开始用了k次免费机会到x的最短路,然后dij跑的时候优先队列里多维护一个k就好了 /********************** ...

  10. BZOJ2763: [JLOI2011]飞行路线(分层图 最短路)

    题意 题目链接 Sol 分层图+最短路 建\(k+1\)层图,对于边\((u, v, w)\),首先在本层内连边权为\(w\)的无向边,再各向下一层对应的节点连边权为\(0\)的有向边 如果是取最大最 ...

随机推荐

  1. 关于Eclipse及JDK安装过程中的一些问题

    一,环境变量的配置 1.配置CLASSPATH系统变量 CLASSPATH系统变量为类查找路径 ①.在使用javac进行编译时遇到import时候就会通过这个变量里面配置的路径去查找.如果配置的是目录 ...

  2. CentOS7Linux中服务器LVS负载均衡、高可用集群搭建(NAT、DR);

    目录 集群 声明 集群概念 集群特性 Web服务器并发相应瓶颈 集群的分类 LB实现方法: LVS集群 负载调度器 服务器池 共享存储 LVS负载均衡的三种模式 负载均衡 集群 声明 文档不断更新中. ...

  3. priority_queue的常见用法

    priority_queue的常见用法 priority_queue是什么? 优先队列 底层实现用堆来实现 每次队首的优先级最大 priority_queue的定义 引入头文件 # include & ...

  4. hadoop-组件

    hadoop1.x 和 hadoop2.x 区别 HDFS 分布式文件存储系统 优点   缺点 MapReduce 分布式计算 详见我的博客 mapreduce YARN 计算资源管理器 主要了解两个 ...

  5. Java日志打印方法

    一.使用log4j打印日志 1. 下载log4j.jar和commons-logging.jar.     log4j.jar下载地址:http://logging.apache.org/log4j/ ...

  6. python 单引号、双引号和三引号混用

    单引号: 当单引号中存在单引号时,内部的单引号需要使用转义字符,要不然就会报错: 当单引号中存在双引号时,双引号可以不用加转义字符,默认双引号为普通的字符,反之亦然. 双引号: 当双引号中存在双引号时 ...

  7. scp 自动带密码参数复制文件到主机

    一.安装sshpass工具 [root@zabbix_server scripts]# yum install sshpass 二.运行 [root@zabbix_server scripts]# s ...

  8. Python 输出百分比

    注:python3环境试验 0x00 使用参数格式化{:2%} {:.2%}: 显示小数点后2位 print('{:.2%}'.format(10/50)) #percent: 20.00% {:.0 ...

  9. zencart批量表上传后 标题显示为网址 批量修改标题状态 SEO三要素

    zencart批量表上传后 标题显示为网址,原因是导入批量表时,产品标题对应状态被重置为0导致的 批量修改标题状态 ', metatags_products_name_status ', metata ...

  10. sed编辑

    data4.txt this is a test of the test scriptthis is the second test of the trial script data6.txt thi ...