POJ 1724 ROADS

题目链接

题意:找花费不超过k的最短路。

分析:dijkstra+优先队列+bfs

#include
#include
#include
#include
#include
#include
#include
#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;
vectorG[MAX_V];
int k,r;
int tol;
int dijkstra(int s)
{
    priority_queue

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;iif(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;iint 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; }

你可能感兴趣的:(图论)