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 城市安排是一种不同寻常的方式,每个交叉路口有一条道路连接,一条道路最多连接两个不同的交叉路口.每个交叉路口不能连接他自己.道路旅行一端到另一端的时间是相同的,任何 ...
随机推荐
- 编码问题(utf-8,gbk,utf-16be)
utf-16be编码 中文汉字 英文字母 还有数字都是占用两个字节( java 是双字节编码 ) gbk编码 中文汉字占用2个字节:英文字母.数字占用一个字节 utf-8编码 中文汉字占用3个字节 ...
- MySQL的安装——源码方式(实验环境下测试用,真实环境请忽略此文)
#虚拟机是最初的的系统,我们在虚拟机里安装scp [root@serv01 ~]# yum install /usr/bin/scp -y #安装过程略 #我们拷贝MySQL的源码包到目标机的/roo ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- CPU相关信息
unit untCpuInfo;interface{ 获取 CPU 制造商 }function GetCpuFactory: String;{ 获取 CPU 家族系统 }function GetCpu ...
- ios运行某些工程时屏幕上下出现黑边的解决办法
今天准备了解下MVVM设计模式,于是就从GitHub上Down了一个MVVM的demo(地址在这)学习,下载之后,在模拟器上运行一下,出现如下图上下有黑边,以前也遇到过这个问题,但当时没有记录,现在还 ...
- nodeValue和innerHTML的比较
<ul id="parent"> <li>1</li> <li>2</li> <li>3</li> ...
- gcc的使用
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/6065410. ...
- bvp4c--语法
bvp4c--语法 1. bvp4c: sol = bvp4c(odefun,bcfun,solinit) sol = bvp4c(odefun,bcfun,solinit,options) so ...
- Nodejs v4.x.0API文档学习(1)简介
文档参考地址:https://nodejs.org/dist/latest-v4.x/docs/api/ 简介 下面是用nodejs编写的一个web服务的例子,返回"Hello World& ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...