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 城市安排是一种不同寻常的方式,每个交叉路口有一条道路连接,一条道路最多连接两个不同的交叉路口.每个交叉路口不能连接他自己.道路旅行一端到另一端的时间是相同的,任何 ...
随机推荐
- CAS总结之Ticket篇
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- p2p項目”復活“之想
http://blog.csdn.net/christopherwu/article/details/23976503 原來以為p2p項目就要夭折,墮入絕望無奈的深淵之時,與多位朋友的交流,抬頭看見了 ...
- [React Fundamentals] Accessing Child Properties
When you're building your React components, you'll probably want to access child properties of the m ...
- QQ强制视频聊天
QQ强制视频聊天 http://ike.126.com 现在,使用QQ的用户已经非常多,QQ聊天已经成了大家的家常便饭,除了跟自己和朋友和同事等熟悉的人聊天外,跟陌生的网友聊天也占了相当大的比例, ...
- debian配置简单的vsftp服务器
Ubuntu Linux与Windows系统不同,Ubuntu Linux不会产生无用垃圾文件,但是在升级缓存中,Ubuntu Linux不会自动删除这些文件,今天就来说说这些垃圾文件清理方法. 1 ...
- Conclusion
Conclusion This concludes our brief look at building a simple, but fully functional, Zend Framework ...
- ubuntu 13.04 root权限设置方法详解
很多朋友安装升级Ubuntu 13.04之后不知道ubuntu 13.04 root权限设置的具体方法,今天这篇文章就将为大家详细介绍设置root权限的步骤,新手朋友可以来看一看哦~ Ubunto 1 ...
- Android View的绘制机制流程深入详解(二)
本系列文章主要着重深入介绍Android View的绘制机制及流程,第二篇主要介绍并分析Android视图的绘制的原理和流程.主要从 onMeasure().onLayout()和onDraw()这三 ...
- C#基础篇03
1:不管是实参还是形参,都在内存中开辟空间. 2:写一个方法,它的功能一定要单一,方法中最忌讳的就是出现提示用户输入的字眼. 3:out参数 如果在一个方法中,返回多个类型相同的值时,可以考虑返回一个 ...
- Hibernate学习笔记--------2.一多|多多的CRUD
一.一多关系 例如用户(Tb_User)和订单(Tb_Order)之间,一个用户对应了多个订单,多个订单对应一个用户. 除了基本的配置外,需要在用户类(单方)中添加订单的集合同样需要get/set方法 ...