POJ 1724 ROADS

来源:互联网 发布:域名升级访问中sdashao 编辑:程序博客网 时间:2024/06/06 04:27

题目链接

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

分析:dijkstra+优先队列+bfs

#include<cstdio>#include<algorithm>#include<vector>#include<iostream>#include<functional>#include<cstring>#include<queue>#define MAX_V 105#define inf 1000000000using 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;}
0 0
原创粉丝点击