题目链接:

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 版本库

    1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库 git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点 ...

  2. LUOGU P4171 [JSOI2010]满汉全席

    传送门 解题思路 2-SAT 裸题. 代码 #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  3. css上下左右居中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 本周汇总 动态rem适配移动端/块状元素居中/透明度

    1.动态rem适配移动端 !function(){ var width = document.documentElement.clientWidth; var head=document.getEle ...

  5. Lowest Common Ancestor (LCA)

    题目链接 In a rooted tree, the lowest common ancestor (or LCA for short) of two vertices u and v is defi ...

  6. LintCode_415 有效回文串

    给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 注意事项 你是否考虑过,字符串有可能是空字符串?这是面试过程中,面试官常常会问的问题. 在这个题目中,我们将空字符串判定为有效回 ...

  7. LintCode_100 删除排序数组中的重复数字 ||

    题目 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2,3]. C++代码 int rem ...

  8. Ubuntu 16.04 配置 L2tp 客户端

    #install lib -dev libsecret--dev libgtk--dev libglib2.-dev xl2tpd strongswan #install network-manage ...

  9. oracle基本认识

    概要图 1. 环境搭建 1.1 Oracle的安装 数据库的三个常用的用户及默认密码sys:change_on_installsystem:managerscott:tiger Oracle客户端: ...

  10. Oracle 优化效率

    一.链接: ORACLE多表查询优化 oracle的 分表 详解 -----表分区 Oracle数据库查询优化方案(处理上百万级记录如何提高处理查询速度) 数据库SQL优化大总结之 百万级数据库优化方 ...