ROADS POJ - 1724(分层最短路)

Posted wtsruvf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROADS POJ - 1724(分层最短路)相关的知识,希望对你有一定的参考价值。

 

  就是在最短路的基础上   多加了一个时间的限制 , 多一个限制多一维就好了  记住 分层最短路要用dijistra !!!  

  

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d
", a);
#define plld(a) printf("%lld
", a);
#define pc(a) printf("%c
", a);
#define ps(a) printf("%s
", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int k, n, m;
int dis[maxn][110], vis[maxn][110], head[maxn];
int cnt;

struct edge
{
    int u, v, d, t;
    edge(int u, int v, int d, int t) : u(u), v(v), d(d), t(t) {}
};

vector<edge> Edge;
vector<int> G[maxn << 1];
struct node
{
    int u, d, t;
    node(int u, int d, int t) : u(u), d(d), t(t) {}
    bool operator < (const node &a) const {
        return d > a.d;
    }
};

void add(int u, int v, int d, int t)
{
    Edge.push_back(edge(u, v, d, t));
    G[u].push_back(Edge.size() - 1);
}

void dijkstra()
{
    priority_queue<node> Q;
    for(int i = 0; i < maxn; i++) for(int j = 0; j < 110; j++) dis[i][j] = INF;
    mem(vis, 0);
    Q.push(node(1, 0, 0));
    dis[0][1] = 0;
   // vis[0][1] = 1;
    while(!Q.empty())
    {
        node x = Q.top(); Q.pop();
        int u = x.u;
        int t = x.t;
        if(vis[t][u]) continue;
        vis[t][u] = 1;
        for(int i = 0; i < G[u].size(); i++)
        {
            edge e = Edge[G[u][i]];
            if(dis[t + e.t][e.v] > dis[t][u] + e.d && t + e.t <= k)
            {
                dis[t + e.t][e.v] = dis[t][u] + e.d;
                Q.push(node(e.v, dis[t + e.t][e.v], t + e.t));
            }
        }
    }
}

int main()
{
    int u, v, d, t;
    rd(k), rd(n), rd(m);
    for(int i = 1; i <= m; i++)
    {
        rd(u), rd(v), rd(d), rd(t);
        add(u, v, d, t);
    }
    dijkstra();
    int maxx = INF;
    for(int i = 0; i <= k; i++)
        maxx = min(maxx, dis[i][n]);
    if(maxx == INF)
        return puts("-1"), 0;
    cout << maxx << endl;

    return 0;
}

 

以上是关于ROADS POJ - 1724(分层最短路)的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

最短路练习 poj1724

[poj1724]Roads

ROADS POJ - 1724(拆点)

ROADS POJ - 1724(拆点)

poj1724 ROADS