HDU 5168
把边按权值排序后,就相当于求一个子序列以1开始和以n结束。由于边权递增,而且相差>=k,所以,边的顺序也必定是递增的。知道,当处理一条出边时,必定是从入边选择一条最优的边,考虑两个因素,入边的权值和入边以前的总的代价。优化很容易想到,每个结点维护一个数组,就不需要从头扫描边数组了。但这个维护的结点的数组在两个因素符合什么条件呢。。。卡住了。。
参考了题解之后,是按入边来维护,当入边权值增大而总代价下降。这样,二分查找出总代价最小而且符合边约束的。要知道,入边权值增大是自然符合的,因为边数组在开始时就已经由小到大排列。所以,当处理到目的顶点v,到达V的总代价比结点数组的最高点还小,则把该边加入到结点数组中即可。
其中,二分使用upper_bound查找上界,学习了http://blog.csdn.net/whai362/article/details/43385107,谢谢了。。。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#define LL __int64
using namespace std;
const LL INF=(LL)0x3f3f3f3f*0x3f3f3f3f;
const int N=;
const int M=; struct TEdge{
int u,v,w;
bool operator<(const TEdge& a)const {
if(w<a.w) return true;
return false;
}
}Cedge[M];
int tot;
struct Point{
int lw;
LL alw;
Point(int c,LL e){lw=c; alw=e;}
}; vector<Point>point[N];
int n,m,k; void addedge(int u,int v,int w){
Cedge[tot].u=u;
Cedge[tot].v=v;
Cedge[tot].w=w;
tot++;
} void Push_into(int p,Point t){
point[p].push_back(t);
} bool cmp(Point a,Point b){
if(a.lw==b.lw) return a.alw>b.alw;
return a.lw<b.lw;
} int main(){
int T,u,v,w,sz;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&k);
tot=;
for(int i=;i<=n;i++){
point[i].clear();
}
for(int i=;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
sort(Cedge,Cedge+tot);
for(int i=;i<tot;i++){
u=Cedge[i].u;
v=Cedge[i].v;
if(u==){
if(point[v].empty()){
Push_into(v,Point(Cedge[i].w,Cedge[i].w));
}
else{
if(point[v][point[v].size()-].alw>Cedge[i].w){
Push_into(v,Point(Cedge[i].w,Cedge[i].w));
}
}
continue;
}
if(point[u].empty()) continue;
else {
int p=upper_bound(point[u].begin(),point[u].end(),Point(Cedge[i].w-k,-INF),cmp)-point[u].begin();
if(p==) continue;
else if(p> && p<point[u].size()) --p;
else if(point[u][point[u].size()-].lw<=Cedge[i].w-k) p=point[u].size()-;
else continue;
if(point[v].empty())
Push_into(v,Point(Cedge[i].w,point[u][p].alw+(LL)Cedge[i].w));
else if(point[v][point[v].size()-].alw>point[u][p].alw+(LL)Cedge[i].w)
Push_into(v,Point(Cedge[i].w,point[u][p].alw+(LL)Cedge[i].w));
}
}
if(point[n].empty())
printf("-1\n");
else printf("%I64d\n",point[n][point[n].size()-].alw);
}
return ;
}
HDU 5168的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- [BZOJ 3387] Fence Obstacle Course
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3387 [算法] f[i][0]表示从第i个栅栏的左端点走到原点的最少移动步数 f[i ...
- Tool-杂项-建模:犀牛(3D造型软件)
ylbtech-Tool-杂项-建模:犀牛(3D造型软件) 犀牛(Rhino)是美国Robert McNeel & Assoc.开发的PC上强大的专业3D造型软件,它可以广泛地应用于三维动画制 ...
- Kaggle爆文:一个框架解决几乎所有机器学习问题
上周一个叫 Abhishek Thakur 的数据科学家,在他的 Linkedin 发表了一篇文章 Approaching (Almost) Any Machine Learning Proble ...
- iterator和iterable的区别
相关博客: http://blog.csdn.net/lipengcn/article/details/51700153 Java中Iterable和Iterator的辨析 http ...
- List 常用方法解析
1.Count属性 (获得List中元素数目) 2.Add( ) 在List中添加一个对象的公有方法 3.AddRange( ) 公有方法,在List尾部添加实现了ICollection接口的多个元素 ...
- DB2 char长度问题
问题:发现用char转换了后的值长度都变为了11,更长的变为了254
- STL编程:C++的忠告!
Copy别人的,有少量修改,可以做为一下参考! C++之父Bjarne Stroustrup 写的 The C++ Programming Language (Special Edition) 中各章 ...
- ubuntu 搭建Mercurial 服务(nginx)
ubuntu 搭建Mercurial 服务(nginx) 环境:ubuntu 12.05 Mercurial 步骤: (1)安装nginx 和 Mercurial: sudo apt-get ins ...
- 【领略RxSwift源码】- 变换操作(Operators)
在上一篇中,我们分析了在RxSwift中的整个订阅流程.在开讲变换操作之前,首先要弄清楚Sink的概念,不清楚的同学可以翻看上一篇的分析.简单的来说,在每一次订阅操作之前都会进行一次Sink对流的操作 ...
- 关于Linux操作系统层次结构分析
本文转自http://www.jb51.net/LINUXjishu/214104.html 首先来看一张图(这是Linux操作系统的大致层次结构): 最内层是硬件,最外层是用户常用的应用,比如说fi ...