题目链接:

http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=16530

题意:

给定每个点最初的颜色,最初颜色持续时间,以及每个颜色的持续时间。每个点的颜色蓝紫交替,只有等待到一条路的两个端点颜色相同才能通行。到达某点时颜色恰好变色,则按照变色之后的颜色考虑。

给定道路的花费,问最少需要多少时间。

分析:

最短路问题。

dijk可做,麻烦之处在于要加上等待时间。对于每个点,判断相邻点颜色是否一致,不一致则选取当前颜色持续时间较短的一个作为等待时间。颜色相同的循环周期最多3次,3次之后颜色仍然无法一致则该路不通。

注意点数很少,边数很多,肯定有重边存在,选择邻接矩阵的形式。

代码:

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
#define sa(a) scanf("%d", &a)
#define fi first
#define se second
#define MEM(a, b) memset(a, b, sizeof(a));
typedef pair<int, int >p;
const int maxn = 300 + 5, INF = 0x3f3f3f3f;
struct junction{int color; int r; int t[2];};
junction j[maxn];
int pa[maxn];
int dist[maxn];
int ss, tt;
int mm[maxn][maxn];
int getcolor(int time, int u)
{
if(time < j[u].r) return j[u].color;
int res = (time - j[u].r) % (j[u].t[0] + j[u].t[1]);
if(res < j[u].t[1 - j[u].color]) return 1 - j[u].color;
return j[u].color;
}
int hold(int u, int v, int time)
{
int ans = 0;
int cnt = 0;
int res1, res2, res;
while(cnt < 3){
int cc = getcolor(time, u);
int ccc = getcolor(time, v);
if(cc == ccc) return ans;
if(time< j[v].r) res1 = j[v].r - time;
else{
res = (time - j[v].r) % (j[v].t[0] + j[v].t[1]);
if(ccc == j[v].color) res1 = j[v].t[0] + j[v].t[1] - res;
else res1 = j[v].t[ccc] - res;
}
if(time < j[u].r) res2 = j[u].r - time;
else{
res = (time - j[u].r) % (j[u].t[0] + j[u].t[1]);
if(cc == j[u].color) res2 = j[u].t[0] + j[u].t[1] - res;
else res2 = j[u].t[cc] - res;
}
ans += min(res1, res2);
time += min(res1, res2);
cnt++;
}
return -1;
}
void dijkstra(int n)
{
priority_queue<p>q;
q.push(p(0,ss));
dist[ss] = 0;
while(!q.empty()){
p t = q.top();q.pop();
int u = t.se;
if(t.fi > dist[u]) continue;
for(int i = 1; i <= n;i++){
if(mm[u][i] == INF) continue;
int tmp = hold(i, u, dist[u]);
if(tmp == -1) continue;
if(mm[i][u] + tmp + dist[u] < dist[i]){
pa[i]=u;
dist[i] = mm[i][u] + tmp + dist[u];
q.push(p(dist[i], i));
}
}
}
}
void init()
{
MEM(pa, -1);
MEM(dist, 0x3f);
MEM(mm, 0x3f);
}
void output(int t)
{
if(t == -1) return;
output(pa[t]);
printf("%d ", t);
}
int main (void)
{
sa(ss),sa(tt);
init();
int n, m; sa(n),sa(m);
char c;
int a, b, d, t;
getchar();
for(int i = 1; i <= n; i++){
if(getchar() == 'B') t = 0;
else t = 1;
scanf("%d%d%d", &a, &b, &d);
j[i].color = t;
j[i].r = a;
j[i].t[0] = b;
j[i].t[1] = d;
getchar();
}
for(int i = 0; i < m; i++){
scanf("%d%d%d", &a, &b, &d);
mm[a][b] = mm[b][a] = d;
}
dijkstra(n);
if(dist[tt] == INF) return printf("0\n"), 0;
printf("%d\n", dist[tt]);
output(tt);
return 0;
}

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

    这道题难得不是算法,而是处理. 题意就是让你求最短路,只有当两个点在某一秒颜色相同时,这条边才可以通行,输入首先给你 起点和终点, 然后给你 点数和边数, 接下来 n 行 初始颜色,初始颜色持续时间, ...

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

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

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

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

  6. Traffic Lights

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

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

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

  8. LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)

    Extended Traffic 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/O Description Dhaka city ...

  9. Codeforces 954D Fight Against Traffic(BFS 最短路)

    题目链接:Fight Against Traffic 题意:有n个点个m条双向边,现在给出两个点S和T并要增加一条边,问增加一条边且S和T之间距离不变短的情况有几种? 题解:首先dfs求一下S到其他点 ...

随机推荐

  1. git与github建立链接(学习笔记)

    总结步骤: 1.将所有文件添加到本库 git add . 2. git commit -m "提示信息随便写" 3.查看git修改状态 git status 4.获取远程库与本地同 ...

  2. grpc入门2

    rpc-gateway使用(同时提供rpc和http接口) 介绍第三方库 https://github.com/grpc-ecosystem/grpc-gateway 在grpc之上加一层代理并转发, ...

  3. UVA1204 Fun Game

    Fun Game https://odzkskevi.qnssl.com/8d698323a1e07d605cdeea708ee8a01d?v=1508703139 [题解] 不难发现如果一个串的原串 ...

  4. 洛谷P1315 [NOIP2011提高组Day2T3] 观光公交

    P1315 观光公交 题目描述 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 0 分钟出现在 1号 ...

  5. 复习解析嵌套json

    在网络上找的一些经典的例子复习使用 一,解析嵌套json字符串,一个json字符串中嵌套另一个json字符串 { "msg": { "resCode": &qu ...

  6. cmd 运行 python

    ①cmd 进入行命令: ②输入 “python” + “空格”,即 ”python “:将已经写好的脚本文件拖拽到当前光标位置,然后敲回车运行即可.

  7. Kibana将语言设置为中文

    6.7以后系统开始支持中文了,修改语言只需要添加一行配置即可. 设置方法 在kibana.yml配置文件中添加一行配置 i18n.locale: "zh-CN" 修改后重启,可以看 ...

  8. Centos 下添加开机自启动服务和脚本【转】

    最近刚玩Centos7的系统,跟Centos6还是很多方面有改变的,这里记录一下怎么在Centos7下添加开机自启动脚本和服务的方法. 1.添加开机自启服务 我这里以docker 服务为例,设置如下两 ...

  9. git命令入门

    http://www.cocoachina.com/ios/20160629/16855.html 译者序:这是一篇给像我这样的新手或者是熟悉图形工具的老鸟看的.仅作为快速入门的教程. git 现在的 ...

  10. day38 16-Spring的Bean的装配:注解的方式

    Struts 2和hibernate也使用注解,但是使用注解在以后的开发中应用不多.但是可以说在整合的时候如何进行注解开发.在Spring中,注解必须会玩. package cn.itcast.spr ...