题意:找花费不超过k的最短路。
分析:dijkstra+优先队列+bfs
#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<functional>
#include<cstring>
#include<queue>
#define MAX_V 105
#define inf 1000000000
using namespace std;
struct edge
{
int to,cost,toll;
edge(){}
edge(int to,int cost,int toll):to(to),cost(cost),toll(toll){}
};
struct P
{
int first;
int second;
int third;
P(int first,int second,int third):first(first),second(second),third(third){}
bool operator<(const P &other)const
{
return first>other.first;
}
};
int V;
vector<edge>G[MAX_V];
int k,r;
int tol;
int dijkstra(int s)
{
priority_queue<P>que;
que.push(P(0,s,0));
while(!que.empty())
{
P p=que.top();
que.pop();
int v=p.second;
if(v==V)
return p.first;
for(int i=0;i<G[v].size();i++)
{
edge e=G[v][i];
if(p.third+e.toll<=k)
que.push(P(p.first+e.cost,e.to,p.third+e.toll));
}
}
return -1;
}
int main(void)
{
while(scanf("%d%d%d",&k,&V,&r)==3)
{
for(int i=0;i<r;i++)
{
int s,e,l,t;
scanf("%d%d%d%d",&s,&e,&l,&t);
G[s].push_back(edge(e,l,t));
// G[e].push_back(edge(s,l,t));
}
tol=0;
printf("%d\n",dijkstra(1));
}
return 0;
}