题意:一个无向图,求起点到终点最少时间,限制:每个路口有灯,要灯颜色一样才能过去,灯之有俩种颜色,周期

变化,给定每个灯初态,时间。

思路:开始就想到直接DP,方程dp[k]=dp[i]+distance[i][k]+waittime(i,k),于是天真得BFS敲了这个,

SB啊!有些点松弛了,之后它又被松弛,它便还可以松弛别人啊!否则世界上最短路O(n)的算法就诞生了!

所以,一旦某个点被松弛了,它便可以再入队,啊哈?这不就是SPFA吗!!我去,这次是对SPFA更加理解了,

自己想法竟然遇上经典算法了。其本质就是DP,状态的转移,初期放的错误是:没有所有点都对k松弛,而k已经出队

(对别人松弛过了)。

这题较麻烦的地方是计算等待的时间,细节模拟,我是先算出当前时间,路口的状态,再讨论下接下来等待的

时间。

未1A:

复制差不多代码的时候2个数据没有改,导致错误。打印路劲的时候开始用栈(因为记录是逆序的)。导致爆内存,

以后尽量不要用栈记录!

改进:其实写等待函数可以分俩个函数,每个点当前状态一个,加一个算时间的函数。那样代码就减少很多,

并且也清晰很多。

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
struct nodes
{
int remain;
char initial;
int tb;
int tp;
};
nodes node[310];
int n,m;int s,l;
int times=0;
int e[50000][3];int head[310];int nume=0;
void inline addedge(int from,int to, int w)
{
e[nume][0]=to;e[nume][1]=head[from];head[from]=nume;
e[nume++][2]=w;
e[nume][0]=from;e[nume][1]=head[to];head[to]=nume;
e[nume++][2]=w;
}
int dp[310];
void readin()
{
cin>>s>>l>>n>>m;
for(int i=1;i<=n;i++)
{
head[i]=-1;
dp[i]=inf;
cin>>node[i].initial>>node[i].remain>>node[i].tb>>node[i].tp;
}
int from,to,w;
for(int i=0;i<m;i++)
{
cin>>from>>to>>w;
addedge(from,to,w);
}
}
char inline change(char s) //变色
{
if(s=='B')return 'P';
return 'B';
}
int gettime(int nowt,int u,int v) //计算等待时间
{
int reu=0,rev=0; //模拟出当前灯还剩余多少时间
int nowu=nowt-node[u].remain; //先减去开始的剩余时间
char cv=node[v].initial,cu=node[u].initial;//cu,cv模拟出当前灯
if(nowu==0) //干好
{
if(node[u].initial=='B'){cu='p';reu=node[u].tp;}
else {cu='B';reu=node[u].tb;}
}
else if(nowu<0) //剩余时间还有
{
reu=node[u].remain-nowt;
}
else
{
int re=nowu%(node[u].tb+node[u].tp);//多余
if(re==0)cu=change(cu); //讨论略繁琐
else if(cu=='B')
{
if(re<node[u].tp)
cu=change(cu);
}
else if(cu=='P')
{
if(re<node[u].tb)
cu=change(cu);
}
if(cu=='P')
{
reu=(node[u].tb+node[u].tp)-re-node[u].tb;
if(node[u].initial=='P')reu+=node[u].tb;
}
else if(cu=='B')
{
reu=(node[u].tb+node[u].tp)-re-node[u].tp;
if(node[u].initial=='B')reu+=node[u].tp;
}
}
int nowv=nowt-node[v].remain;
if(nowv==0)
{
if(node[v].initial=='B'){cv='p';rev=node[v].tp;}
else {cv='B';rev=node[v].tb;}
}
else if(nowv<0)
{
rev=-nowv;
}
else
{
int re=nowv%(node[v].tb+node[v].tp);
if(re==0)cv=change(cv);
else if(cv=='B')
{
if(re<node[v].tp)
cv=change(cv);
}
else if(cv=='P')
{
if(re<node[v].tb)
cv=change(cv);
}
if(cv=='P')
{
rev=(node[v].tb+node[v].tp)-re-node[v].tb;
if(node[v].initial=='P')rev+=node[v].tb;
}
else if(cv=='B')
{
rev=(node[v].tb+node[v].tp)-re-node[v].tp;
if(node[v].initial=='B')rev+=node[v].tp;
}
}
if(cu==cv)return 0;
else
{
if(reu>rev)
return rev;
if(reu<rev)
return reu;
else
{
cu=change(cu);
cv=change(cv);
if(cu=='B')
{
if(node[u].tb<node[v].tp)
return reu+node[u].tb;
else if(node[u].tb>node[v].tp)
return reu+node[v].tp;
else
{
if(node[u].tp<node[v].tb)
return reu+node[u].tb+node[u].tp;
else if(node[u].tp>node[v].tb)
return reu+node[v].tp+node[v].tb;
else
return inf;
}
}
else if(cu=='P')
{
if(node[u].tp<node[v].tb)
return reu+node[u].tp;
else if(node[u].tp>node[v].tb)
return reu+node[v].tb;
else
{
if(node[u].tb<node[v].tp)
return reu+node[u].tp+node[u].tb;
else if(node[u].tb>node[v].tp)
return reu+node[v].tb+node[v].tp;
else
return inf;
}
}
}
}
}
int vis[310];
int fa[310];
int way[310];
queue<int>q;
int main()
{
readin();
q.push(s);
dp[s]=0;
while(!q.empty()) //spfa
{
int cur=q.front();
q.pop();
vis[cur]=0;
for(int i=head[cur];i!=-1;i=e[i][1])
{
int v=e[i][0];
int getv=dp[cur]+e[i][2];
getv+=gettime(dp[cur],cur,v);
if(vis[v]==0&&getv<dp[v])
{
q.push(v);
vis[v]=1;
}
if(getv<inf&&getv<dp[v])
{
dp[v]=getv;
fa[v]=cur; //跟新时候记录父节点,不可记录子节点法,容易想到反例。
}
}
}
if(dp[l]>=inf){cout<<0<<endl;return 0;} //无解
cout<<dp[l]<<endl;
int i=l;
int num=0;
while(i!=s)
{
way[num++]=i;
i=fa[i];
}
way[num]=i;
for(int j=num;j>=0;j--)
{
if(j==0)printf("%d\n",way[j]);
else printf("%d ",way[j]);
}
return 0;
}

SGU103+POJ 1158 最短路/dp的更多相关文章

  1. POJ 3635 Full Tank? 【分层图/最短路dp】

    任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  2. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. BZOJ_1003_[ZJOI2006]物流运输_最短路+dp

    BZOJ_1003_[ZJOI2006]物流运输_最短路+dp 题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1003 分析: 这种一段一段的显 ...

  4. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  5. [USACO07NOV]牛继电器Cow Relays (最短路,DP)

    题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) ...

  6. P1772 [ZJOI2006]物流运输 最短路+DP

    思路:最短路+DP 提交:1次 题解: $f[i]$表示到第$i$天的最小代价,我们可以预先处理出$i,j$两天之间(包括$i,j$)都可通行的最短路的代价记做$s[i][j]$,然后有$f[i]=m ...

  7. Heavy Transportation POJ 1797 最短路变形

    Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...

  8. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  9. bzoj1003物流运输 最短路+DP

    bzoj1003物流运输 题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输 ...

随机推荐

  1. Vue的elementUI实现自定义主题

    使用vue开发项目,用到elementUI,根据官网的写法,我们可以自定义主题来适应我们的项目要求,下面来介绍一下两种方法实现的具体步骤,(可以参考官方文档自定义主题官方文档),先说项目中没有使用sc ...

  2. 浏览器window产生的缓存九种解决办法

    浏览器缓存(Browser Caching)是浏览器端保存数据用于快速读取或避免重复资源请求的优化机制,有效的缓存使用可以避免重复的网络请求和浏览器快速地读取本地数据,整体上加速网页展示给用户.浏览器 ...

  3. Python-DB接口规范

    threadsafety 线程安全级别.threadsafety 这是一个整数, 取值范围如下: 0:不支持线程安全, 多个线程不能共享此模块 1:初级线程安全支持: 线程可以共享模块, 但不能共享连 ...

  4. HTML5<picture>元素

    HTML5<picture>元素可以设置多张图片 <!DOCTYPE html><html><head><meta http-equiv=&quo ...

  5. javaEE(7)_自定义标签&JSTL标签(JSP Standard Tag Library)

    一.自定义标签简介 1.自定义标签主要用于移除Jsp页面中的java代码,jsp禁止出现一行java脚本. 2.使用自定义标签移除jsp页面中的java代码,只需要完成以下两个步骤: •编写一个实现T ...

  6. iOS 常用尺寸

    APP ICON: @1x:57*57 @2x:114*114 @3x:171*171  机型 屏幕尺寸   像素(px)pixel  点(pt)point    PPI iphone4s 3.5吋  ...

  7. Tcp 三次握手 四次分手

    看了 余晟以为的 “tcp没那么难吧”,算是对三次握手,四次分手有了一点点理解,记录下来以方便自己以后的查看. 原文链接:https://mp.weixin.qq.com/s?__biz=MzA3MD ...

  8. Java开发工具下载

    一.Tomcat下载: http://tomcat.apache.org/ 二.Maven下载: http://maven.apache.org/download.cgi 三.eclipse下载: h ...

  9. MacBook Pro休眠掉电、耗电量大问题解决方案

    1.前言 最近我的2015mbpMacBook Pro (Retina, 13-inch, early 2015)更新完10.14系统后,发现休眠待机一晚上后能掉5%电,白天待机4-5小时又掉了8%. ...

  10. python的web框架---Django项目

    Django项目之会议室预预订: 界面效果展示: 1.创建超级管理员,实现预定界面功能 2.预定界面: (一)基于pymysql设计数据表结构,理清前后端与用户交互逻辑.(用户表,会议室表,预定内容存 ...