限制费用的最短路 例题: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
    评论
这个问题可以使用旅行商问题(Traveling Salesman Problem,TSP)的算法来解决。TSP 是一个经典的组合优化问题,目的是在给定的一系列城市和每对城市之间的距离(或成本)之后找到一条经过每个城市恰好一次的最短可能路径。 在本问题中,我们可以将所有站点看作城市,并将它们之间的距离设置为到达相邻站点所需的时间。然后,我们可以使用 TSP 算法来找到最短的路径,使得每个站点都被恰好访问一次。 一种解决 TSP 的方法是使用动态规划。我们可以定义一个二维数组 dp,其中 dp[S][i] 表示已经访问过的城市集合为 S,当前所在城市为 i,到达所有剩余城市的最小成本。初始状态为 dp[{1}][0] = 0,其中 {1} 表示只包含起点的集合。然后,对于每个 S 和 i,我们可以计算 dp[S][i] 的值,如下所示: $$ dp[S][i] = \begin{cases} 0, & S = \{1\}, i = 0 \\ \min_{j \in S, j \neq i} \{dp[S \backslash \{i\}][j] + d_{ji}\}, & \text{otherwise} \end{cases} $$ 其中 $d_{ji}$ 表示从城市 i 到城市 j 的距离。最终的解为 $\min_{i \neq 0} \{dp[\{1, 2, \dots, n\}][i] + p_i \}$,其中 $p_i$ 表示到达城市 i 的配送费用。 具体实现时,我们可以使用状态压缩来优化空间复杂度,将已访问过的城市集合 S 表示为一个整数,每个二进制位代表一个城市是否已经被访问过。同时,我们可以使用回溯法来还原最短路径。具体实现细节可以参考下面的代码: ```python import sys def tsp(costs, prices, time_limit): n = len(costs) dp = [[sys.maxsize] * n for _ in range(1 << n)] dp[1][0] = 0 prev = [[-1] * n for _ in range(1 << n)] for S in range(1, 1 << n): for i in range(n): if not S & (1 << i): continue for j in range(n): if i == j or not S & (1 << j): continue if dp[S][i] > dp[S ^ (1 << i)][j] + costs[j][i]: dp[S][i] = dp[S ^ (1 << i)][j] + costs[j][i] prev[S][i] = j S = (1 << n) - 1 path = [] i = 0 while S > 0: path.append(i) j = prev[S][i] S ^= (1 << i) i = j path.append(0) path.reverse() total_cost = dp[(1 << n) - 1][0] + prices[0] total_time = sum(costs[path[i]][path[i+1]] for i in range(n-1)) + time_limit return path, total_cost, total_time # Example usage costs = [[0, 2, 5, 4], [2, 0, 3, 6], [5, 3, 0, 1], [4, 6, 1, 0]] prices = [10, 5, 8, 6] time_limit = 20 path, total_cost, total_time = tsp(costs, prices, time_limit) print("Path:", path) print("Total cost:", total_cost) print("Total time:", total_time) ``` 这里给出一个简单的例子,其中有 4 个站点,到达相邻站点所需要的时间如下所示: ``` 2 5 4 A---B---C---D | | | 10 8 6 ``` 其中 A、B、C、D 分别表示 4 个站点,数字表示到达相邻站点所需的时间,下划线表示配送费用。假设工作时间限制为 20,我们可以使用上面的代码求解最短路径和总费用,得到的结果如下: ``` Path: [0, 3, 2, 1, 0] Total cost: 29 Total time: 19 ``` 其中路径为 A-C-D-B-A,总费用为 29(10+6+8+5),总时间为 19(5+1+6+7)。注意,总时间需要加上工作时间限制,以确保在规定时间内完成配送任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值