HDU 4479 Shortest path 带限制最短路
题意:给定一个图,求从1到N的递增边权的最短路。
解法:类似于bellman-ford思想,将所有的边先按照权值排一个序,然后依次将边加入进去更新,
每条边只更新一次,为了保证得到的路径是边权递增的,每次将相同权值的边全部取出来一同更新,
每条边能够更新的前提是某一个端点在之前被更小的边权更新过。另外一个要注意的地方就是一次相同边的更新中,
要把所有的更新暂存起来最后一起去更新,这样是为了防止同一权值的边被多次加入到路径中去。
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const LL maxn = 10005;
const LL maxm = 50005;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
struct node{
int u,v,len;
bool operator < (node a)const{
return len < a.len;
}
}edge[maxm];
LL dis[maxn];
int id,n,m; struct Rec{
int v;
LL d;
}rec[maxn];
void update(int l,int r){
int index = 0;
for(int i = l; i <= r; i++){
int u = edge[i].u,v = edge[i].v,len = edge[i].len;
if( dis[u] != INF && dis[v] > dis[u] + len )
rec[index].v = v,rec[index++].d = dis[u] + len;
if( dis[v] != INF && dis[u] > dis[v] + len)
rec[index].v = u,rec[index++].d = dis[v] + len;
}
for(int i = 0; i < index; i++)
dis[rec[i].v] = min(dis[rec[i].v],rec[i].d);
}
void slove(){
memset(dis,0x3f,sizeof(dis));
cout << dis[0] << endl;
dis[1] = 0;
for(int i = 0,j; i < m; i = j){
for( j = i+1; j < m ;j ++)
if( edge[i].len != edge[j].len)
break;
update(i,j-1);
}
}
int main(){
int t;
scanf("%d",&t);
while( t-- ){
scanf("%d%d",&n,&m);
for(int i = 0; i < m; i++)
scanf("%d %d %d",&edge[i].u,&edge[i].v,&edge[i].len);
sort(edge,edge+m);
slove();
if( dis[n] == INF)puts("No answer");
else printf("%I64d\n",dis[n]);
}
return 0;
}
HDU 4479 Shortest path 带限制最短路的更多相关文章
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- HDU 5636 Shortest Path 暴力
Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- HDU 5636 Shortest Path(Floyed,枚举)
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...
- HDU - 4725_The Shortest Path in Nya Graph
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 3631 Shortest Path
floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...
- HDU 5636 Shortest Path
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 题解: 1.暴力枚举: #include<cmath> #include<c ...
- HDU 5636 Shortest Path 分治+搜索剪枝
题意:bc round 74 分析(官方题解): 你可以选择分类讨论, 但是估计可能会写漏一些地方. 只要抽出新增边的端点作为关键点, 建立一个新图, 然后跑一遍floyd就好了. 复杂度大概O(6^ ...
- HDU 5636 Shortest Path(Floyd)
题目链接 HDU5636 n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边. m个询问,每次询问求两个点之前的最短路. 我们把这三条边的6个点两两算最短路, 然 ...
随机推荐
- Git 的常用的命令
之前一直在使用SVN作为版本管理工具,现在项目要求使用Git,下面简单记录一下一些常用的命令.关于原理和使用方式的详细说明,具体教程参考的廖雪峰的git教程. 1. github 账号的申请. 2. ...
- window下不用安装虚拟机,也可以玩转linux,玩转最新redis
想要了解redis的最新特性,可是windows下的可以安装的版本最高为3.2,想要验证redis的诸如stream特性的话,就无能为力了. 解决方法之一在windows上安装虚拟机,然后再虚拟机上安 ...
- solr 新建core
D:\tomcat\webapps\solr\solr_home 在该路径下创建一个新的core,所需文件和层级如下 test_core |-- conf |-- schema.xml |-- sol ...
- 8、大型项目的接口自动化实践记录----DB分别获取预期结果、实际结果
上一篇实现数据分离升级版--从DB获取数据,以及对应的请求实现,作为一个case,还缺少了预期结果与实际结果的获取及对比.因为前面的文章已经说过接口返回值的获取及对比,所以这篇不说这块了,这篇说一下D ...
- 自己学习并保存的一些shell命令
摘要: 在学习过程中,不免会遇到有些命令,那种需要的,但是你没有掌握的命令.为了节省时间,担心忘记这些,特开辟这个随笔,随时记录用到的一些命令.那么常用的不提了,自己去收集吧~ 1.文件按日期排序 应 ...
- h5微信浏览器复制粘贴--ios兼容问题的解决方法(clipboard.js插件)
前段时间在做微信h5的时候,遇到了ios兼容,使用clipboard.js插件完美解决 下载地址:下载地址: https://github.com/zenorocha/clipboard.js cnd ...
- 【Aizu - 2249】Road Construction(最短路 Dijkstra算法)
Road Construction Descriptions Mercer国王是ACM王国的王者.他的王国里有一个首都和一些城市.令人惊讶的是,现在王国没有道路.最近,他计划在首都和城市之间修建道路, ...
- Tomcat源码分析 (六)----- Tomcat 启动过程(一)
说到Tomcat的启动,我们都知道,我们每次需要运行tomcat/bin/startup.sh这个脚本,而这个脚本的内容到底是什么呢?我们来看看. 启动脚本 startup.sh 脚本 #!/bin/ ...
- AutoCAD C#二次开发
https://www.cnblogs.com/gisoracle/archive/2012/02/19/2357925.html using System; using System.Collect ...
- 迁移学习(Transformer),面试看这些就够了!(附代码)
1. 什么是迁移学习 迁移学习(Transformer Learning)是一种机器学习方法,就是把为任务 A 开发的模型作为初始点,重新使用在为任务 B 开发模型的过程中.迁移学习是通过从已学习的相 ...