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相关推荐
- 【BZOJ3049】Island Travels,SPFA预处理+状态压缩DP
传送门(权限题) 3049: [Usaco2013 Jan]Island Travels Time Limit: 10 Sec Memory Limit: 128 MB Submit: 84 Solv ...
- Victor and World(spfa+状态压缩dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/ ...
- 洛谷 P1073 最优贸易 (分层图状态转移+SPFA,求最长路径;另附某dalao的超短代码:暴力+动规)
题目链接1 题目链接2 另附某dalao的超短代码:暴力+动规 P1073 最优贸易 题目描述 C国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市. 任意两个城市之间最多只有 ...
- 【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)
题干: Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bi ...
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
- BZOJ-1644: [Usaco2007 Oct]Obstacle Course 障碍训练课(SPFA)
1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 707 Solve ...
- NOIP 2017 提高组 K: 奶酪 (SPFA || 并查集)
K: 奶酪 时间限制: 1 Sec 内存限制: 128 MB 提交: 115 解决: 30 [提交][状态][讨论版] 题目描述 现有一块大奶酪,它的高度为 h ,它的长度和宽度我们可以认为是无限 ...
- POJ1722二维spfa+优先队列优化
题意: 给你一个有向图,然后求从起点到终点的最短,但是还有一个限制,就是总花费不能超过k,也就是说每条边上有两个权值,一个是长度,一个是花费,求满足花费的最短长度. 思路: 一开 ...
- HDU-4568 Hunter 状态压缩
题意:给定一个网格图,图中有一些点要求全部走到,问最少的花费是多少,从任意边界进入,任意边界出去,如果不能够全部走到,输出0. 解法:一次spfa从边界上的所有点出发,计算到K个宝藏的最短路,然后计算 ...
最新文章
- 【原创】【专栏】《Linux设备驱动程序》--- LDD3源码目录结构和源码分析经典链接
- 20180315 代码错题(1)
- unix mysql_Unix和Mysql中列出的数据库大小之间存在差异
- leetcode 1239. Maximum Length of a Concatenated String with Unique Characters | 1239. 串联字符串的最大长度(回溯)
- urlrewrite伪静态 及多参数传递-附正则表达式语法 [轉]
- RHEL5下DNS配置详解3
- C 20 协程初探
- oracle session_wait,转载:学习Oracle动态性能表-(8)-V$SESSION_WAIT,V$SESSION_EVENT
- Java NPOIFSFileSystem.getRoot方法代碼示例
- html插入flash时钟,PPT怎么插入Flash时钟显示实时时间?
- 2017年全国大学生电子设计竞赛 单相用电器分析监测装置(K题)
- google搜索自己博客文章小记
- windows server 2003 桌面图标有蓝底如何解决
- ios 清理缓存功能实现
- 练习孙氏太极拳的感悟
- java sockets_Java Sockets
- keras实现deblurgan-v1(图像去模糊)
- 贵州学计算机,在贵州省计算机学校学习计算机专业如何?
- Latex加批注 更改
- one-stage和two-stage网络的区别
热门文章
- 网站如何接入支付宝(转)
- Java 基础入门随笔(1) JavaSE版——java语言三种技术架构
- webform开发经验(一):Asp.Net获取Checkbox选中的值
- CString, BSTR, LPCTSTR之间得关系和区别
- 【挑战极限】最短AJAX创建代码
- SQL SERVER2000教程-第四章 创建和维护表 第二节 数据完整性
- 菜鸟requireJS教程---1、初识requirejs
- (转)C# Delegate.Invoke、Delegate.BeginInvoke
- 记一次因坏块引起的dataguard恢复
- 编译Hadoop源码