当前位置:网站首页>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();
}
边栏推荐
- Gee learning notes 2
- [Blue Bridge Cup] children's worship circle
- [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
- Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
- Explanation of 34 common terms on the Internet
- Android kotlin material design technology points
- Research shows that "congenial" is more likely to become friends
- Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
- 【云原生数据库】遇到慢SQL该怎么办(上)?
- 基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
猜你喜欢

Don't spend money, spend an hour to build your own blog website

Error function ERF

de4000h存储安装配置

Unity SKFramework框架(十二)、Score 计分模块

二、帧模式 MPLS 操作

Bridge of undirected graph
![[Blue Bridge Cup] children's worship circle](/img/ad/5af4fe76ad5d1426bc460904d7f049.jpg)
[Blue Bridge Cup] children's worship circle

Unity skframework framework (XXI), texture filter map resource filtering tool

Integral link, inertia link and proportion link in Simulink

Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
随机推荐
de4000h存储安装配置
Unity skframework framework (XIX), POI points of interest / information points
EasyDSS点播服务分享时间出错如何修改?
Explanation: here is your UFO, Goldbach conjecture
Quantum three body problem: Landau fall
Numpy array calculation
题解:《压缩技术》(原版、续集版)
JS reverse row query data decryption
量子三体问题: Landau Fall
Unity skframework framework (XX), VFX lab special effects library
文件的下载与图片的预览
Don't spend money, spend an hour to build your own blog website
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
最近公共祖先LCA的三种求法
Unity skframework framework (XXI), texture filter map resource filtering tool
口袋奇兵点评
Unity skframework framework (XIV), extension extension function
[Blue Bridge Cup] children's worship circle
Engineers who can't read device manuals are not good cooks
Pointer from entry to advanced (1)