这道题难得不是算法,而是处理。

题意就是让你求最短路,只有当两个点在某一秒颜色相同时,这条边才可以通行,输入首先给你 起点和终点, 然后给你 点数和边数, 接下来 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的更多相关文章

  1. sgu 103 Traffic Lights 解题报告及测试数据

    103. Traffic Lights Time limit per test: 0.25 second(s) Memory limit: 4096 kilobytes 题解: 1.其实就是求两点间的 ...

  2. SGU 103.Traffic Lights(最短路)

    时间: 0.50 second(s) 空间: 4096 kilobytes 输入: 标准输入 输出: 标准输出 Dingiville 城市的交通规则非常奇怪,城市公路通过路口相连,两个不同路口之间最多 ...

  3. SGU 103 Traffic Lights【最短路】

    题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=16530 题意: 给定每个点最初的颜色,最初颜色持续时间,以 ...

  4. 快速切题 sgu103. Traffic Lights 最短路 难度:1

    103. Traffic Lights Time limit per test: 0.25 second(s)Memory limit: 4096 kilobytes input: standardo ...

  5. Traffic Lights

    Traffic Lights time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. POJ1158 城市交通Traffic lights IOI 1999 (最短路)

    POJ1158 城市交通Traffic lights IOI 1999 (最短路) (1) 问题描述(probolem) 在d城里交通的安排不同寻常,城中有路口和路口之间的道路,再任意两个不同的路口之 ...

  7. Traffic Lights - SGU 103(最短路)

    题目大意:有一个城市的路线图,有N个交叉点,每两个交叉点之间只有一条路,现在想从交点u去交点v,不过这个路的交通比较特别,每个路都有一个交通灯,灯有两种颜色,蓝色和紫色,例如一条路线在交点s,t之间, ...

  8. [atAGC050E]Three Traffic Lights

    原题意可能略微有一些复杂,这里给出简述的题意-- 给定$g_{i}$和$r_{i}$(其中$1\le i\le 3$),求有多少个整数$t$满足: $0\le t< \prod_{i=1}^{3 ...

  9. TRAFFIC LIGHTS POJ 1158

    题目大意: 在Dingilville 城市安排是一种不同寻常的方式,每个交叉路口有一条道路连接,一条道路最多连接两个不同的交叉路口.每个交叉路口不能连接他自己.道路旅行一端到另一端的时间是相同的,任何 ...

随机推荐

  1. Microsoft.AspNet.FriendlyUrls发布到IIS后404报错的解决方案

    我一个项目都基本上做完了,结果部署到我服务器的时候结果一直报404 找不到 一看global.asax有个路由注册的代码 public static void RegisterRoutes(Route ...

  2. BAE3.0还不支持本地写入文件

    BAE3.0对比2.0做了很大的改动,对于安装应用方面也方便了很多,普通的应用表面上(下文就是讲为什么说表面上)不需要做什么适配.比如wp博客,直接修改wp-config.php,把数据库信息填一下就 ...

  3. node.js在windows下的学习笔记(9)---文件I/O模块

    开发中我们经常会有文件I/O的需求,node.js中提供一个名为fs的模块来支持I/O操作,fs模块的文件I/O是对标准POSIX函数的简单封装. 1.将"hello world" ...

  4. Ubuntu装机必备

    sudo apt-get install rar unrar p7zip* sudo apt-get install tree sudo apt-get install wine sudo apt-g ...

  5. LVS NAT模型

    1,环境 VMWare10, CentOS6.3 2,LVS NAT网络规划 可以看到Director机器有2个IP,也就是说需要2张网卡:Real Server只需要一个网卡. VIP: 虚拟IP, ...

  6. Mysql性能调优

    在MYSQL命令行客户端添加 \G 语句终止符可以让返回的结果集垂直显示. 一.查找运行缓慢的 SQL语句 :show full processlist ; 二.生成一个查询执行计划(Query Ex ...

  7. linux之Apache

    apache  在linux下命令 2. 编译Apache 在src目录下 ./configure 可用 ./configure--help|less 得到帮助, make, make install ...

  8. java_Cookie_example(你上次访问的时间)

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...

  9. 移动平台的meta标签-----神奇的功效

    对于桌面平台web布局中大家对meta标签再熟悉不过了,它永远位于 head 元素内部,对做SEO的朋友一定对meta有种特殊的感情吧,今天我们就来说说移动平台的meta标签,在移动平台meta标签究 ...

  10. 面试体验:Google 篇(转)

    http://www.cnblogs.com/cathsfz/archive/2012/08/08/google-interview-experience.html 尝试在自己的博客上搜索点东西,结果 ...