POJ1724 ROADS(深搜DFS,最短路,dijkstra,用优先队列优化)

题目:

ROADS

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14825 Accepted: 5370

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

[Submit]   [Go Back]   [Status]   [Discuss]

思路:

先说下题意,题意第一行给了一个k,代表你有k的钱数,下一行有一个n,代表n个点,然后一个m,代表m条边,然后接下来m行,每行有四个数,分别代表起点、终点、路径长度和要花费的钱数,题目想问在花的钱不超过k的情况下,从1—n的最短路径是多少。

我们用两种方法来解决这个问题,第一种办法是深搜,深搜的思路就是用邻接表建图,然后搜索这个图一直更新步数的最小值,第二种是用优先队列优化的迪杰斯特拉算法,我们用dis[i][j]表示从点1到点i时花费为j的最短距离,然后用优先队列时路径小的先出队。

代码1(DFS):

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define N 100+20
#define M 1000000+10
#define ll long long
using namespace std;
int first[N], vis[N];
int k,n,m,len,sum;
struct node
{
    int u,v,w,cost;
    int next;
} g[10200];
void add_edge(int u,int v,int w,int cost)//邻接表建图
{
    g[len].v=v;
    g[len].w=w;
    g[len].cost=cost;
    g[len].next=first[u];
    first[u]=len++;
}
void dfs(int x,int step,int cost)
{
    if(cost>k||step>sum)//剪枝条件,花费大于拥有的钱数和步数大于当前最小的步数
        return;
    if(x==n)
        sum=min(sum,step);//更新步数最小值
    for(int i=first[x]; i!=-1; i=g[i].next)
    {
        int v=g[i].v;
        if(!vis[v])
        {
            vis[v]=1;
            dfs(v,step+g[i].w,cost+g[i].cost);
            vis[v]=0;
        }
    }
}
int main()
{
    len=0;
    int a,b,c,d;
    mem(vis,0);
    mem(first,-1);
    scanf("%d%d%d",&k,&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        add_edge(a,b,c,d);
    }
    sum=inf;
    dfs(1,0,0);
    if(sum==inf)
        puts("-1");
    else
        printf("%d\n",sum);
    return 0;
}

代码2(Dijkstra):

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 10000007
#define N 100+20
#define M 1000000+10
#define ll long long
using namespace std;
int k,n,m,len,sum;
int first[N],dis[N][10005];
struct node1
{
    int v,dis,cost,next;
} g[10010];
struct node
{
    int num,dis,cost;
    bool friend operator < (node a,node b)//重载运算符
    {
        return a.dis>b.dis;
    }
};
void add_edge(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++;
}
void dijkstra()
{
    for(int i=1; i<=n; i++)
        for(int j=0; j<=k; j++)
            dis[i][j]=inf;//dis[i][j]表示从点1到点i花费的钱数为j的最短距离
    dis[1][0]=0;
    priority_queue<node>q;
    node now,to;
    now.num=1;
    now.cost=0;
    now.dis=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        if(now.num==n)//找到的时候直接输出并返回
        {
            printf("%d\n",now.dis);
            return;
        }
        for(int i=first[now.num]; i!=-1; i=g[i].next)
        {
            int cost=now.cost+g[i].cost;
            if(cost>k)continue;
            if(dis[g[i].v][cost]>now.dis+g[i].dis)//松弛条件
            {
                dis[g[i].v][cost]=now.dis+g[i].dis;
                to.num=g[i].v;
                to.cost=cost;
                to.dis=dis[g[i].v][cost];
                q.push(to);
            }
        }
    }
    puts("-1");
}
int main()
{
    len=0;
    int a,b,c,d;
    mem(first,-1);
    scanf("%d%d%d",&k,&n,&m);
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        add_edge(a,b,c,d);
    }
    dijkstra();
    return 0;
}

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/riba2534/article/details/70169857
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞