[BZOJ2963][JLOI2011]飞行路线 分层图+spfa
Description
Input
Output
Sample Input
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
Sample Output
HINT
对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.
Solution
做法:分层图+$spfa$
大概是叫分层图吧...感觉和$dp$一个样子
就是给$dis$数组多一个属性:用了多少次免费
更新的时候多$for$一遍更新整个$dis$数组就行了
两种转移:
$dis[v][k]=min(dis[v][k],d[u][k]+e[i].v)$
$dis[v][k]=min(dis[v][k],dis[u][k-1])$
$k$表示目前一共用了$k$次免费次数
然后直接跑一遍$spfa$就行了,$k<=10$,$spfa$的常数大了十倍。不过能过就是了
还有就是注意队列用下循环队列,不然空间爆炸
#include <bits/stdc++.h> using namespace std ; #define N 1000010
#define inf 0x3f3f3f3f int n , m , k , s , t ;
int cnt , head[ N ] ;
int q[ N ] , d[ N ][ ] ;
bool vis[ N ] ;
struct node {
int to , nxt , v ;
}e[ N ] ; void ins( int u , int v , int w ) {
e[ ++ cnt ].to = v ;
e[ cnt ].nxt = head[ u ] ;
e[ cnt ].v = w ;
head[ u ] = cnt ;
} void spfa() {
memset( d , 0x3f , sizeof( d ) ) ;
for( int i = ; i <= k ; i ++ ) d[ s ][ i ] = ;
q[ ] = s ;vis[ s ] = ;
int l = , r = ;
while( l != r ) {
int u = q[ l ++ ] ;
vis[ u ] = ;
if( l == ) l = ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
int v = e[ i ].to ,now = ;
for( int j = ; j <= k ; j ++ ) {
if( !j ) now = d[ u ][ j ] + e[ i ].v ;
else now = min( d[ u ][ j - ] , d[ u ][ j ] + e[ i ].v ) ;
if( d[ v ][ j ] > now ) {
d[ v ][ j ] = now ;
if( !vis[ v ] ) vis[ v ] = , q[ r ++ ] = v ;
if( r == ) r = ;
}
}
}
}
} int main() {
scanf( "%d%d%d%d%d" , &n , &m , &k , &s , &t ) ;
s ++ ; t ++ ;
for( int i = ; i <= m ; i ++ ) {
int u , v , w ;
scanf( "%d%d%d" , &u , &v , &w ) ;
u ++ , v ++ ;
ins( u , v , w ) ;
ins( v , u , w ) ;
}
spfa() ;
int ans = inf ;
for( int i = ; i <= k ; i ++ ) {
ans = min( ans , d[ t ][ i ] ) ;
}
printf( "%d\n" , ans ) ;
return ;
}
[BZOJ2963][JLOI2011]飞行路线 分层图+spfa的更多相关文章
- bzoj2763: [JLOI2011]飞行路线(分层图spfa)
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3234 Solved: 1235[Submit][Stat ...
- 【bzoj2763】[JLOI2011]飞行路线 分层图最短路
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的 ...
- bzoj 2763: [JLOI2011]飞行路线 -- 分层图最短路
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MB Description Alice和Bob现在要乘飞机旅行,他们选择了一家相 ...
- bzoj 2763 [JLOI2011]飞行路线——分层图
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 分层图两种方法的练习. 1.把图分成k+1层,本层去上面一层的边免费.但空间时间都不算 ...
- [JLOI2011]飞行路线 分层图最短路
题目描述: Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一 ...
- P4568 [JLOI2011]飞行路线 分层图
题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在nn个城市设有业务,设这些城市分别标记为00到n-1n−1,一共有mm种航线,每种航线连接两个城市,并且 ...
- BZOJ2763[JLOI2011]飞行路线 [分层图最短路]
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2523 Solved: 946[Submit][Statu ...
- BZOJ2763: [JLOI2011]飞行路线(分层图 最短路)
题意 题目链接 Sol 分层图+最短路 建\(k+1\)层图,对于边\((u, v, w)\),首先在本层内连边权为\(w\)的无向边,再各向下一层对应的节点连边权为\(0\)的有向边 如果是取最大最 ...
- bzoj2763 [JLOI2011]飞行路线——分层图
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 构建分层图. 代码如下: 写法1(空间略大)(时间很慢): #include<i ...
随机推荐
- 关于找不到类org/apache/commons/lang/xwork/StringUtils的问题
在替换最新版的 struts2包的解决过程中.遇到 找不到这两个包org/apache/commons/lang/xwork/StringUtils.org/apache/commons/lang/x ...
- ListView多种item注意以及自己出现的莫名其妙的错误
如果ListView不懂,请绕路 1.ListView添加多个item必须用到的两个方法 getViewTypeCount一共有多少种item,我这里写的两种 getItemViewType当前pos ...
- keepalived的log
vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/8088" interval 1 weight -2 } ke ...
- github中的一个快捷键
在github中,快捷键t的作用是对项目的内容进行查找.在项目目录中按下t键,会进入下面这个界面, 在first-program(这是我自己的项目名)这个项目名后,输入你要查找的内容,即可找到项目中所 ...
- Spark与Spring集成做web接口
需要实现的功能: 写访问spark的接口,也就是从web上输入网址就能把我们需要的信息通过提交一个job然后返回给我们json数据. 成果展示: 通过url请求,然后的到一个wordcount的jso ...
- vue-router中参数传递 && 编程式导航 && 坑 && beforeRouteEnter
第一部分: vue-router参数传递 通过router-link我们可以向路由到的组件传递参数,这在我们实际使用中时非常重要的. 路由: { path:"/DetailPage" ...
- PAT 1067 Sort with Swap[难]
1067 Sort with Swap(0,*) (25)(25 分) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is e ...
- PAT Product of Polynomials[一般]
1009 Product of Polynomials (25)(25 分) This time, you are supposed to find A*B where A and B are two ...
- C# 如何把dataTable以参数的形式传入 sql 存储过程
==================================================-- sql代码 示例:CREATE TYPE dbo.Content AS TABLE( ID i ...
- sql distinct去除重复
distinct select distinct * from table1 或者用 group by