最短路 spfa POJ1724ROADS

ROADS
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12091 Accepted: 4465
Description
N cities named with numbers 1 … N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash.

We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has.
Input
The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way.
The second line contains the integer N, 2 <= N <= 100, the total number of cities.

The third line contains the integer R, 1 <= R <= 10000, the total number of roads.

Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :
• S is the source city, 1 <= S <= N
• D is the destination city, 1 <= D <= N
• L is the road length, 1 <= L <= 100
• T is the toll (expressed in the number of coins), 0 <= T <=100

Notice that different roads may have the same source and destination cities.
Output
The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins.
If such path does not exist, only number -1 should be written to the output.
Sample Input
5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2
Sample Output
11
Source
CEOI 1998
此题是最短路径的改进版本,实际上就是双重标准最短路问题。难点是松弛的时候还要考虑到有费用的限制。解法要点是对每个点开一个长度为k的数组,dis[i][j]表示从起始点(结点1)到结点i的费用不大于j的 最短路长度。最后返回dis[n][k]即可。其余同spfa,更新结点时将dis[i][1]…dis[i][k]全部更新。
最后,dis[n][k]即为所求
类似动态规划

//1692K 329MS
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<cstdio>
#define MAXEDGE 10005
#define MAXCITY 105
#define inf 1000000000
using namespace std;
int _count,k,IncityNum,InedgeNum;
int dis[MAXCITY][MAXEDGE];
int adjacentEdge[MAXEDGE];
int vis[MAXCITY];
struct EDGE{
    int endV,nextRoad,weight,cost;
}edge[MAXEDGE];
void init(){
    for(int i=2;i<=IncityNum;i++)
      for(int j=0;j<=k;j++)
        dis[i][j]=inf;
    _count=0;
    memset(adjacentEdge,-1,sizeof(adjacentEdge));//边从0开始编号,号为-1表示已经遍历完邻边
    memset(vis,0,sizeof(vis));
}
void addEdge(int startV,int endV,int weight,int cost){
    edge[_count].cost=cost;
    edge[_count].endV=endV;
    edge[_count].weight=weight;
    edge[_count].nextRoad=adjacentEdge[startV];//令起始点startV的上一个邻边为当前邻边的下一边
    adjacentEdge[startV]=_count++;//将当前
}
void spfa(){
  queue<int>q;
  q.push(1);
  while(!q.empty()){
int frontV=q.front();q.pop();vis[frontV]=0;
    for(int i=adjacentEdge[frontV];i!=-1;i=edge[i].nextRoad){//遍历队首点的每条邻边
        for(int j=edge[i].cost;j<=k;j++){
            if(dis[edge[i].endV][j]>dis[frontV][j-edge[i].cost]+edge[i].weight){
                dis[edge[i].endV][j]=dis[frontV][j-edge[i].cost]+edge[i].weight;
                if(!vis[edge[i].endV]){//没在队列里
                    vis[edge[i].endV]=1;
                    q.push(edge[i].endV);
                }
            }
        }
    }
  }
 printf("%d\n",inf==dis[IncityNum][k]?-1:dis[IncityNum][k]);
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int startV,endV,weight,cost;
    while(scanf("%d%d%d",&k,&IncityNum,&InedgeNum)!=EOF){
        init();
        for(int i=1;i<=InedgeNum;i++){//边从1开始编号
            scanf("%d%d%d%d",&startV,&endV,&weight,&cost);
            addEdge(startV,endV,weight,cost);
        }
        spfa();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值