CSU - 1333 1333: Funny Car Racing(spfa)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333
这题多了一个限制条件是每一条路都会规律的开放a时间关闭b时间,车子必须在开放的时候进入,在关闭之前出来,那么在加边的时候只要权值>开放时间的就不用加进去了.
还有一个问题是重边,那么用邻接表存储就好,并且这题不用spfa就超时,至于判断到达某一个点的时间,只要比较dist[x]%(temp.a+temp.b)的数即可.
算出来之后a需要判断是否还能够在剩余时间通过这个路口,不然就需要等待。还有算出需要等待的时间后必须判断是不是能更新目标点,否则会wa.
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include <deque>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define INF 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 310
#define maxv 50010
#define mod 1000000000
using namespace std; struct edge
{
int to,weight,a,b;
};
vector<edge>adjmap[maxv]; //动态邻接表
bool in_queue[maxn]; //顶点是否在队列中
int dist[maxn];//源点到各点的最短路径
int nodesum; //顶点数
int edgesum; //边数
int S,T;//起点和终点 void SPFA(int source)
{
deque<int>dq;
int i,j,x,to;
for(int i=;i<=nodesum;i++)
{
in_queue[i]=false;
dist[i]=INF;
}
dq.push_back(source);
dist[source]=;
in_queue[source]=true;
//初始化 完成
while(!dq.empty())
{
x=dq.front(); //取出队首元素
dq.pop_front();
in_queue[x]=false; //访问标记置0 ,同一个顶点可以访问多次,只要dist值变小
for(i=;i<adjmap[x].size();i++)
{
to=adjmap[x][i].to;
edge temp=adjmap[x][i];
// cout<<temp.a<<" "<<temp.b<<endl;
if((dist[x]<INF)&&(dist[to]>dist[x]+temp.weight))
{
int t=dist[x]%(temp.a+temp.b),t1;
//cout<<dist[x]<<" "<<t<<endl;
if(t<=temp.a)
{
if(temp.a-t-temp.weight>=) t1=temp.weight;
else t1=temp.a-t+temp.b+temp.weight;
}
else t1=temp.a+temp.b-t+temp.weight;
// cout<<t1<<endl;
if(dist[to]>dist[x]+t1) dist[to]=dist[x]+t1;
else continue; //注意这里
if(!in_queue[to])
{
in_queue[to]=true;
if(!dq.empty())
{
if(dist[to]>dist[dq.front()]) dq.push_back(to); //大的加入 队尾
else dq.push_front(to); //小的加入队首
}
else dq.push_back(to); //队列为空加入队尾
}
}
}
}
} int main()
{
//Read;
int i,s,e,w,x,y,j=;
edge temp;
while(cin>>nodesum>>edgesum>>S>>T)
{
for(int i=;i<=nodesum;i++) adjmap[i].clear();
for(int i=;i<=edgesum;i++)
{
cin>>s>>e>>x>>y>>w;
if(w<=x)
{
temp.to=e;
temp.weight=w;
temp.a=x,temp.b=y;
adjmap[s].push_back(temp);
}
}
SPFA(S);
cout<<"Case "<<j++<<":"<<" "<<dist[T]<<endl;
}
return ;
}
CSU - 1333 1333: Funny Car Racing(spfa)的更多相关文章
- UVa 12661 Funny Car Racing - spfa
很简单的一道最短路问题.分情况处理赛道的打开和关闭. Code /** * UVa * Problem#12661 * Accepted * Time:50ms */ #include<iost ...
- Funny Car Racing CSU - 1333 (spfa)
There is a funny car racing in a city with n junctions and m directed roads. The funny part is: each ...
- CSU 1333 Funny Car Racing (最短路)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1333 解题报告:一个图里面有n个点和m条单向边,注意是单向边,然后每条路开a秒关闭b秒 ...
- CSU 1333 Funny Car Racing
最短路问题稍微复杂了一点,松弛的时候多判断一些条件就可以了.第一次用SPFA写最短路. #include<cstdio> #include<cmath> #include< ...
- <一道题>abc+cba=1333,求满足条件的abc的值,隐含条件a!=0,c!=0
这类东西,无非就是穷举法.见下面代码: #include <stdio.h> #include <stdlib.h> /* *abc + cba = 1333 * *a = ? ...
- ural 1333 化平面为点计算覆盖率
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1333 #include<cstdio> #include<cstrin ...
- hihocoder#1333 : 平衡树·Splay2 (区间操作)
题面: #1333 : 平衡树·Splay2 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:好麻烦啊~~~~~ 小Hi:小Ho你在干嘛呢? 小Ho:我在干活啊! ...
- 1333:【例2-2】Blah数集
1333:[例2-2]Blah数集 注意是数组,答案数组中不能有重复数字 q数组是存储答案的 代码: #include<iostream> #include<cstdio> # ...
- 用js写已知abc+cba = 1333,其中a、b、c均为一位数,编程求出满足条件的a、b、c所有组合。
<!--<script type="text/javascript"> //已知abc+cba = 1333,其中a.b.c均为一位数,编程求出满足条件的a.b. ...
随机推荐
- php中除法取整的方法(round,ceil,floor)
PHP中遇到需要将除法所得结果取整的情况时,就需要用到以下方法: 1. round:四舍五入 round() 函数对浮点数进行四舍五入. 语法:round(x, prec) 参数 描述 x 可选.规定 ...
- mysql之通过cmd连接远程数据库
---恢复内容开始--- 目录 前提 连接远程数据库 前提: 本地安装了mysql数据库 本地和远程网络是连通的,通过命令ping ip (即ping 192.168.0.333),可以ping通 连 ...
- javascript之input获取的时间减1秒&&t时间恢复
将输入得到的时间减少1秒:20:00:00 ——— 19:59:59 方法一:普通时间转换 endDateMap(date){ var h = new Date(date).getHours ...
- 对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流: Ob ...
- 一台机器运行多个JBoss多实例
JBossXMLJVMTomcat应用服务器 我们经常会遇到这种情况,有时候希望在同一台机器上部署若干个JBoss实例,上面运行不同的应用程序,这样的话无论由于什么原因需要对某个JBoss实例进行关 ...
- CREATE TABLE - 定义一个新表
SYNOPSIS CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name ( { column_name data_ty ...
- Poi 写入图片进入excel
public static void cacheWritePicture(BufferedImage bufferImg, Sheet sheet, Workbook wb, int width, i ...
- BZOJ4318: OSU! (概率DP)
题意:一个串 给出每个字符为1的可能性 否则为0 一段连续的1能获得长度的立方的收益 问总收益的期望 题解:设x_i为到第i位时连续的1的期望长度 由i-1递推来的贡献 如果这一位是0没有贡献 如果是 ...
- Java中List集合的遍历
一.对List的遍历有三种方式 List<String> list = new ArrayList<String>(); list.add("testone" ...
- Tab键可访问的下拉菜单demo
<ul id="navigationRegion"> <li token="1" class="index_on"> ...