G-Lexicographically Minimum Walk[CF-Gym-102391][2019-2020 XX Open Cup, Grand Prix of Korea]
原题传送门
题面
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
题意描述
给一张有向图,带边权且每条边的边权唯一。问从 S 到 T 的路径中,字典序最小的路径。输出该路径的边权。
题目分析
思维+贪心。当前点选边时总是选取能够到达 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]相关推荐
- CF Gym 100227 I题 题解
这场Gym全名是这个:2013-2014 CT S01E01: Extended 2000 ACM-ICPC East Central North America Regional Contest ( ...
- mastercam后处理升级_MastercamX9,2017,2018,2019,2020各版本后处理(功能全面)2019-11-23更新...
马上注册,结识高手,享用更多资源,轻松玩转三维网社区. 您需要 登录 才可以下载或查看,没有帐号?注册 x 本帖最后由 737852701 于 2019-11-27 08:17 编辑& y4 ...
- 动态改变_【学校动态】在坚守与改变中追求卓越——礼县二中召开2019—2020学年度秋季学期第二次全体教职工大会...
点击上方"礼县第二中学"关注我们 礼县二中召开2019-2020学年度秋季学期第二次全体教职工大会 9月22日晚,礼县二中召开2019-2020学年度秋季学期第二次全体教职工大会. ...
- 《2019~2020网络安全态势观察报告》重磅发布!
[导读]过去一年多,各种 APT 攻击事件.勒索挖矿事件,数据泄露事件,漏洞攻击事件仍然不绝于耳.从 ATT&CK 模型框架的兴起到实战化攻防环境的建立,从反序列化漏洞的攻防博弈到 VPN 漏 ...
- 2018/2019/2020/2021/2022/2023年度计划阅读书籍(持续更新)
2018/2019/2020/2021/2022/2023年度计划阅读书籍 1. Java加密与解密的艺术(第二版) 作者:梁栋 在读 2. Spring源码深度解析 作者:郝佳 在读 3. 深入理解 ...
- 预测超级计算机排名2020,足球超级计算机预测2019/2020英超联赛排名
原标题:足球超级计算机预测2019/2020英超联赛排名 英超联赛很快就将拉开帷幕,人们都在急切地等待着.球迷们已经开始猜测他们俱乐部的最终排名怎么样了.但是光凭人们自己要想 预测英超联赛的最终结果是 ...
- 智慧树怎么导入教务系统的课_西南交通大学希望学院 关于2019—2020学年春季学期公共任选课的选课通知...
西南交通大学希望学院 关于2019-2020学年春季学期公共任选课的选课通知 院内各教学单位: 根据学院教务处教学工作安排,2019-2020春季学期公共任选课选课工作即将开始.由于当前正处于疫情防控 ...
- 1479A - Searching Local Minimum 交互,二分,2019 ccpc 哈尔滨 E 拓扑排序
1479A - Searching Local Minimum 交互,二分 找一个区间[l,r]始终满足a[l+1]>a[l]&&a[r]<a[r+1],然后不断缩小区间当 ...
- CF Gym 100187E Two Labyrinths (迷宫问题)
题意:问两个迷宫是否存在公共最短路. 题解:两个反向bfs建立层次图,一遍正向bfs寻找公共最短路 #include<cstdio> #include<cstring> #in ...
最新文章
- 当我们思考问题时,能还是不能,请别预设立场
- 批处理文件将多台连接的手机安装同一个APP
- 利用Python语言Appium启动ios app
- JDBC实现图书管理小案例
- Mongodb -(3) replica set+sharding
- 《C++并发编程实战》——1.1 什么是并发
- ROS学习(15)RoboWare Studio的安装使用
- FANUC机器人_KAREL编程入门(2)_通用IO信号的使用方法
- ngix入门 Linux系统Ubuntu ngix安装
- MLCC电容的直流偏压特性 贴片电容
- 对视频图像进行OSD叠加
- 适合记录日常工作的便签如何在电脑桌面上添加
- 实现Ubuntu与Windows之间的复制粘贴
- PL3366C-ASEMI移动电源管理IC
- 请问在 1 到 2020 中,有多少个数既是 4 的整数倍,又是 6 的整数倍。
- OS App体验设计
- Java SE 基础部分经典100道笔试题
- windows10 如何使用 debug
- Python 关于整除以及负数取余遇到的问题
- Audition CS6 安装到2%出现安装失败