当前位置:网站首页>693. 行程排序(map + 拓扑)
693. 行程排序(map + 拓扑)
2022-07-02 10:14:00 【Joanh_Lan】
题目如下
玛丽需要从某地飞往另一目的地,由于没有直达飞机,所以需要在中途转很多航班。
例如:SFO -> DFW DFW -> JFK JFK -> MIA MIA -> ORD。
显然旅途中不可能到同一中转城市两次或以上,因为这没有意义。
不幸的是,她将自己的机票的顺序搞乱了,将机票按乘坐顺序整理好对她来说不是一件容易的事。
请你帮助玛丽整理机票,使机票按正确顺序排列。
输入格式
第一行包含整数 T,表示共有 T组测试数据。
每组数据第一行包含整数 N。
接下来 2N行,每 2行一组,表示一张机票的信息,每行包含一个字符串,其中第一行表示出发地,第二行表目的地。
输出格式
每组数据输出一个结果,每个结果占一行。
结果表示为 Case #x: y,其中 x是组别编号(从 1 开始),y
是表示实际行程的机票列表,行程中的每个航段应以 source-destination 的形式输出,航段之间用空格隔开。
数据范围
1≤T≤100,1≤N≤10000
输入样例:
2
1
SFO
DFW
4
MIA
ORD
DFW
JFK
SFO
DFW
JFK
MIA
输出样例:
Case #1: SFO-DFW
Case #2: SFO-DFW DFW-JFK JFK-MIA MIA-ORD
思路:
map + 拓扑
AC代码如下:
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 10009;
int d[N];
int n;
map<string, int> mp;
int h[N], e[N], ne[N], idx;
int cnt;
string ch[N];
int rou;
void add(int a, int b)
{
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
void topsort()
{
int q[N];
int tt = -1, hh = 0;
for (int i = 1; i <= cnt; i++)
if (!d[i])
{
q[++tt] = i;
break;
}
while (hh <= tt)
{
int t = q[hh++];
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
d[j]--;
if (!d[j])
q[++tt] = j;
}
}
for (int i = 0; i < cnt - 1; i++)
cout << ch[q[i]] << '-' << ch[q[i + 1]] << ' ';
}
void solve()
{
rou++;
mp.clear();
memset(d, 0, sizeof d);
memset(h, -1, sizeof h);
cnt = 0, idx = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
string a, b;
cin >> a >> b;
if (!mp[a])
{
mp[a] = ++cnt;
ch[cnt] = a;
}
if (!mp[b])
{
mp[b] = ++cnt;
ch[cnt] = b;
}
d[mp[b]]++;
add(mp[a], mp[b]);
}
cout << "Case #" << rou << ": ";
topsort();
cout << '\n';
}
int main()
{
int t;
cin >> t;
while (t--)
solve();
}
边栏推荐
- 基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
- Unity SKFramework框架(十三)、Question 问题模块
- linux下清理系统缓存并释放内存
- How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
- Find love for speed in F1 delta time Grand Prix
- 操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
- TVOC, VOC, VOCs gas detection + Solution
- We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
- Solution: Compression Technology (original version and sequel version)
- 验证失败,请检查您的回电网址。您可以按照指导进行操作
猜你喜欢
Solution: Compression Technology (original version and sequel version)
三翼鸟两周年:羽翼渐丰,腾飞指日可待
Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
Unity skframework framework (XII), score scoring module
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
Three methods of finding LCA of the nearest common ancestor
Web Foundation
Daily practice of C language --- monkeys divide peaches
Node. JS accessing PostgreSQL database through ODBC
随机推荐
【OpenGL】笔记二十九、高级光照(镜面高光)
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
[Unity]使用GB2312,打包后程序不正常解决方案
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
题解《子数整数》、《欢乐地跳》、《开灯》
量子三体问题: Landau Fall
EasyDSS点播服务分享时间出错如何修改?
D为何链接不了dll
[unity] using GB2312, the solution to abnormal program after packaging
Clean up system cache and free memory under Linux
[indomitable medal activity] life goes on and writing goes on
三翼鸟两周年:羽翼渐丰,腾飞指日可待
JS逆向之行行查data解密
How to explain binary search to my sister? This is really difficult, fan!
Mysql常用命令详细大全
Pocket Raider comments
Unity SKFramework框架(十三)、Question 问题模块
Add sequence number column to query results in MySQL
二、帧模式 MPLS 操作