UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)
题意:赛道有n个交叉点,和m条单向路径(有重边),每条路都是周期性关闭的,且通过仍需一段时间。在比赛开始时,所有道路刚好打开,选择进入该道路必须满足“在打开的时间段进入,在关闭之前出来”,即不可在路上逗留,但是可以在交叉点逗留。问到达终点的时间要多少?
思路:最短路,而且正权,用Dijkstra+优先队列够了。主要的难点在计算是否可以进入该路段,画图清晰点。
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+;
vector<int> vect[]; struct node
{
int from;
int to;
int a;
int b;
int len; }edge[N];
int edge_cnt; void add_node(int u,int v,int a,int b,int t)
{
edge[edge_cnt].from=u;
edge[edge_cnt].to=v;
edge[edge_cnt].a=a;
edge[edge_cnt].b=b;
edge[edge_cnt].len=t;
vect[u].push_back(edge_cnt++);
} int dis[];
bool vis[]; int Dijkstra(int s,int e)
{
memset(vis,,sizeof(vis));
memset(dis,0x7f,sizeof(dis));
priority_queue<pii, vector<pii>, greater<pii> > que;
que.push(make_pair(,s));
dis[s]=; while(!que.empty()) //每次用一个点来更新别人
{
int x=que.top().second; que.pop();
if(vis[x]) continue; //遍历过
vis[x]=;
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if( dis[x]%(e.a+e.b)+e.len<=e.a
&& dis[e.to]>dis[x]+e.len ) //在可通过时间段
{
dis[e.to]=dis[x]+e.len;
que.push(make_pair(dis[e.to],e.to));
}
else if( dis[e.to]>dis[x]+e.len+ (e.a+e.b-dis[x]%(e.a+e.b)) ) //要等待
{
dis[e.to]=dis[x]+e.len+ (e.a+e.b-dis[x]%(e.a+e.b)) ;
que.push(make_pair(dis[e.to],e.to));
}
}
}
return dis[e];
} int main()
{ freopen("input.txt", "r", stdin); int n, m, s, t, u, v, a, b, tt, j=;
while(~scanf("%d%d%d%d",&n,&m,&s,&t))
{
for(int i=; i<=n; i++) vect[i].clear();
memset(edge,,sizeof(edge));
edge_cnt=; for(int i=; i<m; i++)
{
scanf("%d %d %d %d %d", &u, &v, &a, &b, &tt );
if(a>=tt) add_node(u, v, a, b, tt);//去掉废路
}
printf("Case %d: %d\n", ++j, Dijkstra(s,t));
}
return ;
}
AC代码
UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)的更多相关文章
- UVa - 12661 - Funny Car Racing
先上题目: 12661 Funny Car RacingThere is a funny car racing in a city with n junctions and m directed ro ...
- UVa 12661 - Funny Car Racing(Dijkstra)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 12661 Funny Car Racing - spfa
很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code /** * UVa * Problem#12661 * Accepted * Time:50ms */ #include<iost ...
- 洛谷 题解 UVA12661 【有趣的赛车比赛 Funny Car Racing】
[题意] 在一个赛车比赛中,赛道有\(n(n<=300)\)个交叉点和\(m(m<=50000)\)条单向道路.有趣的是,每条道路都是周期性关闭的.每条道路用5个整数\(u,v,a,b,t ...
- UVa 12661 Funny Car Racing (dijkstra)
题意:给定一个有向图,每条路有5个整数修饰,u, v, a, b, t,表示起点为u,终点为v,打开时间a,关闭时间为b,通过时间为t,打开关闭是交替进行的, 问你从s到t最短时间是多少. 析:使用d ...
- UVA - 12661 Funny Car Racing (Dijkstra算法)
题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...
- UVa 12661 Funny Car Racing【 dijkstra 】
题意:给出n个点,m条路,每条路用5个整数表示u,v,a,b,t u表示这条路的起点,v表示终点,a表示打开时间,b表示关闭时间,t表示通过这条道路需要的时间 看的紫书,因为边权不再仅仅是路上的时间, ...
- 题解-[HNOI2001]遥控赛车比赛
题解-[HNOI2001]遥控赛车比赛 前置知识:记忆化搜索.\(\texttt{Bfs}\). 参考资料 https://www.luogu.com.cn/blog/CYJian/solution- ...
- UVa 12661 (单源最短路) Funny Car Racing
题意: 有一个赛车跑道,可以看做一个加权有向图.每个跑道(有向边)还有一个特点就是,会周期性地打开a秒,然后关闭b秒.只有在赛车进入一直到出来,该跑道一直处于打开状态,赛车才能通过. 开始时所有跑道处 ...
随机推荐
- zhuan: ubuntu 安装 apache2
安装 用 sudo apt-get install apache2 sudo /etc/init.d/apache2 restart 如果发现错误: 以下from:http://cache.baid ...
- ZOJ 1074 最大子矩阵和
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 用JS给浏览器的关闭按钮添加事件
以下是指在js中实现,而非 <body onunload="close()"> 这种方法! 因为这样是在unload掉body的时候触发,而无论任何浏览器,都会在关闭的 ...
- JSP页面时间动态显示 (转载)
<script type="text/javascript"> function startTime(){ var today=new Dat ...
- sybase下convert函数第三个参数(时间格式)
convert(varchar(10),字段名,转换格式) 比如:1.select user_id,convert(varchar(10),dayts,11) as dates from tb_use ...
- python logging 日志轮转文件不删除问题
前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...
- Mac环境下装node.js,npm,express;(包括express command not found)
1. 下载node.js for Mac 地址: http://nodejs.org/download/ 直接下载 pkg的,双击安装,一路点next,很容易就搞定了. 安装完会提醒注意 node和n ...
- vs2010 使用SignalR 提高B2C商城用户体验(二)
vs2010 使用SignalR 提高B2C商城用户体验(二) 上一节,已经实现了,当前域内的通信,这一节中,介绍一下跨域的即时通信,既然要做,我们肯定要把这个推送及聊天服务器做为一个单独的服务器,以 ...
- 1048: [HAOI2007]分割矩阵 - BZOJ
Description 将一个a*b的数字矩阵进行如下分割:将原矩阵沿某一条直线分割成两个矩阵,再将生成的两个矩阵继续如此分割(当然也可以只分割其中的一个),这样分割了(n-1)次后,原矩阵被分割成了 ...
- MySQL的基本命令
MySQL的基本命令 启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库 ...