UVa 12661 Funny Car Racing (dijkstra)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVa 12661 Funny Car Racing (dijkstra)相关的知识,希望对你有一定的参考价值。

题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的,

问你从s到t最短时间是多少。

析:使用dijkstra算法,从每个结点出发,求最短路,并维护时间的最小值,这个可以用优先队列,然后考虑能不能通过这条路,如果t<a,可以在输入时处理。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 300 + 10;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
  int to, a, b, t;
};
vector<Node> G[maxn];
int d[maxn];

int dijkstra(int s, int ttt){
  priority_queue<P, vector<P>, greater<P> > pq;
  pq.push(P(0, s));
  fill(d, d+n+1, INF);
  d[s] = 0;

  while(!pq.empty()){
    P p = pq.top();  pq.pop();
    if(p.second == ttt)  return p.first;
    int v = p.second;
    if(d[v] < p.first)  continue;
    for(int i = 0; i < G[v].size(); ++i){
      Node &u = G[v][i];
      int t =  p.first % (u.a+u.b);
      if(t + u.t <= u.a && d[u.to] > d[v] + u.t){
        d[u.to] = d[v] + u.t;
        pq.push(P(d[u.to], u.to));
      }
      else if(t + u.t > u.a){
        int tt = u.a + u.b - t + u.t;
        if(d[u.to] > d[v] + tt){
          d[u.to] = d[v] + tt;
          pq.push(P(d[u.to], u.to));
        }
      }
    }
  }
  return 0;
}

int main(){
  int kase = 0;
  int s, t;
  while(scanf("%d %d %d %d", &n, &m, &s, &t) == 4){
    int u, v, a, b, tt;
    for(int i = 1; i <= n; ++i)  G[i].clear();
    while(m--){
      scanf("%d %d %d %d %d", &u, &v, &a, &b, &tt);
      if(tt > a)  continue;
      G[u].push_back((Node){v, a, b, tt});
    }
    printf("Case %d: %d\n", ++kase, dijkstra(s, t));
  }
  return 0;
}

 

以上是关于UVa 12661 Funny Car Racing (dijkstra)的主要内容,如果未能解决你的问题,请参考以下文章

UVa 12661 - Funny Car Racing(Dijkstra)

UVa 12661 Funny Car Racing (dijkstra)

最短路 uva12661 Funny Car Racing

UVA - 12661 Funny Car Racing (Dijkstra算法)

CSU 1333 & Uva 12661 Funny Car Racing最短路变形+spfa算法,链式前向星建图

Funny Car Racing - 最短路小技巧