sgu 103 Traffic Lights
这道题难得不是算法,而是处理。
题意就是让你求最短路,只有当两个点在某一秒颜色相同时,这条边才可以通行,输入首先给你 起点和终点, 然后给你 点数和边数, 接下来 n 行 初始颜色,初始颜色持续时间,蓝色持续时间,紫色持续时间。 再接下来m行,无向边的起点和终点以及通过所需的时间。
题意他说的有些模糊,样例我看了很多遍也不对,后来才发现如果你在某一秒到达一个点,这一秒颜色和下一个点相同,但是下一秒这个点就要变色,那么是不能在这一秒走的,这个具体处理起来很麻烦
这篇博客说的很详细,戳链接:http://www.cnblogs.com/Rinyo/archive/2012/11/29/2795030.html
上代码……
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#define N 350
#define M 15000*2
#define inf 1<<30
using namespace std; int n, m, S, T;
int p[N] = {}, next[M], v[M], bnum = , c[M], fa[N];
int dis[N], vis[N], firstcolor[N], firstremain[N], remain[N][]; void addbian(int x, int y, int z)
{
bnum++; next[bnum] = p[x]; p[x] = bnum;
v[bnum] = y; c[bnum] = z;
bnum++; next[bnum] = p[y]; p[y] = bnum;
v[bnum] = x; c[bnum] = z;
} void calt(int now, int nowtime, int &color, int &changetime)
{
if (nowtime < firstremain[now])
{
color = firstcolor[now];
changetime = firstremain[now];
return;
}
int k;
k = (nowtime-firstremain[now]) % (remain[now][]+remain[now][]);
nowtime -= k;
if (firstcolor[now])
{
if (k < remain[now][]) { color = ; changetime = nowtime + remain[now][]; }
else { color = ; changetime = nowtime + remain[now][] + remain[now][]; }
}
else
{
if (k < remain[now][]) { color = ; changetime = nowtime + remain[now][]; }
else { color = ; changetime = nowtime + remain[now][] + remain[now][]; }
}
} int gettime(int x, int y, int nowtime, int dis, int f)
{
int ca, cb, ta, tb, ta1, ca1;
calt(x, nowtime, ca, ta);
calt(y, nowtime, cb, tb);
if (ca == cb) return nowtime + dis;
if (ta == tb)
{
if (f == ) return gettime(x, y, ta, dis, f+);
else if (nowtime <= firstremain[x] || nowtime <= firstremain[y])
gettime(x, y, ta, dis, f+);
else return inf;
}
return min(ta, tb) + dis;
} void spfa()
{
queue<int> q;
for (int i = ; i <= n; ++i) { vis[i] = ; dis[i] = inf; }
q.push(S); dis[S] = ; vis[S] = ;
while (!q.empty())
{
int now = q.front(); q.pop();
int k = p[now];
while (k != )
{
int t = gettime(now, v[k], dis[now], c[k], );
if (dis[v[k]] > t)
{
dis[v[k]] = t; fa[v[k]] = now;
if (!vis[v[k]])
{
vis[v[k]] = ;
q.push(v[k]);
}
}
k = next[k];
}
vis[now] = ;
}
} void print(int now)
{
if (now == S) printf("%d ", S);
else
{
print(fa[now]);
if (now != T) printf("%d ", now);
else printf("%d\n", T);
}
} int main()
{
scanf("%d%d", &S, &T);
scanf("%d%d", &n, &m);
for (int i = ; i <= n; ++i)
{
char s[];
scanf("%s", s);
if (s[] == 'B') firstcolor[i] = ;
else firstcolor[i] = ;
scanf("%d%d%d", &firstremain[i], &remain[i][], &remain[i][]);
}
for (int i = ; i <= m; ++i)
{
int x, y, z; scanf("%d%d%d", &x, &y, &z);
addbian(x, y, z);
}
spfa();
if (dis[T] == inf)
{
printf("0\n");
return ;
}
printf("%d\n", dis[T]);
print(T);
}
sgu 103 Traffic Lights的更多相关文章
- sgu 103 Traffic Lights 解题报告及测试数据
103. Traffic Lights Time limit per test: 0.25 second(s) Memory limit: 4096 kilobytes 题解: 1.其实就是求两点间的 ...
- SGU 103.Traffic Lights(最短路)
时间: 0.50 second(s) 空间: 4096 kilobytes 输入: 标准输入 输出: 标准输出 Dingiville 城市的交通规则非常奇怪,城市公路通过路口相连,两个不同路口之间最多 ...
- SGU 103 Traffic Lights【最短路】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=16530 题意: 给定每个点最初的颜色,最初颜色持续时间,以 ...
- 快速切题 sgu103. Traffic Lights 最短路 难度:1
103. Traffic Lights Time limit per test: 0.25 second(s)Memory limit: 4096 kilobytes input: standardo ...
- Traffic Lights
Traffic Lights time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- POJ1158 城市交通Traffic lights IOI 1999 (最短路)
POJ1158 城市交通Traffic lights IOI 1999 (最短路) (1) 问题描述(probolem) 在d城里交通的安排不同寻常,城中有路口和路口之间的道路,再任意两个不同的路口之 ...
- Traffic Lights - SGU 103(最短路)
题目大意:有一个城市的路线图,有N个交叉点,每两个交叉点之间只有一条路,现在想从交点u去交点v,不过这个路的交通比较特别,每个路都有一个交通灯,灯有两种颜色,蓝色和紫色,例如一条路线在交点s,t之间, ...
- [atAGC050E]Three Traffic Lights
原题意可能略微有一些复杂,这里给出简述的题意-- 给定$g_{i}$和$r_{i}$(其中$1\le i\le 3$),求有多少个整数$t$满足: $0\le t< \prod_{i=1}^{3 ...
- TRAFFIC LIGHTS POJ 1158
题目大意: 在Dingilville 城市安排是一种不同寻常的方式,每个交叉路口有一条道路连接,一条道路最多连接两个不同的交叉路口.每个交叉路口不能连接他自己.道路旅行一端到另一端的时间是相同的,任何 ...
随机推荐
- iOS开发-自动布局和自动旋转
今天学习自动布局中的自动调整尺寸大小. 一.尺寸分类 尺寸分类是对设备宽高的一种大致分类. 有两种具体的尺寸分类用来表示真机:紧凑(Compact)和标准(Regular).还有第三种分类可以在设计工 ...
- 我的AndroidStudio设置
转载:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1/ 官方下载有两个地方,均需要FQ. Android Deve ...
- java.io.File中的pathSeparator与separator的区别
先总的说一下区别:File.pathSeparator指的是分隔连续多个路径字符串的分隔符,例如:java -cp test.jar;abc.jar HelloWorld就是指“;” Fi ...
- linux IO诊断命令集
IO.sh ##iostat是查看磁盘活动统计情况 ##显示全部设备负载情况 r/s: 每秒完毕的读 I/O 设备次数.即 rio/s:w/s: 每秒完毕的写 I/O 设备次数.即 wio/s等 io ...
- javasctipt显示几分钟前、几天前等
jsp页面: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ...
- 经常使用的webservice接口
Web Service 一些对外公开的网络服务接口 2011-10-29 14:12 商业和贸易: 1.股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同一时候查询) En ...
- CPU相关信息
unit untCpuInfo;interface{ 获取 CPU 制造商 }function GetCpuFactory: String;{ 获取 CPU 家族系统 }function GetCpu ...
- esui控件validatebox 通过正则判断输入 json传值
<td> @Html.TextBoxFor(m => m.ActualInvoiceFee, new { @id = "txtActualInvoiceFee", ...
- Starling性能优化技巧十五则
Starling的性能优化要点 一.尽可能减少状态变更 如您所知,Starling使用Stage3D来渲染所有的可见对象.这就意味着所有的绘制都是GPU完成的. 现在,Starling可以一个接一个的 ...
- C# Firefox Session Manager 文件的导出与管理
长时间使用Firefox会产生很多的session文件(使用了插件:Session Manager).这些文件保存了你浏览过的所有的网址.虽然我们可以使用firefox的书签功能.但有时候打开的网址过 ...