很简单的一道最短路问题。分情况处理赛道的打开和关闭。

Code

 /**
* UVa
* Problem#12661
* Accepted
* Time:50ms
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef bool boolean;
#define smin(a, b) (a) = min((a), (b))
#define smax(as, b) (a) = max((a), (b))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) return false;
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} typedef class Edge{
public:
int end;
int next;
int open;
int close;
int t;
Edge(const int end = , const int next = , const int open = , const int close = , const int t = ):end(end), next(next), open(open), close(close), t(t){ }
int across(int arrived){
int re = arrived % (open + close);
if(re >= && re + t <= open) return arrived + t;
return (arrived / (open + close) + ) * (open + close) + t;
}
}Edge; typedef class MapManager{
public:
int ce;
Edge* edges;
int* h;
MapManager():ce(), edges(NULL), h(NULL){ }
MapManager(int points, int limit):ce(){
h = new int[(const int)(points + )];
edges = new Edge[(const int)(limit + )];
memset(h, , sizeof(int) * (points + ));
}
inline void addEdge(int from, int end, int open, int close, int t){
if(t > open) return; //无法通过
edges[++ce] = Edge(end, h[from], open, close, t);
h[from] = ce;
}
Edge& operator [](int pos){
return edges[pos];
}
void clear(){
delete[] edges;
delete[] h;
ce = ;
}
}MapManager; #define m_begin(g, i) (g).h[(i)] int n, m, from, _end;
MapManager g; inline boolean init(){
if(!readInteger(n)) return false;
readInteger(m);
readInteger(from);
readInteger(_end);
g = MapManager(n, m);
for(int i = , u, v, a, b, t; i <= m; i++){
readInteger(u);
readInteger(v);
readInteger(a);
readInteger(b);
readInteger(t);
g.addEdge(u, v, a, b, t);
}
return true;
} boolean* visited;
queue<int> que;
int* f;
inline int spfa(int s, int t){
que.push(s);
visited[s] = true;
f[s] = ;
while(!que.empty()){
int e = que.front();
que.pop();
visited[e] = false;
for(int i = m_begin(g, e); i != ; i = g[i].next){
int& eu = g[i].end;
int cmp = g[i].across(f[e]);
if(cmp < f[eu]){
f[eu] = cmp;
if(!visited[eu] && eu != t){
que.push(eu);
visited[eu] = true;
}
}
}
}
return f[t];
} inline void solve(){
visited = new boolean[(const int)(n + )];
f = new int[(const int)(n + )];
memset(visited, false, sizeof(boolean) * (n + ));
memset(f, 0x7f, sizeof(int) * (n + ));
int res = spfa(from, _end);
printf("%d\n", res);
} inline void clear(){
g.clear();
delete[] visited;
delete[] f;
} int main(){
int kase = ;
while(init()){
printf("Case %d: ", kase++);
solve();
clear();
}
return ;
}

UVa 12661 Funny Car Racing - spfa的更多相关文章

  1. UVa - 12661 - Funny Car Racing

    先上题目: 12661 Funny Car RacingThere is a funny car racing in a city with n junctions and m directed ro ...

  2. UVa 12661 - Funny Car Racing(Dijkstra)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)

    题意:赛道有n个交叉点,和m条单向路径(有重边),每条路都是周期性关闭的,且通过仍需一段时间.在比赛开始时,所有道路刚好打开,选择进入该道路必须满足“在打开的时间段进入,在关闭之前出来”,即不可在路上 ...

  4. UVa 12661 Funny Car Racing (dijkstra)

    题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的, 问你从s到t最短时间是多少. 析:使用d ...

  5. UVA - 12661 Funny Car Racing (Dijkstra算法)

    题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...

  6. UVa 12661 Funny Car Racing【 dijkstra 】

    题意:给出n个点,m条路,每条路用5个整数表示u,v,a,b,t u表示这条路的起点,v表示终点,a表示打开时间,b表示关闭时间,t表示通过这条道路需要的时间 看的紫书,因为边权不再仅仅是路上的时间, ...

  7. UVa 12661 (单源最短路) Funny Car Racing

    题意: 有一个赛车跑道,可以看做一个加权有向图.每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒.只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过. 开始时所有跑道处 ...

  8. UVA 11090 Going in Cycle!! SPFA判断负环+二分

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. UVA 11280 - Flying to Fredericton SPFA变形

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...

随机推荐

  1. Copying and Cloning Objects

    PHP Advanced and Object-OrientedProgrammingVisual Quickpro GuideLarry Ullman class someClass { publi ...

  2. Java applets A Java applet example

    https://en.wikipedia.org/wiki/Ajax_(programming) https://zh.wikipedia.org/wiki/AJAX Ajax (also AJAX; ...

  3. android(五)----使用WakeLock使Android应用程序保持后台唤醒

    在使用一些产品列如微信.QQ之类的,如果有新消息来时,手机屏幕即使在锁屏状态下也会亮起并提示声音,这时用户就知道有新消息来临了. 但是,一般情况下手机锁屏后,Android系统为了省电以及减少CPU消 ...

  4. 深入浅出 TCP/IP 协议栈

    写的不错:http://www.cnblogs.com/onepixel/p/7092302.html#3899256

  5. 【剑指offer】斐波那契数列

    一.题目: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.n<=39 二.思路: 式子: n=0时,f=0:n=1或者n=2时f=1:否则f=f(n-1)+f(n ...

  6. [py]python面向对象的str getattr特殊方法

    本文旨在说清楚 类中的 def init def str def getattr 这三个方法怎么用的. 定制输入实例名时输出内容 def __str__会定制输出实例名时候的输出 class Chai ...

  7. XPath轴

    XPath 轴翻译:Linyupark / 2006-03-24 The XML Example DocumentXML举例文档 We will use the following XML docum ...

  8. Google面试题[一]

    谷歌是不少IT人都想去的企业,那么在进入公司前,少不了面试笔试的测试.那么这里我们就总结了如下谷歌笔试题,并提供了一些参考答案.希望对您有用. 谷歌笔试题:判断一个自然数是否是某个数的平方.当然不能使 ...

  9. 浏览器 extension和plugin的区别[来自知乎]

    "扩展"和"插件",其实都是软件组件的一种形式,Chrome 只不过是把两种类型的组件分别给与了专有名称,一个叫"扩展",另一个叫" ...

  10. [LeetCode] 181. Employees Earning More Than Their Managers_Easy tag: SQL

    The Employee table holds all employees including their managers. Every employee has an Id, and there ...