原题传送门


题面

Lexicographically Minimum Walk

                             time limit per test2 secondsmemory limit per test1024 megabytesinput standard inputoutput standard output

problem descride

There is a directed graph G with N nodes and M edges. Each node is numbered 1 through N, and each edge is numbered 1 through M. For each i (1≤i≤M) , edge i goes from vertex ui_{i}i​ to vertex vi_{i}i​ and has a unique color ci_{i}i​ .

A walk is defined as a sequence of edges eee1_{1}1​ , eee2_{2}2​, ⋯, eeel_{l}l​ where for each 1≤k<l, vek_{ek}ek​ (the tail of edge ek) is the same as uek+1_{ek+1}ek+1​ (the head of edge ek+1_{k+1}k+1​). We can say a walk e1_{1}1​,e2_{2}2​,⋯,el_{l}l​ starts at vertex uuue1_{e1}e1​ and ends at vertex vvvel_{el}el​. Note that the same edge can appear multiple times in a walk.

The color sequence of a walk eee1_{1}1​ , eee2_{2}2​ , ⋯ , eeel_{l}l​ is defined as ccce1_{e1}e1​ , ccce2_{e2}e2​ , ⋯ , cccel_{el}el​.

Consider all color sequences of walks of length at most 1010010^{100}10100 from vertex S to vertex T in G . Write a program that finds the lexicographically minimum sequence among them.

Input

The first line of the input contains four space-separated integers N , M , S , and T (1≤N≤1000001≤N≤1000001≤N≤100000 , 0≤M≤3000000≤M≤3000000≤M≤300000 , 1≤S≤N1≤S≤N1≤S≤N , 1≤T≤N1≤T≤N1≤T≤N , S≠TS≠TS​=T).

Then M lines follow: the j (1≤j≤M1≤j≤M1≤j≤M)-th of them contains three space-separated integers uiu_{i}ui​, viv_{i}vi​ and cic_{i}ci​ (1≤uiu_{i}ui​, viv_{i}vi​≤N, uiu_{i}ui​≠viv_{i}vi​, 1≤cic_{i}ci​≤10910^9109); it describes a directional edge from vertex uiu_{i}ui​ to vertex viv_{i}vi​ with color cic_{i}ci​.

The graph doesn’t have multiple edges and each edge has a unique color. Formally, for any 1≤i<j≤M1≤i<j≤M1≤i<j≤M, cic_{i}ci​≠cjc_{j}cj​ and (uiu_{i}ui​,viv_{i}vi​)≠(uju_{j}uj​,vjv_{j}vj​) holds.

Output

If there is no walk from vertex S to vertex T, print “IMPOSSIBLE”. (without quotes)

Otherwise, let’s say a1a_{1}a1​ , a2a_{2}a2​ , ⋯ , ala_{l}al​ is the lexicographically minimum sequence among all color sequences of length at most 1010010^{100}10100 from vertex S to vertex T.

If l≤106l≤10^6l≤106, print a1a_{1}a1​ , a2a_{2}a2​ , ⋯ , ala_{l}al​ in the first line. There should be a space between each printed integer.
If l>106l>10^6l>106, print “TOO LONG”. (without quotes)

Sample Input

3 3 1 3
1 2 1
2 3 7
1 3 5

Sample Output

1 7

题意描述

给一张有向图,带边权且每条边的边权唯一。问从 ST 的路径中,字典序最小的路径。输出该路径的边权。

题目分析

思维+贪心。当前点选边时总是选取能够到达 T 且边权最小的边即可。先建一个反图,从 T 出发跑BFS,将所有能到达 T 的点都跑出来,然后从 S 出发跑原图的DFS,若跑到之前已经过的点则说明存在环,则是TOO LONG的情况。

具体代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <queue>using namespace std;const int maxn = 1e5 + 7;
const int inf = 1e9 + 7;int n, m, s, t, dd;
bool vis[maxn];
bool can[maxn];
vector < pair < int , int > > G[maxn];
vector < pair < int, int > > RG[maxn];
vector < int > ans;void bfs(int u)
{queue < int > que;que.push(u);while (!que.empty()){u = que.front();que.pop();can[u] = 1;for (int i = 0; i < RG[u].size(); i++){int v = RG[u][i].first;if (!can[v])que.push(v);can[v] = 1;}}
}bool dfs_2(int u)
{vis[u] = 1;if (u == t) return 1;int nxt = -1, val = inf;for (int i = 0; i < G[u].size(); i++){if (G[u][i].second < val && can[G[u][i].first]){nxt = G[u][i].first;val = G[u][i].second;}}if (vis[nxt]) return 0;ans.push_back(val);return dfs_2(nxt);
}int main()
{dd = 0;cin >> n >> m >> s >> t;for (int i = 1; i <= m; i++){int u, v, val;cin >> u >> v >> val;G[u].push_back({ v , val });RG[v].push_back({ u , val });}bfs(t);if (!can[s]){cout << "IMPOSSIBLE" << endl;}else{if (!dfs_2(s)) cout << "TOO LONG" << endl;elsefor (int tmp : ans)cout << tmp << " ";}return 0;
}

G-Lexicographically Minimum Walk[CF-Gym-102391][2019-2020 XX Open Cup, Grand Prix of Korea]相关推荐

  1. CF Gym 100227 I题 题解

    这场Gym全名是这个:2013-2014 CT S01E01: Extended 2000 ACM-ICPC East Central North America Regional Contest ( ...

  2. mastercam后处理升级_MastercamX9,2017,2018,2019,2020各版本后处理(功能全面)2019-11-23更新...

    马上注册,结识高手,享用更多资源,轻松玩转三维网社区. 您需要 登录 才可以下载或查看,没有帐号?注册 x 本帖最后由 737852701 于 2019-11-27 08:17 编辑& y4 ...

  3. 动态改变_【学校动态】在坚守与改变中追求卓越——礼县二中召开2019—2020学年度秋季学期第二次全体教职工大会...

    点击上方"礼县第二中学"关注我们 礼县二中召开2019-2020学年度秋季学期第二次全体教职工大会 9月22日晚,礼县二中召开2019-2020学年度秋季学期第二次全体教职工大会. ...

  4. 《2019~2020网络安全态势观察报告》重磅发布!

    [导读]过去一年多,各种 APT 攻击事件.勒索挖矿事件,数据泄露事件,漏洞攻击事件仍然不绝于耳.从 ATT&CK 模型框架的兴起到实战化攻防环境的建立,从反序列化漏洞的攻防博弈到 VPN 漏 ...

  5. 2018/2019/2020/2021/2022/2023年度计划阅读书籍(持续更新)

    2018/2019/2020/2021/2022/2023年度计划阅读书籍 1. Java加密与解密的艺术(第二版) 作者:梁栋 在读 2. Spring源码深度解析 作者:郝佳 在读 3. 深入理解 ...

  6. 预测超级计算机排名2020,足球超级计算机预测2019/2020英超联赛排名

    原标题:足球超级计算机预测2019/2020英超联赛排名 英超联赛很快就将拉开帷幕,人们都在急切地等待着.球迷们已经开始猜测他们俱乐部的最终排名怎么样了.但是光凭人们自己要想 预测英超联赛的最终结果是 ...

  7. 智慧树怎么导入教务系统的课_西南交通大学希望学院 关于2019—2020学年春季学期公共任选课的选课通知...

    西南交通大学希望学院 关于2019-2020学年春季学期公共任选课的选课通知 院内各教学单位: 根据学院教务处教学工作安排,2019-2020春季学期公共任选课选课工作即将开始.由于当前正处于疫情防控 ...

  8. 1479A - Searching Local Minimum 交互,二分,2019 ccpc 哈尔滨 E 拓扑排序

    1479A - Searching Local Minimum 交互,二分 找一个区间[l,r]始终满足a[l+1]>a[l]&&a[r]<a[r+1],然后不断缩小区间当 ...

  9. CF Gym 100187E Two Labyrinths (迷宫问题)

    题意:问两个迷宫是否存在公共最短路. 题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路 #include<cstdio> #include<cstring> #in ...

最新文章

  1. 当我们思考问题时,能还是不能,请别预设立场
  2. 批处理文件将多台连接的手机安装同一个APP
  3. 利用Python语言Appium启动ios app
  4. JDBC实现图书管理小案例
  5. Mongodb -(3) replica set+sharding
  6. 《C++并发编程实战》——1.1 什么是并发
  7. ROS学习(15)RoboWare Studio的安装使用
  8. FANUC机器人_KAREL编程入门(2)_通用IO信号的使用方法
  9. ngix入门 Linux系统Ubuntu ngix安装
  10. MLCC电容的直流偏压特性 贴片电容
  11. 对视频图像进行OSD叠加
  12. 适合记录日常工作的便签如何在电脑桌面上添加
  13. 实现Ubuntu与Windows之间的复制粘贴
  14. PL3366C-ASEMI移动电源管理IC
  15. 请问在 1 到 2020 中,有多少个数既是 4 的整数倍,又是 6 的整数倍。
  16. OS App体验设计
  17. Java SE 基础部分经典100道笔试题
  18. windows10 如何使用 debug
  19. Python 关于整除以及负数取余遇到的问题
  20. Audition CS6 安装到2%出现安装失败

热门文章

  1. 虎符CTF 2022 mva
  2. 独家 | 2019届互联网校招高薪清单出炉
  3. HTML 视频播放代码
  4. armadillo使用,armadillo提高编译效率和速度
  5. UVA 1626括号序列DP
  6. 【pwn】2021 鹤壁杯 wp
  7. 2006年中国动漫行业预测及投资分析报告
  8. redis安装、持久化、数据类型、常用操作、操作键值、安全设置、慢查询日志、存储session、主从配置、集群介绍、集群搭建配置、集群操作,php安装redis扩展...
  9. 吸烟者问题——进程同步
  10. 通过JAVA读取Visio