UVA 10039 Railroads
2024-08-31 05:08:55
这道题好吧,一开始便是拓扑排序的想法,搞了好久,试了多组测试数据,没错啊,可是没过。。。作孽啊,竟然忘了拓扑不能处理环,白浪费了一晚上。。。
只好用动态规划了。。
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 ;
}
最新文章
- LeetCode:Search in Rotated Sorted Array I II
- jQuery特效
- js计时器的问题
- 越狱Season 1-Episode 15: By the Skin and the Teeth
- javascript scroll事件
- jquery压缩图片插件
- jquery中的一点工作小记
- 《Think in UML》读后感
- CNS数据库网站开发环境的配置
- python之zip打包
- 使用kbmMWConfiguration 让 kbmmw smartservice 更聪明
- 使用C++ stringstream来进行数据类型转换
- Centos7更改yum源
- Jmeter进阶篇之监控服务器cpu,内存
- 错误提示 nginx: [emerg] unknown directive ";gzip_static";
- 安卓工作室 android studio 汉化后,报错。 设置界面打不开。Can&#39;t find resource for bundle java.util.PropertyResourceBundle, key emmet.bem.class.name.element.separator.label
- 各版本系统安装tesseract-ocr
- [蓝桥] 基础练习 十进制转十六进制 (java)
- jmeter修改ServerAgent的默认端口号
- PHP批量导出数据为excel表格