限制费用的最短路 例题:poj 1724

原题  poj  1724:http://poj.org/problem?id=1724



题意:给你钱数 k,有n个城市(编号1~n),r 条路

然后花费要在k以内,从1到达n城市的最短路。


解法:方法很多:可以用优先队列+bfs(/dijkstra),也可以用spfa+dfs(/dp),也可以用vector 存储一下边的关系。


代码1:优先队列+bfs

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include<queue>
#include<algorithm>
using namespace std;
#define N 120
int tot;
int first[N];
 int k,n,r;
struct node
{
    int u,l,c;
    friend bool operator <(node a,node b)
    {
        if(a.l==b.l)
            return a.c>b.c;
        else
            return a.l>b.l;
    }
};
struct edge
{
    int v,l,c,next;

}e[N*N];
void addedge(int u,int v,int l,int c,int &tot)
{
    e[tot].v=v;
    e[tot].l=l;
    e[tot].c=c;
    e[tot].next=first[u];
    first[u]=tot++;
}
void init()
{
    memset(first,-1,sizeof(first));
    tot=0;
}
int bfs()
{
    priority_queue<node> q;
    node x;
    x.u=1,x.c=0,x.l=0;
    q.push(x);
    while(!q.empty())
    {
        node now=q.top();
        q.pop();
        if(now.u==n)
            return now.l;
        for(int i=first[now.u];i!=-1;i=e[i].next)
        {

       int v=e[i].v,l=e[i].l,c=e[i].c;
            if(now.c+c>k)
                continue;
                node nn;
                nn.u=v;
                nn.c=now.c+c;
                nn.l=now.l+l;
            q.push(nn);
        }
    }
    return -1;
}
int main()
{
    init();
scanf("%d%d%d",&k,&n,&r);
    //cin>>k>>n>>r;
    int u,v,l,c;
    for(int i=0;i<r;i++)
    {
        scanf("%d%d%d%d",&u,&v,&l,&c);
        //cin>>u>>v>>l>>c;
        addedge(u,v,l,c,tot);
    }
    printf("%d\n",bfs());
    return 0;
    //cout<<bfs()<<endl;
}

代码2:用vector存边


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
#define N 120
int k,n,r;
struct node
{
    int v,l,c;
    friend bool operator <(node a,node b)
    {
        if(a.l==b.l)
            return a.c>b.c;
        else
            return a.l>b.l;
    }
};
vector<node>e[N];
int bfs()
{
    priority_queue<node> q;
    node x;
    x.v=1,x.c=0,x.l=0;
    q.push(x);
    while(!q.empty())
    {
        node now=q.top();
        q.pop();
        if(now.v==n)
            return now.l;
        for(int i=0; i<e[now.v].size(); i++)
        {
            if(now.c+e[now.v][i].c<=k)
            {
                node nn;
                nn.v=e[now.v][i].v;
                nn.c=now.c+e[now.v][i].c;
                nn.l=now.l+e[now.v][i].l;
                q.push(nn);
            }
        }
    }
    return -1;
}
int main()
{
    scanf("%d%d%d",&k,&n,&r);
    int u,v,l,c;
    for(int i=0; i<r; i++)
    {
        scanf("%d%d%d%d",&u,&v,&l,&c);
        e[u].push_back((node){v,l,c});
    }
    printf("%d\n",bfs());
    return 0;
}



最短路算法有:dijkstra、spfa、floyd、Bellman_Ford

四种算法的模版:http://blog.csdn.net/zhongyanghu27/article/details/8221276

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值