这道题好吧,一开始便是拓扑排序的想法,搞了好久,试了多组测试数据,没错啊,可是没过。。。作孽啊,竟然忘了拓扑不能处理环,白浪费了一晚上。。。

只好用动态规划了。。

DP【time】【city】表示在time时刻到达city的最迟出发时间,当然,在这个时间不一定到city。

转移方程挺简单,不说你也会。

 #include <iostream>
#include <cstdio>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std; const int MAXN=;
const int MAXM=;
const int inf=;
int head[MAXN];
struct e{
int u,v;
int depart,arrival;
int next;
}edge[MAXM];
int tot,n,m,limit;
int start_city,des_city;
string start,destin;
int timeh[][]; void addedge(int u,int v,int de,int ar){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].depart=de;
edge[tot].arrival=ar;
edge[tot].next=head[u];
head[u]=tot++;
} void slove(){
for(int e=head[start_city];e!=-;e=edge[e].next){
if(edge[e].depart>=limit){
timeh[edge[e].v][edge[e].arrival]=edge[e].depart;
}
}
for(int i=;i<=;i++){
for(int j=;j<=n;j++){
if(timeh[j][i]!=-){
for(int e=head[j];e!=-;e=edge[e].next){
if(i<=edge[e].depart){
timeh[edge[e].v][edge[e].arrival]=max(timeh[edge[e].v][edge[e].arrival],timeh[j][i]);
}
}
}
}
}
for(int i=;i<=;i++){
if(timeh[des_city][i]!=-){
cout << "Departure " << setw() << setfill('');
cout << timeh[des_city][i] << " " << start << endl;
cout << "Arrival " << setw() << setfill('');
cout << i << " " << destin << endl;
return ;
}
}
cout << "No connection" << endl;
} int main(){
string station,pre,cur;
int cas=;int T,pretime,curtime,u,v,train;
scanf("%d",&T);
while(T--){
cas++; tot=;
memset(head,-,sizeof(head));
memset(timeh,-,sizeof(timeh));
map<string,int>city;
scanf("%d",&n);
for(int i=;i<=n;i++){
cin>>station;
city[station]=i;
}
scanf("%d",&train);
while(train--){
scanf("%d",&m);
for(int i=;i<=m;i++){
cin>>curtime>>cur;
if(i>){
u=city[pre];
v=city[cur];
addedge(u,v,pretime,curtime);
}
pre=cur;
pretime=curtime;
}
}
cin>>limit>>start>>destin;
start_city=city[start]; des_city=city[destin];
cout << "Scenario " << cas << endl;
slove();
cout << endl;
}
return ;
}

最新文章

  1. LeetCode:Search in Rotated Sorted Array I II
  2. jQuery特效
  3. js计时器的问题
  4. 越狱Season 1-Episode 15: By the Skin and the Teeth
  5. javascript scroll事件
  6. jquery压缩图片插件
  7. jquery中的一点工作小记
  8. 《Think in UML》读后感
  9. CNS数据库网站开发环境的配置
  10. python之zip打包
  11. 使用kbmMWConfiguration 让 kbmmw smartservice 更聪明
  12. 使用C++ stringstream来进行数据类型转换
  13. Centos7更改yum源
  14. Jmeter进阶篇之监控服务器cpu,内存
  15. 错误提示 nginx: [emerg] unknown directive &quot;gzip_static&quot;
  16. 安卓工作室 android studio 汉化后,报错。 设置界面打不开。Can&#39;t find resource for bundle java.util.PropertyResourceBundle, key emmet.bem.class.name.element.separator.label
  17. 各版本系统安装tesseract-ocr
  18. [蓝桥] 基础练习 十进制转十六进制 (java)
  19. jmeter修改ServerAgent的默认端口号
  20. PHP批量导出数据为excel表格

热门文章

  1. B1076 [SCOI2008]奖励关 状压dp&amp;&amp;期望dp
  2. 92. extjs specialkey监听回车按键
  3. Rocky(模拟)
  4. go之switch
  5. 辨析 singleton 和 prototype
  6. poj3669 广搜
  7. Microsoft SQL Server数据库学习(一)
  8. IT狂人职场路:揭秘华为百度高管如何炼成?
  9. 排序算法Java版
  10. msmq消息队列使用场景