用dij实现 最短路限制下的bfs 比较难理解
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
struct state{
int n,d,c; // n 当前点,d 距离 c 话费
bool operator < (const struct state a) const{
if(a.d == d) return a.c < c;
return a.d < d;
}
};
struct edg {
int u,v,w,c,next;
};
edg data[10010]={};
int tot;
int head[110]={}; int cost,n,m;
void add(int u,int v,int w,int c) {
data[tot].u = u; data[tot].v = v;
data[tot].w = w; data[tot].c = c;
data[tot].next = head[u];
head[u] = tot++;
}
void dij() {
priority_queue<state>q;
state sta; int res = inf;
sta.d = 0; // ??
sta.n = 1; // 点
sta.c = 0;// 话费
q.push(sta);
while(!q.empty()) {
state x,y;
int u,v,w,c,d;
x = q.top();
q.pop();
u = x.n; d = x.d;
if(u == n) { res = x.d; break; } // 到大目标点
for(int k = head[u]; k + 1; k = data[k].next) {
v = data[k].v; w = data[k].w;
c = data[k].c;
if(x.c + c <= cost) {
y.n = v;
y.d = d + w;
y.c = x.c + c;
q.push(y);
}
}
}
if(res == inf) printf("-1\n");
else printf("%d\n",res);
}
int main() {
memset(head,-1,sizeof(head));
scanf("%d%d%d",&cost,&n,&m); tot = 0;
while(m--) {
int u,v,w,c;
scanf("%d%d%d%d",&u,&v,&w,&c);
add(u,v,w,c);
}
dij();
return 0;
}