首页 技术 正文
技术 2022年11月6日
0 收藏 847 点赞 387 浏览 1171 个字

题意:有向图,给你m条边,每条边有两个权值,路径长和通过这条路径的花费,问你在不超过k花费的前提下,最短的路径从1走到n

解题思路:因为边数很少,我们可以直接用暴力每条边的方式来找最小的路径长,也就是用一个优先队列,每次弹出路径最短的边,计算当前花费和下条边的花费如果小于k,那么这条边就可行。

很多博客写是迪杰斯特拉+heap,我觉得没有松弛过程的应该算不上单元最短路的写法把(QAQ);

代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define maxn 100000
#define inf 1e9
using namespace std;
struct Edge
{
int next;
int to;
int w;
int c;
}edge[maxn];
struct node
{
int num;
int dist;
int cost;
node(int _num=0,int _dist=0,int _cost=0):num(_num),dist(_dist),cost(_cost){}
bool operator < (const node &a) const
{
return a.dist<dist;
}};
int head[maxn];
int cnt;
int dist[maxn];
int k,n,m;
void add(int u,int v,int w,int c)
{
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].c=c;
edge[cnt].next=head[u];
head[u]=cnt++;}
int dij(int x)
{
int ans=-1;
node u,v;
priority_queue<node>que;
que.push(node(x,0,0));
while(!que.empty())
{
node u=que.top();
que.pop();
int now=u.num;
if(now==n)
{
ans=u.dist;
return ans;
break;
}
for(int i=head[now];i!=-1;i=edge[i].next)
{
if(u.cost+edge[i].c<=k)
{
v.num=edge[i].to;
v.dist=u.dist+edge[i].w;
v.cost=u.cost+edge[i].c;
que.push(v);
}
}
}
return ans;
}
int main()
{
int x,y,w,c; while(cin>>k>>n>>m)
{
memset(head,-1,sizeof(head));
cnt=0;
for(int i=1;i<=m;i++)
{
cin>>x>>y>>w>>c;
add(x,y,w,c);
}
cout<<dij(1)<<endl;
}}

  

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