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

只好用动态规划了。。

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 ;
}

UVA 10039 Railroads的更多相关文章

  1. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  2. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  3. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

  4. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  7. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  8. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  9. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

随机推荐

  1. 跟渣渣辉玩ffms

    [SQL] /* Navicat MySQL Data Transfer Source Server : root Source Server Version : 50717 Source Host ...

  2. 最短路径问题(floyd)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1867 #include<stdio.h& ...

  3. 0428-mysql(事务、权限)

    1.事务 “事务”是一种可以保证“多条语句一次性执行完成”或“一条都不执行”的机制. 两种开始事务的方法: 1.set  autocommit = 0; //false,此时不再是一条语句一个事务了, ...

  4. hdu 3037Saving Beans(卢卡斯定理)

    Saving Beans Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  5. 明解C语言

    本文为阅读书籍<明解C语言-中级篇>所积累的知识点及编译书本代码时遇到的问题.部分对应代码在\Code_2018\BK_明解C语言目录下.每个代码内都含有程序功能.思路.疑惑点等内容,如有 ...

  6. Phoenix与Squirrel 是什么?

    不多说,直接上干货! 前言 Phoenix是HBase的开源SQL引擎. squirrel是windows上Phoneix可视化工具.  Phoenix的官网 http://phoenix.apach ...

  7. MessageDigest 加密和解密2

    package com.drawthink.platform.util; import java.security.MessageDigest; import java.security.NoSuch ...

  8. Struts2 在登录拦截器中对ajax请求的处理

    前言: 由于ajax请求不像http请求,可以直接进行页面跳转,你返回的所有东西,ajax都只会识别为一个字符串. 之前尝试的方法是在拦截器中返回一个标识给ajax,然后再在每一个ajax请求成功之后 ...

  9. 努比亚 Z17(Nubia NX563J) 解锁BootLoader 并刷入recovery

    工具下载链接:https://pan.baidu.com/s/1mjEzcyG 备用下载链接:https://pan.baidu.com/s/1eTdx6Zg 密码:1d3r 本篇教程教你如何傻瓜式解 ...

  10. OC对象的本质及分类

    Object-C的底层都是通过C/C++来实现的,所以OC中的对象也会转化成C/C++中的某一个数据结构, 我们在终端里通过指令 xcrun -sdk iphoneos clang -arch arm ...