hdu4396 多状态spfa
题意:
给你一个图,让你送起点走到终点,至少经过k条边,问你最短路径是多少....
思路:
把每个点拆成50点,记为dis[i][j] (i 1---50 ,j 1---n);代表走到第j个点做过i条边时的最短距离,因为做多五十条边,如果走的过程中,边数大于50直接等于50,因为大于50的时候就没有必要走"回头路"了...然后跑完spfa后在dis[i][t](i = k---50)中取一个最小的输出来,就行了...
#include<stdio.h>
#include<string.h>
#include<queue> #define N_node 5000 + 100
#define N_edge 200000 + 1000
#define inf 100000000
using namespace std; typedef struct
{
int to ,next ,cost;
}STAR; typedef struct
{
int x ,t;
}NODE; int s_x[55][N_node] ,n ,m ,s ,t;
int mark[55][N_node];
int list[N_node] ,tot;
NODE xin ,tou;
STAR E[N_edge]; void add(int a ,int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
} void SPFA()
{
for(int i = 0 ;i <= 52 ;i ++)
for(int j = 1 ;j <= n ;j ++)
s_x[i][j] = inf;
// printf("%d %d\n" ,s_x[1][3] ,s_x[1][2]);
s_x[0][s] = 0;
xin.x = s;
xin.t = 0;
queue<NODE>q;
q.push(xin);
memset(mark ,0 ,sizeof(mark));
mark[0][s] = 1;
while(!q.empty())
{
tou = q.front();
q.pop();
mark[tou.t][tou.x] = 0;
for(int k = list[tou.x] ;k ;k = E[k].next)
{
xin.x = E[k].to;
xin.t = tou.t + 1;
if(xin.t > 50) xin.t = 50;
//printf("%d %d %d %d\n" ,s_x[xin.t][xin.x] ,s_x[tou.t][tou.x] + E[k].cost ,xin.t ,xin.x);
if(s_x[xin.t][xin.x] > s_x[tou.t][tou.x] + E[k].cost)
{
s_x[xin.t][xin.x] = s_x[tou.t][tou.x] + E[k].cost; if(!mark[xin.t][xin.x])
{
mark[xin.t][xin.x] = 1;
q.push(xin);
}
}
}
}
} int main ()
{
int m ,a ,b ,c ,k ,i;
while(~scanf("%d %d" ,&n ,&m))
{
memset(list ,0 ,sizeof(list));
tot = 1;
for(i = 1 ;i <= m ;i ++)
{
scanf("%d %d %d" ,&a ,&b ,&c);
add(a ,b ,c);
add(b ,a ,c);
} scanf("%d %d %d" ,&s ,&t ,&k);
SPFA();
int ans = inf;
k = (k + 9)/10;
for(i = k ;i <= 50 ;i ++)
if(ans > s_x[i][t])
ans = s_x[i][t];
if(ans == inf) ans = -1;
printf("%d\n" ,ans);
}
return 0;
}
hdu4396 多状态spfa的更多相关文章
- Travelling(spfa+状态压缩dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ...
- Victor and World(spfa+状态压缩dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/ ...
- HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA
状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j] ...
- [luogu P3786]萃香抱西瓜 [spfa][状态压缩]
题目背景 伊吹萃香(Ibuki Suika)正在魔法之森漫步,突然,许多西瓜(Suika)从四周飞来,划出了绚丽的轨迹.虽然阵势有点恐怖,但她还是决定抱走一些西瓜. 题目描述 萃香所处的环境被简化为一 ...
- BZOJ2763 [JLOI2011]飞行路线(SPFA + DP)
题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=2763 Description Alice和Bob现在要乘飞机旅行,他们选择了一家 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- 【BZOJ-1097】旅游景点atr SPFA + 状压DP
1097: [POI2007]旅游景点atr Time Limit: 30 Sec Memory Limit: 357 MBSubmit: 1531 Solved: 352[Submit][Sta ...
- ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)
这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...
随机推荐
- arch 安装笔记
arch- 第一次装archLinux时,照着别人的安装教程来安装,由于不懂有些命令的意思,装了好几次才成功,这次趁着热乎,把安装的步骤写下来,为自己踩踩坑(桌面是xfce,下面也有换桌面的方法,我第 ...
- flask wtforms 的效验
flask版 .py from flask import Flask, render_template, request, session, current_app, g, redirect from ...
- [LeetCode 279.] Perfect Squres
LeetCode 279. Perfect Squres DP 是笨办法中的高效办法,又是一道可以被好办法打败的 DP 题. 题目描述 Given a positive integer n, find ...
- Spring Boot 自动配置 源码分析
Spring Boot 最大的特点(亮点)就是自动配置 AutoConfiguration 下面,先说一下 @EnableAutoConfiguration ,然后再看源代码,到底自动配置是怎么配置的 ...
- ch1_6_5求解旋转词问题
import java.util.Scanner; public class ch1_6_5求解旋转词问题 { public static void main(String[] args) { // ...
- java例题_25 判断是否为回文数!
1 /*25 [程序 25 求回文数] 2 题目:一个 5 位数,判断它是不是回文数.即 12321 是回文数,个位与万位相同,十位与千位相同. 3 */ 4 5 /*分析 6 * 先用%和/将5个数 ...
- [素数判断]P1125 笨小猴
笨小猴 题目描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词中出 ...
- JavaWeb 补充(Filter&Listener)
目录 Filter:过滤器 Listener:监听器 Filter:过滤器 1. 概念: * 生活中的过滤器:净水器,空气净化器,土匪. * web中的过滤器:当访问服务器的资源时,过 ...
- Spring Cloud Gateway 全局通用异常处理
为什么需要全局异常处理 在传统 Spring Boot 应用中, 我们 @ControllerAdvice 来处理全局的异常,进行统一包装返回 // 摘至 spring cloud alibaba c ...
- Go+gRPC-Gateway(V2) 微服务实战,小程序登录鉴权服务(五):鉴权 gRPC-Interceptor 拦截器实战
拦截器(gRPC-Interceptor)类似于 Gin 中间件(Middleware),让你在真正调用 RPC 服务前,进行身份认证.参数校验.限流等通用操作. 系列 云原生 API 网关,gRPC ...