UVa 12661 (单源最短路) Funny Car Racing
题意:
有一个赛车跑道,可以看做一个加权有向图。每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒。只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过。
开始时所有跑道处于刚打开的状态,求从起点到终点的最短时间。
分析:
设d[i]为起点到节点i的最短时间。
和普通的单源最短路问题一样,只不过在进行松弛操作的时候分两种情况。松弛的前提是,赛道打开的时间不短于赛车通过的时间。
- 赛车从进入直到出跑道,一直是打开状态。则d[v] = min(d[v], d[u] + t)
- 赛道已经关闭或会在中途关闭,则只能等到下次刚刚打开时进入,因此有个等待时间。d[v] = min(d[v], d[u] + wait + t)
#include <bits/stdc++.h>
using namespace std; const int maxn = + ;
const int INF = ; struct Edge
{
int from, to, a, b, t;
Edge(int u, int v, int a, int b, int t):from(u), to(v), a(a), b(b), t(t) {}
}; vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
int n, m, s, t, d[maxn]; void Init()
{
edges.clear();
for(int i = ; i < n; ++i) G[i].clear();
} void AddEdge(int u, int v, int a, int b, int t)
{
edges.push_back(Edge(u, v, a, b, t));
int m = edges.size();
G[u].push_back(m-);
} void SPFA()
{
memset(inq, false, sizeof(inq));
for(int i = ; i < n; ++i) d[i] = INF;
queue<int> Q;
d[s] = ; inq[s] = true; Q.push(s); while(!Q.empty())
{
int u = Q.front(); Q.pop();
inq[u] = false;
for(int i = ; i < G[u].size(); ++i)
{
Edge& e = edges[G[u][i]];
int v = e.to, a = e.a, b = e.b, t = e.t;
if(a < t) continue;
int now = d[u] % (a+b);
if(now + t <= a)
{//情况一
if(d[v] > d[u] + t)
{
d[v] = d[u] + t;
Q.push(v);
inq[v] = true;
}
}
else
{//情况二
int wait = a + b - now;
if(d[v] > d[u] + wait + t)
{
d[v] = d[u] + wait + t;
Q.push(v);
inq[v] = true;
}
}
}
}
} int main()
{
//freopen("in.txt", "r", stdin); int kase = ;
while(scanf("%d%d%d%d", &n, &m, &s, &t) == )
{
s--; t--;
Init();
for(int i = ; i < m; ++i)
{
int u, v, a, b, t;
scanf("%d%d%d%d%d", &u, &v, &a, &b, &t);
AddEdge(u-, v-, a, b, t);
}
SPFA();
printf("Case %d: %d\n", ++kase, d[t]);
} return ;
}
代码君
UVa 12661 (单源最短路) Funny Car Racing的更多相关文章
- 紫书 习题 11-7 UVa 10801 (单源最短路变形)
把每个电梯口看作一个节点, 然后计算边的权值的时候处理一下, 就ok了. #include<cstdio> #include<vector> #include<queue ...
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)
Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...
- 用scheme语言实现SPFA算法(单源最短路)
最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...
- 单源最短路_SPFA_C++
当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...
- 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)
题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...
- 【算法系列学习】Dijkstra单源最短路 [kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home
https://vjudge.net/contest/66569#problem/A http://blog.csdn.net/wangjian8006/article/details/7871889 ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结
刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...
随机推荐
- windows批处理(cmd/bat)编程详解
reference: http://blog.csdn.net/bingjie1217/article/details/12947327 http://www.cnblogs.com/doit8791 ...
- ora-28002 the password will expire解决办法
Oracle11g R2数据库提示ORA-28002: the password will expire within 5 days,是说密码过期,将Oracle密码设置成永不过期就可以了,不过并不推 ...
- html+css学习笔记 3[浮动]
inline-block/float(浮动) 回顾:inline-block 特性: 1.块在一排显示 2.内联支持宽高 3.默认内容撑开宽度 4.标签之间的换行间隙被解析(问题) 5.ie ...
- Codeforces Round #327 (Div. 2) E. Three States
题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- 解决asp.net mvc中*.resx资源文件访问报错
个人笔记 问题重现 在asp.net mvc中,使用资源文件会出现一个问题,例如: 紧接着我进入视图界面,输入下面代码: <a href="javascript:void(0);&qu ...
- #define x do{......} while(0)的用处
比如定义宏,#define FREE1(p) if (p) free (p)然后这样调用:if (expression)FREE1(p);elseprintf(“expression was fals ...
- crawler
# !/usr/bin/env python# encoding:UTF-8from util import request_urlimport reimport osimport sys#from ...
- 0到N数其中三个数的全排列
#include<iostream> using namespace std; int main(){ ; int count; count=; ;i<=N;i++) ;j<= ...
- 淘宝(taobao)HSF框架
一.背景 随着网站访问量增加,仅仅靠增加机器已不能满足系统的要求,于是需要对应用系统进行垂直拆分和水平拆分.在拆分之后,各个被拆分的模块如何通信?如何保证 性能?如何保证各个应用都以同样的方式交互?这 ...