题目大意:有一个城市的路线图,有N个交叉点,每两个交叉点之间只有一条路,现在想从交点u去交点v,不过这个路的交通比较特别,每个路都有一个交通灯,灯有两种颜色,蓝色和紫色,例如一条路线在交点s,t之间,如果想从s走到t,那么只有等s和t的交通灯的颜色一致的时候才可以从s走,求出来从u到v的最短时间。

分析:比较明显能看出来是一个最短路问题,不过里面夹杂的这个交通灯比较恶心,要随时能求出来两点点下一个相同颜色的时间,如果使用时间去枚举无疑是个比较笨的方法,注意到有个剩余时间,并且交通灯的每种颜色存在的最大值是100,所以可以判断出,一定会有重复情况出现,比如s剩余a,t剩余b,这种状态再次出现不会超过200次蓝紫灯循环,所以只需要枚举200次即可,如果还是找不带相同颜色,那么就说明这条路不能行走。ps.时间内存都比较小,边数比较多,所以邻接矩阵比连接表更省内存,注意有可能会有重边的情况。

代码如下:

=========================================================================================================================

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN = ;
const int oo = 1e9+; struct Junction
{
int op;///等于0是B, 等于1代表P
int Last;///剩余时间
int t[];///两种灯的转换时间 Junction NewOp(int time)
{///time时刻的状态
Junction res;
if(time >= Last)
{///当前时间大于剩余时间,等于的时候也需要转变成下一个灯
time -= Last;
time %= (t[]+t[]); if(time >= t[op^])
{
res.op = op;
res.Last = t[op] - (time-t[op^]);
}
else
{
res.op = op^;
res.Last = t[op^]-time;
}
}
else
{
res.op = op;
res.Last = Last - time;
} res.t[] = t[], res.t[] = t[]; return res;
}
int SameColor(Junction a, Junction b)
{///两个路灯下个相同颜色最少需要时间,-1表示不可能相同
///因为中间涉及到值得改变,所以不使用指针
int i, time=; for(i=; i<; i++)
{
if(a.op == b.op)
return time;
else if(a.Last >= b.Last)
{
time += b.Last;
a.Last -= b.Last;
b.Last = ;
}
else if(a.Last < b.Last)
{
time += a.Last;
b.Last -= a.Last;
a.Last = ;
} if(a.Last == )
{
a.op ^= ;
a.Last = a.t[a.op];
}
if(b.Last == )
{
b.op ^= ;
b.Last = b.t[b.op];
}
} return -;
}
}; Junction p[MAXN];///交叉点
int N;///交叉点个数
int G[MAXN][MAXN]; void Dij(int start, int end)
{
int dist[MAXN], path[MAXN]={};
int visit[MAXN]={}; for(int i=; i<=N; i++)
dist[i] = oo;
dist[start] = ; for(int t=; t<N; t++)
{
int index=start, Min=oo; for(int i=; i<=N; i++)
{
if(visit[i] == false && dist[i] < Min)
{
Min = dist[i];
index = i;
}
} if(visit[index])
break;
visit[index] = true; Junction u = p[index].NewOp(dist[index]), v; for(int i=; i<=N; i++)
{
if(!visit[i] && G[index][i] != oo && dist[i]>dist[index]+G[index][i])
{
v = p[i].NewOp(dist[index]);
int time = u.SameColor(u, v); if(time != - && dist[i] > dist[index]+G[index][i]+time)
{
dist[i] = dist[index] + G[index][i] + time;
path[i] = index;
}
}
}
} if(dist[end] == oo)
printf("0\n");
else
{
printf("%d\n", dist[end]); int k=, ans[MAXN]; while(end)
{
ans[k++] = end;
end = path[end];
} for(int i=k-; i>=; i--)
printf("%d%c", ans[i], i==?'\n':' ');
}
} int main()
{
int M, start, end;
char s[]; scanf("%d%d%d%d", &start, &end, &N, &M); for(int i=; i<=N; i++)
{
scanf("%s%d%d%d", s, &p[i].Last, &p[i].t[], &p[i].t[]);
p[i].op = (s[]=='B' ? : );
} int u, v, len; for(int i=; i<=N; i++)
for(int j=; j<=N; j++)
G[i][j] = oo; while(M--)
{
scanf("%d%d%d", &u, &v, &len);
G[u][v] = G[v][u] = min(G[u][v], len);
} Dij(start, end); return ;
}

Traffic Lights - SGU 103(最短路)的更多相关文章

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

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

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

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

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

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

  4. Traffic Lights

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

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

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

  6. SGU 103 Traffic Lights【最短路】

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

  7. sgu 103 Traffic Lights

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

  8. sgu 185 最短路建网络流

    题目:给出一个图,从图中找出两条最短路,使得边不重复. 分析:既然是最短路,那么,两条路径上的所有节点的入边(s,x).出边(x,e)必定是最优的,即 dis[x] = dis[s]+edge_dis ...

  9. TRAFFIC LIGHTS POJ 1158

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

随机推荐

  1. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...

  2. Codeforces 551D GukiZ and Binary Operations(矩阵快速幂)

    Problem D. GukiZ and Binary Operations Solution 一位一位考虑,就是求一个二进制序列有连续的1的种类数和没有连续的1的种类数. 没有连续的1的二进制序列的 ...

  3. Attributes(2): Displaying attributes for a class.(显示类属性)

    输出类属性   using System; using System.Reflection;   namespace Attribute02 { //用于Class和Struct类型 [Attribu ...

  4. jsonp跨域问题记录

    这段时间用H5做移动app开发,遇到不少之前做web的时候不曾遇到的问题,记录一下,共勉-- 首先说一个:js跨域取数的问题 描述:  之前做web都是通过后台获取数据,没考虑过跨域的问题.这次用h5 ...

  5. 配置安装theano环境(非GPU版)

    终于成功配置了theano环境,但由于本机没有gpu,所以配置的是非gpu版本的theano,下面将具体过程进行描述(安装成功后,有时对python的各种库进行更新时,可能会导致某个模块无法调用其他被 ...

  6. matlab拟合三维椭球

            同学问的,查了下资料. %需要拟合的点的坐标为(0,-174.802,990.048),(0.472,-171.284,995.463),(0.413,-168.639,1003.55 ...

  7. jQuery EasyUI parser 的使用场景

    转自原文地址:http://www.easyui.info/archives/216.html parser,故名意思,就是解析器的意思,别看他只有那么几行代码,jQuery Easyui 能够根据c ...

  8. lc面试准备:Implement Queue using Stacks

    1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...

  9. 通过Delphi获得qq安装路径

    procedure TForm1.Button2Click(Sender: TObject); var Reg:TRegistry; Val:TStrings; ii:System.Integer; ...

  10. BZOJ3715: [PA2014]Lustra

    3715: [PA2014]Lustra Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 237  Solved: 149[Submit][Status ...