首页 技术 正文
技术 2022年11月9日
0 收藏 931 点赞 4,657 浏览 1977 个字

把边按权值排序后,就相当于求一个子序列以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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902