ROADS//POJ - 1724//dijkstra

非 Y 不嫁゛ 提交于 2020-02-04 15:55:29

ROADS//POJ - 1724//dijkstra


题目

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
题意
给你一定数量钱,4个数据是两顶点,边长和价格,要求不超过所有钱求最短路
链接:https://vjudge.net/contest/351234#problem/F

思路

用优先队列优化的dijkstra

代码

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>

using namespace std;
const int INF=0x3f3f3f3f;
struct road{
    int v,dis,cost,next;
}G[10005];

struct Road{
    int u,dis,cost;
    Road(int U,int DIS,int COST){                //方便存入队列
        u=U,dis=DIS,cost=COST;
    }
    bool operator < (const Road&x)const{         //优先队列从小到大排序
        return dis>x.dis;
    }
};
int first[105],d[105][10005];                   //d[i][j]表示从点1到点i花费j的路程
int n,m,k,len,ans;                               //用数组模拟链表

void dij(){
    for(int i=1; i<=n; ++i)
        for(int j=0; j<=k; ++j)
            d[i][j]=INF;
    d[1][0]=0;
    priority_queue<Road>Q;
    Q.push(Road(1,0,0));
    while(!Q.empty()){
        Road q=Q.top();
        Q.pop();
        if(q.cost>k||q.dis>d[q.u][q.cost])
            continue;
        if(q.u==n){
            printf("%d\n",q.dis);
            return ;
        }
        for(int i=first[q.u]; i!=-1; i=G[i].next){
            int v=G[i].v,dis=G[i].dis,cost=G[i].cost+q.cost;
            if(d[v][cost]>q.dis+dis){
                d[v][cost]=q.dis+dis;
                Q.push(Road(v,d[v][cost],cost));
            }
        }
    }
    printf("-1\n");
}

void addn(int u,int v,int dis,int cost)
{
    G[len].v=v,G[len].dis=dis,G[len].cost=cost;
    G[len].next=first[u];
    first[u]=len++;
}

int main()
{
    memset(first,-1,sizeof(first));
    scanf("%d%d%d",&k,&n,&m);
    int u,v,dis,cost;
    len=0;
    for(int i=1; i<=m; ++i){
        scanf("%d%d%d%d",&u,&v,&dis,&cost);
        addn(u,v,dis,cost);
    }
    ans=INF;
    dij();
    return 0;
}

注意

用数组表示链表
用二维数组表示1到x点的费用和距离

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!